1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/csvparser.cpp

49 lines
1013 B
C++
Raw Normal View History

2016-10-23 11:09:20 +02:00
#include "csvparser.h"
2016-10-29 10:40:30 +02:00
bool CSVParser::loadFile(QFile *file)
2016-10-23 11:09:20 +02:00
{
bool res;
2016-10-23 11:09:20 +02:00
int ln = 1;
_errorLine = 0;
_errorString.clear();
2016-10-29 10:40:30 +02:00
while (!file->atEnd()) {
QByteArray line = file->readLine();
2016-10-23 11:09:20 +02:00
QList<QByteArray> list = line.split(',');
if (list.size() < 3) {
2016-10-29 12:22:28 +02:00
_errorString = "Parse error";
2016-10-23 11:09:20 +02:00
_errorLine = ln;
return false;
}
qreal lat = list[0].trimmed().toDouble(&res);
if (!res || (lat < -90.0 || lat > 90.0)) {
2016-10-29 12:22:28 +02:00
_errorString = "Invalid latitude";
2016-10-23 11:09:20 +02:00
_errorLine = ln;
return false;
}
qreal lon = list[1].trimmed().toDouble(&res);
if (!res || (lon < -180.0 || lon > 180.0)) {
2016-10-29 12:22:28 +02:00
_errorString = "Invalid longitude";
2016-10-23 11:09:20 +02:00
_errorLine = ln;
return false;
}
Waypoint wp(Coordinates(lon, lat));
2016-10-23 11:09:20 +02:00
QByteArray ba = list[2].trimmed();
QString name = QString::fromUtf8(ba.data(), ba.size());
wp.setName(name);
if (list.size() > 3) {
ba = list[3].trimmed();
wp.setDescription(QString::fromUtf8(ba.data(), ba.size()));
}
_waypoints.append(wp);
ln++;
}
return true;
}