From 7f12c0ca959c31d54fb6e8d0d19bf0844da0aaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Thu, 27 Oct 2016 09:15:05 +0200 Subject: [PATCH] TCX parser code cleanup --- src/gpxparser.h | 1 - src/tcxparser.cpp | 87 ++++++++++++++++++++++++++--------------------- src/tcxparser.h | 17 +++++---- 3 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/gpxparser.h b/src/gpxparser.h index a159bc85..58f62dfe 100644 --- a/src/gpxparser.h +++ b/src/gpxparser.h @@ -2,7 +2,6 @@ #define GPXPARSER_H #include -#include #include "parser.h" diff --git a/src/tcxparser.cpp b/src/tcxparser.cpp index 0567ebf2..b7b1c272 100644 --- a/src/tcxparser.cpp +++ b/src/tcxparser.cpp @@ -1,6 +1,28 @@ #include "tcxparser.h" +qreal TCXParser::number() +{ + bool res; + qreal ret = _reader.readElementText().toDouble(&res); + if (!res) + _reader.raiseError(QString("Invalid %1.").arg( + _reader.name().toString())); + + return ret; +} + +QDateTime TCXParser::time() +{ + QDateTime d = QDateTime::fromString(_reader.readElementText(), + Qt::ISODate); + if (!d.isValid()) + _reader.raiseError(QString("Invalid %1.").arg( + _reader.name().toString())); + + return d; +} + Coordinates TCXParser::position() { Coordinates pos; @@ -11,13 +33,13 @@ Coordinates TCXParser::position() if (_reader.name() == "LatitudeDegrees") { val = _reader.readElementText().toDouble(&res); if (!res || (val < -90.0 || val > 90.0)) - _reader.raiseError("Invalid latitude."); + _reader.raiseError("Invalid LatitudeDegrees."); else pos.setLat(val); } else if (_reader.name() == "LongitudeDegrees") { val = _reader.readElementText().toDouble(&res); if (!res || (val < -180.0 || val > 180.0)) - _reader.raiseError("Invalid longitude."); + _reader.raiseError("Invalid LongitudeDegrees."); else pos.setLon(val); } else @@ -27,78 +49,71 @@ Coordinates TCXParser::position() return pos; } -void TCXParser::trackpointData(Trackpoint &t) +void TCXParser::trackpointData(Trackpoint &trackpoint) { while (_reader.readNextStartElement()) { if (_reader.name() == "Position") - t.setCoordinates(position()); + trackpoint.setCoordinates(position()); else if (_reader.name() == "AltitudeMeters") - t.setElevation(_reader.readElementText().toDouble()); + trackpoint.setElevation(number()); else if (_reader.name() == "Time") - t.setTimestamp(QDateTime::fromString(_reader.readElementText(), - Qt::ISODate)); + trackpoint.setTimestamp(time()); else if (_reader.name() == "HeartRateBpm") - t.setHeartRate(_reader.readElementText().toDouble()); + trackpoint.setHeartRate(number()); else _reader.skipCurrentElement(); } } -void TCXParser::routepointData(Waypoint &w) +void TCXParser::routepointData(Waypoint &waypoint) { while (_reader.readNextStartElement()) { if (_reader.name() == "Position") - w.setCoordinates(position()); + waypoint.setCoordinates(position()); else if (_reader.name() == "AltitudeMeters") - w.setElevation(_reader.readElementText().toDouble()); + waypoint.setElevation(number()); else if (_reader.name() == "Time") - w.setTimestamp(QDateTime::fromString(_reader.readElementText(), - Qt::ISODate)); + waypoint.setTimestamp(time()); else _reader.skipCurrentElement(); } } -void TCXParser::waypointData(Waypoint &w) +void TCXParser::waypointData(Waypoint &waypoint) { while (_reader.readNextStartElement()) { if (_reader.name() == "Position") - w.setCoordinates(position()); + waypoint.setCoordinates(position()); else if (_reader.name() == "Name") - w.setName(_reader.readElementText()); + waypoint.setName(_reader.readElementText()); else if (_reader.name() == "Notes") - w.setDescription(_reader.readElementText()); + waypoint.setDescription(_reader.readElementText()); else if (_reader.name() == "AltitudeMeters") - w.setElevation(_reader.readElementText().toDouble()); + waypoint.setElevation(number()); else if (_reader.name() == "Time") - w.setTimestamp(QDateTime::fromString(_reader.readElementText(), - Qt::ISODate)); + waypoint.setTimestamp(time()); else _reader.skipCurrentElement(); } } -void TCXParser::trackpoints() +void TCXParser::trackpoints(QVector &track) { while (_reader.readNextStartElement()) { if (_reader.name() == "Trackpoint") { - Trackpoint t; - trackpointData(t); - if (t.coordinates().isValid()) - _track->append(t); + track.append(Trackpoint()); + trackpointData(track.back()); } else _reader.skipCurrentElement(); } } -void TCXParser::routepoints() +void TCXParser::routepoints(QVector &route) { while (_reader.readNextStartElement()) { if (_reader.name() == "Trackpoint") { - Waypoint w; - routepointData(w); - if (w.coordinates().isValid()) - _route->append(w); + route.append(Waypoint()); + routepointData(route.back()); } else _reader.skipCurrentElement(); } @@ -109,8 +124,7 @@ void TCXParser::lap() while (_reader.readNextStartElement()) { if (_reader.name() == "Track") { _tracks.append(QVector()); - _track = &_tracks.back(); - trackpoints(); + trackpoints(_tracks.back()); } else _reader.skipCurrentElement(); } @@ -121,13 +135,10 @@ void TCXParser::course() while (_reader.readNextStartElement()) { if (_reader.name() == "Track") { _routes.append(QVector()); - _route = &_routes.back(); - routepoints(); + routepoints(_routes.back()); } else if (_reader.name() == "CoursePoint") { - Waypoint w; - waypointData(w); - if (w.coordinates().isValid()) - _waypoints.append(w); + _waypoints.append(Waypoint()); + waypointData(_waypoints.back()); } else _reader.skipCurrentElement(); } diff --git a/src/tcxparser.h b/src/tcxparser.h index 9bbcadc2..b8e31c52 100644 --- a/src/tcxparser.h +++ b/src/tcxparser.h @@ -2,7 +2,6 @@ #define TCXPARSER_H #include -#include #include "parser.h" @@ -11,7 +10,7 @@ class TCXParser : public Parser public: TCXParser(QList > &tracks, QList > &routes, QList &waypoints) - : Parser(tracks, routes, waypoints) {_track = 0; _route = 0;} + : Parser(tracks, routes, waypoints) {} ~TCXParser() {} bool loadFile(QIODevice *device); @@ -26,16 +25,16 @@ private: void course(); void activity(); void lap(); - void trackpoints(); - void routepoints(); - void trackpointData(Trackpoint &t); - void routepointData(Waypoint &w); - void waypointData(Waypoint &w); + void trackpoints(QVector &track); + void routepoints(QVector &route); + void trackpointData(Trackpoint &trackpoint); + void routepointData(Waypoint &waypoint); + void waypointData(Waypoint &waypoint); Coordinates position(); + qreal number(); + QDateTime time(); QXmlStreamReader _reader; - QVector *_track; - QVector *_route; }; #endif // TCXPARSER_H