1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00
GPXSee/src/data.cpp

96 lines
2.3 KiB
C++
Raw Normal View History

#include <QFile>
#include <QFileInfo>
2016-10-23 11:09:20 +02:00
#include <QLineF>
#include "gpxparser.h"
#include "tcxparser.h"
#include "csvparser.h"
#include "kmlparser.h"
#include "fitparser.h"
2016-11-10 00:08:11 +01:00
#include "igcparser.h"
2016-11-16 23:54:15 +01:00
#include "nmeaparser.h"
2016-10-23 11:09:20 +02:00
#include "data.h"
Data::Data() : _errorLine(0)
{
_parsers.insert("gpx", new GPXParser(_trackData, _routeData,
_waypointData));
_parsers.insert("tcx", new TCXParser(_trackData, _routeData,
_waypointData));
_parsers.insert("kml", new KMLParser(_trackData, _routeData,
_waypointData));
_parsers.insert("fit", new FITParser(_trackData, _routeData,
_waypointData));
_parsers.insert("csv", new CSVParser(_trackData, _routeData,
_waypointData));
2016-11-10 00:08:11 +01:00
_parsers.insert("igc", new IGCParser(_trackData, _routeData,
_waypointData));
2016-11-16 23:54:15 +01:00
_parsers.insert("nmea", new NMEAParser(_trackData, _routeData,
_waypointData));
2016-10-23 11:09:20 +02:00
}
Data::~Data()
{
QHash<QString, Parser*>::iterator it;
for (it = _parsers.begin(); it != _parsers.end(); it++)
delete it.value();
2016-10-23 11:09:20 +02:00
for (int i = 0; i < _tracks.count(); i++)
delete _tracks.at(i);
for (int i = 0; i < _routes.count(); i++)
delete _routes.at(i);
}
void Data::createData()
{
for (int i = 0; i < _trackData.count(); i++)
_tracks.append(new Track(_trackData.at(i)));
for (int i = 0; i < _routeData.count(); i++)
_routes.append(new Route(_routeData.at(i)));
}
2016-10-23 11:09:20 +02:00
bool Data::loadFile(const QString &fileName)
{
QFile file(fileName);
QFileInfo fi(fileName);
2016-10-23 11:09:20 +02:00
_errorString.clear();
_errorLine = 0;
if (!file.open(QFile::ReadOnly)) {
2016-10-23 11:09:20 +02:00
_errorString = qPrintable(file.errorString());
return false;
}
QHash<QString, Parser*>::iterator it;
if ((it = _parsers.find(fi.suffix().toLower())) != _parsers.end()) {
if (it.value()->loadFile(&file)) {
createData();
2016-10-23 11:09:20 +02:00
return true;
}
_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();
2016-10-23 11:09:20 +02:00
}
qWarning("Error loading data file: %s:\n", qPrintable(fileName));
for (it = _parsers.begin(); it != _parsers.end(); it++)
qWarning("%s: line %d: %s\n", qPrintable(it.key()),
it.value()->errorLine(), qPrintable(it.value()->errorString()));
_errorLine = 0;
_errorString = "Unknown format";
2016-10-23 11:09:20 +02:00
}
return false;
}