1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45:53 +01:00

Some more error handling fixes.

This commit is contained in:
Martin Tůma 2016-10-29 10:40:30 +02:00
parent 78fb5c1547
commit 640829d89d
11 changed files with 45 additions and 25 deletions

View File

@ -2,11 +2,6 @@
#include "wgs84.h" #include "wgs84.h"
#include "coordinates.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 qreal Coordinates::distanceTo(const Coordinates &c) const
{ {

View File

@ -19,7 +19,12 @@ public:
qreal lon() const {return _lon;} qreal lon() const {return _lon;}
qreal lat() const {return _lat;} 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; qreal distanceTo(const Coordinates &c) const;
QPointF toMercator() const; QPointF toMercator() const;

View File

@ -1,6 +1,6 @@
#include "csvparser.h" #include "csvparser.h"
bool CSVParser::loadFile(QIODevice *device) bool CSVParser::loadFile(QFile *file)
{ {
bool res; bool res;
int ln = 1; int ln = 1;
@ -8,8 +8,8 @@ bool CSVParser::loadFile(QIODevice *device)
_errorLine = 0; _errorLine = 0;
_errorString.clear(); _errorString.clear();
while (!device->atEnd()) { while (!file->atEnd()) {
QByteArray line = device->readLine(); QByteArray line = file->readLine();
QList<QByteArray> list = line.split(','); QList<QByteArray> list = line.split(',');
if (list.size() < 3) { if (list.size() < 3) {
_errorString = "Parse error."; _errorString = "Parse error.";

View File

@ -11,7 +11,7 @@ public:
{_errorLine = 0;} {_errorLine = 0;}
~CSVParser() {} ~CSVParser() {}
bool loadFile(QIODevice *device); bool loadFile(QFile *file);
QString errorString() const {return _errorString;} QString errorString() const {return _errorString;}
int errorLine() const {return _errorLine;} int errorLine() const {return _errorLine;}

View File

@ -235,10 +235,10 @@ bool GPXParser::parse()
return !_reader.error(); return !_reader.error();
} }
bool GPXParser::loadFile(QIODevice *device) bool GPXParser::loadFile(QFile *file)
{ {
_reader.clear(); _reader.clear();
_reader.setDevice(device); _reader.setDevice(file);
return parse(); return parse();
} }

View File

@ -12,7 +12,7 @@ public:
QList<Waypoint> &waypoints) : Parser(tracks, routes, waypoints) {} QList<Waypoint> &waypoints) : Parser(tracks, routes, waypoints) {}
~GPXParser() {} ~GPXParser() {}
bool loadFile(QIODevice *device); bool loadFile(QFile *file);
QString errorString() const {return _reader.errorString();} QString errorString() const {return _reader.errorString();}
int errorLine() const {return _reader.lineNumber();} int errorLine() const {return _reader.lineNumber();}

View File

@ -220,6 +220,9 @@ void KMLParser::point(Waypoint &waypoint)
} else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
if (waypoint.coordinates().isNull())
_reader.raiseError("Missing Point coordinates.");
} }
void KMLParser::Track(TrackData &track) void KMLParser::Track(TrackData &track)
@ -332,10 +335,10 @@ bool KMLParser::parse()
return !_reader.error(); return !_reader.error();
} }
bool KMLParser::loadFile(QIODevice *device) bool KMLParser::loadFile(QFile *file)
{ {
_reader.clear(); _reader.clear();
_reader.setDevice(device); _reader.setDevice(file);
return parse(); return parse();
} }

View File

@ -12,7 +12,7 @@ public:
QList<Waypoint> &waypoints) : Parser(tracks, routes, waypoints) {} QList<Waypoint> &waypoints) : Parser(tracks, routes, waypoints) {}
~KMLParser() {} ~KMLParser() {}
bool loadFile(QIODevice *device); bool loadFile(QFile *file);
QString errorString() const {return _reader.errorString();} QString errorString() const {return _reader.errorString();}
int errorLine() const {return _reader.lineNumber();} int errorLine() const {return _reader.lineNumber();}

View File

@ -3,11 +3,11 @@
#include <QString> #include <QString>
#include <QList> #include <QList>
#include <QFile>
#include "trackdata.h" #include "trackdata.h"
#include "routedata.h" #include "routedata.h"
#include "waypoint.h" #include "waypoint.h"
class QIODevice;
class Parser class Parser
{ {
@ -17,7 +17,7 @@ public:
_waypoints(waypoints) {} _waypoints(waypoints) {}
virtual ~Parser() {} virtual ~Parser() {}
virtual bool loadFile(QIODevice *device) = 0; virtual bool loadFile(QFile *file) = 0;
virtual QString errorString() const = 0; virtual QString errorString() const = 0;
virtual int errorLine() const = 0; virtual int errorLine() const = 0;

View File

@ -1,6 +1,13 @@
#include "tcxparser.h" #include "tcxparser.h"
void TCXParser::warning(const char *text)
{
QFile *file = static_cast<QFile *>(_reader.device());
fprintf(stderr, "%s:%lld: %s\n", qPrintable(file->fileName()),
_reader.lineNumber(), text);
}
qreal TCXParser::number() qreal TCXParser::number()
{ {
bool res; bool res;
@ -87,8 +94,12 @@ void TCXParser::trackpoints(TrackData &track)
{ {
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "Trackpoint") { if (_reader.name() == "Trackpoint") {
track.append(Trackpoint()); Trackpoint t;
trackpointData(track.back()); trackpointData(t);
if (!t.coordinates().isNull())
track.append(t);
else
warning("Missing Trackpoint coordinates");
} else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
@ -114,8 +125,12 @@ void TCXParser::course(TrackData &track)
else if (_reader.name() == "Notes") else if (_reader.name() == "Notes")
track.setDescription(_reader.readElementText()); track.setDescription(_reader.readElementText());
else if (_reader.name() == "CoursePoint") { else if (_reader.name() == "CoursePoint") {
_waypoints.append(Waypoint()); Waypoint w;
waypointData(_waypoints.back()); waypointData(w);
if (!w.coordinates().isNull())
_waypoints.append(w);
else
warning("Missing Trackpoint coordinates");
} else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
@ -177,10 +192,10 @@ bool TCXParser::parse()
return !_reader.error(); return !_reader.error();
} }
bool TCXParser::loadFile(QIODevice *device) bool TCXParser::loadFile(QFile *file)
{ {
_reader.clear(); _reader.clear();
_reader.setDevice(device); _reader.setDevice(file);
return parse(); return parse();
} }

View File

@ -12,7 +12,7 @@ public:
QList<Waypoint> &waypoints) : Parser(tracks, routes, waypoints) {} QList<Waypoint> &waypoints) : Parser(tracks, routes, waypoints) {}
~TCXParser() {} ~TCXParser() {}
bool loadFile(QIODevice *device); bool loadFile(QFile *file);
QString errorString() const {return _reader.errorString();} QString errorString() const {return _reader.errorString();}
int errorLine() const {return _reader.lineNumber();} int errorLine() const {return _reader.lineNumber();}
@ -31,6 +31,8 @@ private:
qreal number(); qreal number();
QDateTime time(); QDateTime time();
void warning(const char *text);
QXmlStreamReader _reader; QXmlStreamReader _reader;
}; };