-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Target saving and loading done. Simple exception throwing/catching also
included!
- Loading branch information
Showing
8 changed files
with
157 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
#include "target.h" | ||
|
||
// Init the serialization seperator character(s) | ||
const QString Target::SER_SEP = "\t"; | ||
|
||
Target::Target(){ | ||
} | ||
|
||
Target::Target(const QString& iname, const int& ix, const int& iy) | ||
{ | ||
Target::Target(const QString& iname, const int& ix, const int& iy){ | ||
name = iname; | ||
x = ix; | ||
y = iy; | ||
} | ||
|
||
Target::~Target(){ | ||
} | ||
|
||
QTextStream& operator<<(QTextStream&strm, const Target &t) { | ||
return strm << t.name << Target::SER_SEP << t.x << Target::SER_SEP << t.y; | ||
} | ||
|
||
QTextStream& operator>>(QTextStream& strm, Target &t){ | ||
QString s = strm.readLine(); | ||
QStringList sl = s.split(Target::SER_SEP); | ||
// Decode the string | ||
QString name = sl[0]; | ||
int x = sl[1].toInt(); | ||
int y = sl[2].toInt(); | ||
// Make the object | ||
t = Target(name, x, y); | ||
return strm; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "targetfilehandler.h" | ||
|
||
const QString TargetFileHandler::fileExtension = ".targets"; | ||
|
||
TargetFileHandler::TargetFileHandler() | ||
{ | ||
} | ||
|
||
void TargetFileHandler::saveFile(const QVector<Target>& targets, const QString& fileName){ | ||
QFile file(fileName); | ||
// Open the file | ||
if (!file.open(QIODevice::WriteOnly)) { | ||
// TODO throw different errors depending on why | ||
throw FileWriteException(); | ||
} | ||
// Write targets out | ||
QTextStream out(&file); | ||
for (int i = 0; i < targets.size(); i++){ | ||
out << targets[i] << "\n"; | ||
} | ||
file.close(); | ||
} | ||
|
||
QVector<Target> TargetFileHandler::readFile(const QString& fileName){ | ||
QFile file(fileName); | ||
// Open the file | ||
if (!file.open(QIODevice::ReadOnly)) { | ||
// TODO throw different errors depending on why | ||
// ie. file doesn't exist | ||
throw FileReadException(); | ||
} | ||
// Read the file | ||
QVector<Target> targets; | ||
QTextStream in(&file); | ||
while(!in.atEnd()) { | ||
Target t; | ||
in >> t; | ||
targets.append(t); | ||
} | ||
file.close(); | ||
return targets; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef TARGETFILEHANDLER_H | ||
#define TARGETFILEHANDLER_H | ||
|
||
#include <QVector> | ||
#include <QString> | ||
#include <QFile> | ||
#include <QIODevice> | ||
#include <QMessageBox> | ||
#include <QTextStream> | ||
|
||
#include "target.h" | ||
|
||
using namespace std; | ||
|
||
class TargetFileHandler | ||
{ | ||
public: | ||
static const QString fileExtension; | ||
class FileReadException : public exception | ||
{ | ||
public: | ||
virtual const char* what() const throw() | ||
{ | ||
return "File Read Exception"; | ||
} | ||
}; | ||
class FileWriteException : public exception | ||
{ | ||
public: | ||
virtual const char* what() const throw() | ||
{ | ||
return "File Write Exception"; | ||
} | ||
}; | ||
|
||
public: | ||
TargetFileHandler(); | ||
|
||
void saveFile(const QVector<Target>&, const QString&); | ||
QVector<Target> readFile(const QString&); | ||
}; | ||
|
||
#endif // TARGETFILEHANDLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters