diff --git a/src/csvparser.h b/src/csvparser.h index c1b09de4..8fabff88 100644 --- a/src/csvparser.h +++ b/src/csvparser.h @@ -14,7 +14,6 @@ public: bool loadFile(QIODevice *device); QString errorString() const {return _errorString;} int errorLine() const {return _errorLine;} - const char *name() const {return "CSV";} private: QString _errorString; diff --git a/src/data.cpp b/src/data.cpp index 03bd22eb..55a05d76 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "gpxparser.h" #include "tcxparser.h" @@ -9,16 +10,22 @@ Data::Data() : _errorLine(0) { - _parsers << new GPXParser(_track_data, _route_data, _waypoint_data); - _parsers << new TCXParser(_track_data, _route_data, _waypoint_data); - _parsers << new KMLParser(_track_data, _route_data, _waypoint_data); - _parsers << new CSVParser(_track_data, _route_data, _waypoint_data); + _parsers.insert("gpx", new GPXParser(_track_data, _route_data, + _waypoint_data)); + _parsers.insert("tcx", new TCXParser(_track_data, _route_data, + _waypoint_data)); + _parsers.insert("kml", new KMLParser(_track_data, _route_data, + _waypoint_data)); + _parsers.insert("csv", new CSVParser(_track_data, _route_data, + _waypoint_data)); } Data::~Data() { - for(int i = 0; i < _parsers.count(); i++) - delete _parsers.at(i); + QHash::iterator it; + + for (it = _parsers.begin(); it != _parsers.end(); it++) + delete it.value(); for (int i = 0; i < _tracks.count(); i++) delete _tracks.at(i); @@ -26,9 +33,19 @@ Data::~Data() delete _routes.at(i); } +void Data::createData() +{ + for (int i = 0; i < _track_data.count(); i++) + _tracks.append(new Track(_track_data.at(i))); + for (int i = 0; i < _route_data.count(); i++) + _routes.append(new Route(_route_data.at(i))); +} + bool Data::loadFile(const QString &fileName) { QFile file(fileName); + QFileInfo fi(fileName); + _errorString.clear(); _errorLine = 0; @@ -38,25 +55,31 @@ bool Data::loadFile(const QString &fileName) return false; } - for (int i = 0; i < _parsers.size(); i++) { - if (_parsers.at(i)->loadFile(&file)) { - for (int i = 0; i < _track_data.count(); i++) - _tracks.append(new Track(_track_data.at(i))); - for (int i = 0; i < _route_data.count(); i++) - _routes.append(new Route(_route_data.at(i))); + QHash::iterator it; + if ((it = _parsers.find(fi.suffix().toLower())) != _parsers.end()) { + if (it.value()->loadFile(&file)) { + createData(); return true; } - file.reset(); - } - fprintf(stderr, "Error loading data file: %s:\n", qPrintable(fileName)); - for (int i = 0; i < _parsers.size(); i++) { - fprintf(stderr, "%s: line %d: %s\n", _parsers.at(i)->name(), - _parsers.at(i)->errorLine(), qPrintable(_parsers.at(i)->errorString())); - if (_parsers.at(i)->errorLine() > _errorLine) { - _errorLine = _parsers.at(i)->errorLine(); - _errorString = _parsers.at(i)->errorString(); + _errorLine = it.value()->errorLine(); + _errorString = it.value()->errorString(); + } else { + for (it = _parsers.begin(); it != _parsers.end(); it++) { + if (it.value()->loadFile(&file)) { + createData(); + return true; + } + file.reset(); } + + fprintf(stderr, "Error loading data file: %s:\n", qPrintable(fileName)); + for (it = _parsers.begin(); it != _parsers.end(); it++) + fprintf(stderr, "%s: line %d: %s\n", qPrintable(it.key()), + it.value()->errorLine(), qPrintable(it.value()->errorString())); + + _errorLine = 0; + _errorString = "Unknown format"; } return false; diff --git a/src/data.h b/src/data.h index 094c2c7f..c70f08c2 100644 --- a/src/data.h +++ b/src/data.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include "waypoint.h" @@ -26,9 +27,11 @@ public: const QList &waypoints() const {return _waypoint_data;} private: + void createData(); + QString _errorString; int _errorLine; - QList _parsers; + QHash _parsers; QList _tracks; QList _routes; diff --git a/src/gpxparser.h b/src/gpxparser.h index 3121d3e3..360bc5dd 100644 --- a/src/gpxparser.h +++ b/src/gpxparser.h @@ -17,7 +17,6 @@ public: bool loadFile(QIODevice *device); QString errorString() const {return _reader.errorString();} int errorLine() const {return _reader.lineNumber();} - const char *name() const {return "GPX";} private: enum DataType { diff --git a/src/kmlparser.h b/src/kmlparser.h index 228619c3..9bea74c3 100644 --- a/src/kmlparser.h +++ b/src/kmlparser.h @@ -17,7 +17,6 @@ public: bool loadFile(QIODevice *device); QString errorString() const {return _reader.errorString();} int errorLine() const {return _reader.lineNumber();} - const char *name() const {return "KML";} private: bool parse(); diff --git a/src/parser.h b/src/parser.h index adbd7bac..72cec289 100644 --- a/src/parser.h +++ b/src/parser.h @@ -19,7 +19,6 @@ public: virtual bool loadFile(QIODevice *device) = 0; virtual QString errorString() const = 0; virtual int errorLine() const = 0; - virtual const char *name() const = 0; protected: QList > &_tracks; diff --git a/src/tcxparser.h b/src/tcxparser.h index b3f315ee..9bbcadc2 100644 --- a/src/tcxparser.h +++ b/src/tcxparser.h @@ -17,7 +17,6 @@ public: bool loadFile(QIODevice *device); QString errorString() const {return _reader.errorString();} int errorLine() const {return _reader.lineNumber();} - const char *name() const {return "TCX";} private: bool parse();