From 640829d89dd5e75c60ca1fbdcb7fa645a6f1e7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sat, 29 Oct 2016 10:40:30 +0200 Subject: [PATCH] Some more error handling fixes. --- src/coordinates.cpp | 5 ----- src/coordinates.h | 7 ++++++- src/csvparser.cpp | 6 +++--- src/csvparser.h | 2 +- src/gpxparser.cpp | 4 ++-- src/gpxparser.h | 2 +- src/kmlparser.cpp | 7 +++++-- src/kmlparser.h | 2 +- src/parser.h | 4 ++-- src/tcxparser.cpp | 27 +++++++++++++++++++++------ src/tcxparser.h | 4 +++- 11 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/coordinates.cpp b/src/coordinates.cpp index 200ac03b..d4603f47 100644 --- a/src/coordinates.cpp +++ b/src/coordinates.cpp @@ -2,11 +2,6 @@ #include "wgs84.h" #include "coordinates.h" -bool Coordinates::isValid() const -{ - return (_lon >= -180.0 && _lon <= 180.0 && _lat >= -90.0 && _lat <= 90.0) - ? true : false; -} qreal Coordinates::distanceTo(const Coordinates &c) const { diff --git a/src/coordinates.h b/src/coordinates.h index d979d5c8..9f09973b 100644 --- a/src/coordinates.h +++ b/src/coordinates.h @@ -19,7 +19,12 @@ public: qreal lon() const {return _lon;} qreal lat() const {return _lat;} - bool isValid() const; + bool isNull() const + {return (std::isnan(_lon) || std::isnan(_lat)) ? true : false;} + bool isValid() const + {return (_lon >= -180.0 && _lon <= 180.0 && _lat >= -90.0 + && _lat <= 90.0) ? true : false;} + qreal distanceTo(const Coordinates &c) const; QPointF toMercator() const; diff --git a/src/csvparser.cpp b/src/csvparser.cpp index 38764790..a6d244cd 100644 --- a/src/csvparser.cpp +++ b/src/csvparser.cpp @@ -1,6 +1,6 @@ #include "csvparser.h" -bool CSVParser::loadFile(QIODevice *device) +bool CSVParser::loadFile(QFile *file) { bool res; int ln = 1; @@ -8,8 +8,8 @@ bool CSVParser::loadFile(QIODevice *device) _errorLine = 0; _errorString.clear(); - while (!device->atEnd()) { - QByteArray line = device->readLine(); + while (!file->atEnd()) { + QByteArray line = file->readLine(); QList list = line.split(','); if (list.size() < 3) { _errorString = "Parse error."; diff --git a/src/csvparser.h b/src/csvparser.h index 93ea34d3..36b5c47a 100644 --- a/src/csvparser.h +++ b/src/csvparser.h @@ -11,7 +11,7 @@ public: {_errorLine = 0;} ~CSVParser() {} - bool loadFile(QIODevice *device); + bool loadFile(QFile *file); QString errorString() const {return _errorString;} int errorLine() const {return _errorLine;} diff --git a/src/gpxparser.cpp b/src/gpxparser.cpp index 11487633..778dede3 100644 --- a/src/gpxparser.cpp +++ b/src/gpxparser.cpp @@ -235,10 +235,10 @@ bool GPXParser::parse() return !_reader.error(); } -bool GPXParser::loadFile(QIODevice *device) +bool GPXParser::loadFile(QFile *file) { _reader.clear(); - _reader.setDevice(device); + _reader.setDevice(file); return parse(); } diff --git a/src/gpxparser.h b/src/gpxparser.h index 807f5fc9..1c9e28af 100644 --- a/src/gpxparser.h +++ b/src/gpxparser.h @@ -12,7 +12,7 @@ public: QList &waypoints) : Parser(tracks, routes, waypoints) {} ~GPXParser() {} - bool loadFile(QIODevice *device); + bool loadFile(QFile *file); QString errorString() const {return _reader.errorString();} int errorLine() const {return _reader.lineNumber();} diff --git a/src/kmlparser.cpp b/src/kmlparser.cpp index 80f966a7..a44bf871 100644 --- a/src/kmlparser.cpp +++ b/src/kmlparser.cpp @@ -220,6 +220,9 @@ void KMLParser::point(Waypoint &waypoint) } else _reader.skipCurrentElement(); } + + if (waypoint.coordinates().isNull()) + _reader.raiseError("Missing Point coordinates."); } void KMLParser::Track(TrackData &track) @@ -332,10 +335,10 @@ bool KMLParser::parse() return !_reader.error(); } -bool KMLParser::loadFile(QIODevice *device) +bool KMLParser::loadFile(QFile *file) { _reader.clear(); - _reader.setDevice(device); + _reader.setDevice(file); return parse(); } diff --git a/src/kmlparser.h b/src/kmlparser.h index 14c237c5..9596a610 100644 --- a/src/kmlparser.h +++ b/src/kmlparser.h @@ -12,7 +12,7 @@ public: QList &waypoints) : Parser(tracks, routes, waypoints) {} ~KMLParser() {} - bool loadFile(QIODevice *device); + bool loadFile(QFile *file); QString errorString() const {return _reader.errorString();} int errorLine() const {return _reader.lineNumber();} diff --git a/src/parser.h b/src/parser.h index 2dbc4bce..dcb31850 100644 --- a/src/parser.h +++ b/src/parser.h @@ -3,11 +3,11 @@ #include #include +#include #include "trackdata.h" #include "routedata.h" #include "waypoint.h" -class QIODevice; class Parser { @@ -17,7 +17,7 @@ public: _waypoints(waypoints) {} virtual ~Parser() {} - virtual bool loadFile(QIODevice *device) = 0; + virtual bool loadFile(QFile *file) = 0; virtual QString errorString() const = 0; virtual int errorLine() const = 0; diff --git a/src/tcxparser.cpp b/src/tcxparser.cpp index f4e96d58..74281ac8 100644 --- a/src/tcxparser.cpp +++ b/src/tcxparser.cpp @@ -1,6 +1,13 @@ #include "tcxparser.h" +void TCXParser::warning(const char *text) +{ + QFile *file = static_cast(_reader.device()); + fprintf(stderr, "%s:%lld: %s\n", qPrintable(file->fileName()), + _reader.lineNumber(), text); +} + qreal TCXParser::number() { bool res; @@ -87,8 +94,12 @@ void TCXParser::trackpoints(TrackData &track) { while (_reader.readNextStartElement()) { if (_reader.name() == "Trackpoint") { - track.append(Trackpoint()); - trackpointData(track.back()); + Trackpoint t; + trackpointData(t); + if (!t.coordinates().isNull()) + track.append(t); + else + warning("Missing Trackpoint coordinates"); } else _reader.skipCurrentElement(); } @@ -114,8 +125,12 @@ void TCXParser::course(TrackData &track) else if (_reader.name() == "Notes") track.setDescription(_reader.readElementText()); else if (_reader.name() == "CoursePoint") { - _waypoints.append(Waypoint()); - waypointData(_waypoints.back()); + Waypoint w; + waypointData(w); + if (!w.coordinates().isNull()) + _waypoints.append(w); + else + warning("Missing Trackpoint coordinates"); } else _reader.skipCurrentElement(); } @@ -177,10 +192,10 @@ bool TCXParser::parse() return !_reader.error(); } -bool TCXParser::loadFile(QIODevice *device) +bool TCXParser::loadFile(QFile *file) { _reader.clear(); - _reader.setDevice(device); + _reader.setDevice(file); return parse(); } diff --git a/src/tcxparser.h b/src/tcxparser.h index c6be3549..b11cb2c9 100644 --- a/src/tcxparser.h +++ b/src/tcxparser.h @@ -12,7 +12,7 @@ public: QList &waypoints) : Parser(tracks, routes, waypoints) {} ~TCXParser() {} - bool loadFile(QIODevice *device); + bool loadFile(QFile *file); QString errorString() const {return _reader.errorString();} int errorLine() const {return _reader.lineNumber();} @@ -31,6 +31,8 @@ private: qreal number(); QDateTime time(); + void warning(const char *text); + QXmlStreamReader _reader; };