mirror of
https://github.com/tumic0/GPXSee.git
synced 2025-06-27 03:29:16 +02:00
Allow advanced CSV formating in CRS files
This commit is contained in:
@ -1,67 +0,0 @@
|
||||
#include <QStringList>
|
||||
#include "csv.h"
|
||||
|
||||
/*
|
||||
RFC 4180 parser with the following enchancements:
|
||||
- allows an arbitrary delimiter
|
||||
- allows LF line ends in addition to CRLF line ends
|
||||
*/
|
||||
|
||||
bool CSV::readEntry(QStringList &list)
|
||||
{
|
||||
int state = 0;
|
||||
char c;
|
||||
QByteArray field;
|
||||
|
||||
while (_device->getChar(&c)) {
|
||||
switch (state) {
|
||||
case 0:
|
||||
if (c == '\r')
|
||||
state = 3;
|
||||
else if (c == '\n') {
|
||||
list.append(field);
|
||||
_line++;
|
||||
return true;
|
||||
} else if (c == _delimiter) {
|
||||
list.append(field);
|
||||
field.clear();
|
||||
} else if (c == '"') {
|
||||
if (!field.isEmpty())
|
||||
return false;
|
||||
state = 1;
|
||||
} else
|
||||
field.append(c);
|
||||
break;
|
||||
case 1:
|
||||
if (c == '"')
|
||||
state = 2;
|
||||
else {
|
||||
field.append(c);
|
||||
if (c == '\n')
|
||||
_line++;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (c == '"') {
|
||||
field.append('"');
|
||||
state = 1;
|
||||
} else if (c == _delimiter || c == '\r' || c == '\n') {
|
||||
_device->ungetChar(c);
|
||||
state = 0;
|
||||
} else
|
||||
return false;
|
||||
break;
|
||||
case 3:
|
||||
if (c == '\n') {
|
||||
_device->ungetChar(c);
|
||||
state = 0;
|
||||
} else
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
list.append(field);
|
||||
|
||||
return (_device->atEnd() && (state == 0 || state == 2));
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
#ifndef CSV_H
|
||||
#define CSV_H
|
||||
|
||||
#include <QIODevice>
|
||||
|
||||
class CSV
|
||||
{
|
||||
public:
|
||||
CSV(QIODevice *device, char delimiter = ',')
|
||||
: _device(device), _delimiter(delimiter), _line(1) {}
|
||||
|
||||
bool readEntry(QStringList &list);
|
||||
bool atEnd() const {return _device->atEnd();}
|
||||
int line() const {return _line;}
|
||||
|
||||
private:
|
||||
QIODevice *_device;
|
||||
char _delimiter;
|
||||
int _line;
|
||||
};
|
||||
|
||||
#endif // CSV_H
|
@ -1,5 +1,5 @@
|
||||
#include <QStringList>
|
||||
#include "csv.h"
|
||||
#include <QByteArrayList>
|
||||
#include "common/csv.h"
|
||||
#include "csvparser.h"
|
||||
|
||||
bool CSVParser::parse(QFile *file, QList<TrackData> &tracks,
|
||||
@ -10,10 +10,9 @@ bool CSVParser::parse(QFile *file, QList<TrackData> &tracks,
|
||||
Q_UNUSED(routes);
|
||||
Q_UNUSED(polygons);
|
||||
CSV csv(file);
|
||||
QStringList entry;
|
||||
QByteArrayList entry;
|
||||
bool ok;
|
||||
|
||||
|
||||
while (!csv.atEnd()) {
|
||||
if (!csv.readEntry(entry) || entry.size() < 3) {
|
||||
_errorString = "Parse error";
|
||||
@ -21,26 +20,24 @@ bool CSVParser::parse(QFile *file, QList<TrackData> &tracks,
|
||||
return false;
|
||||
}
|
||||
|
||||
double lon = entry.at(0).trimmed().toDouble(&ok);
|
||||
double lon = entry.at(0).toDouble(&ok);
|
||||
if (!ok || (lon < -180.0 || lon > 180.0)) {
|
||||
_errorString = "Invalid longitude";
|
||||
_errorLine = csv.line();
|
||||
return false;
|
||||
}
|
||||
double lat = entry.at(1).trimmed().toDouble(&ok);
|
||||
double lat = entry.at(1).toDouble(&ok);
|
||||
if (!ok || (lat < -90.0 || lat > 90.0)) {
|
||||
_errorString = "Invalid latitude";
|
||||
_errorLine = csv.line();
|
||||
return false;
|
||||
}
|
||||
Waypoint wp(Coordinates(lon, lat));
|
||||
wp.setName(entry.at(2).trimmed());
|
||||
wp.setName(entry.at(2));
|
||||
if (entry.size() > 3)
|
||||
wp.setDescription(entry.at(3).trimmed());
|
||||
wp.setDescription(entry.at(3));
|
||||
|
||||
waypoints.append(wp);
|
||||
|
||||
entry.clear();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include <cmath>
|
||||
#include <QStringList>
|
||||
#include "csv.h"
|
||||
#include "common/csv.h"
|
||||
#include "cupparser.h"
|
||||
|
||||
|
||||
@ -58,7 +58,8 @@ static double elevation(const QString &str)
|
||||
}
|
||||
|
||||
|
||||
bool CUPParser::waypoint(const QStringList &entry, QVector<Waypoint> &waypoints)
|
||||
bool CUPParser::waypoint(const QByteArrayList &entry,
|
||||
QVector<Waypoint> &waypoints)
|
||||
{
|
||||
if (entry.size() < 11) {
|
||||
_errorString = "Invalid number of fields";
|
||||
@ -85,7 +86,7 @@ bool CUPParser::waypoint(const QStringList &entry, QVector<Waypoint> &waypoints)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CUPParser::task(const QStringList &entry,
|
||||
bool CUPParser::task(const QByteArrayList &entry,
|
||||
const QVector<Waypoint> &waypoints, QList<RouteData> &routes)
|
||||
{
|
||||
if (entry.size() < 3) {
|
||||
@ -125,10 +126,9 @@ bool CUPParser::parse(QFile *file, QList<TrackData> &tracks,
|
||||
Q_UNUSED(tracks);
|
||||
Q_UNUSED(polygons);
|
||||
CSV csv(file);
|
||||
QStringList entry;
|
||||
QByteArrayList entry;
|
||||
SegmentType segment = Header;
|
||||
|
||||
|
||||
while (!csv.atEnd()) {
|
||||
if (!csv.readEntry(entry)) {
|
||||
_errorString = "CSV parse error";
|
||||
@ -159,7 +159,6 @@ bool CUPParser::parse(QFile *file, QList<TrackData> &tracks,
|
||||
return false;
|
||||
}
|
||||
|
||||
entry.clear();
|
||||
_errorLine = csv.line();
|
||||
}
|
||||
|
||||
|
@ -14,8 +14,8 @@ public:
|
||||
int errorLine() const {return _errorLine;}
|
||||
|
||||
private:
|
||||
bool waypoint(const QStringList &entry, QVector<Waypoint> &waypoints);
|
||||
bool task(const QStringList &entry, const QVector<Waypoint> &waypoints,
|
||||
bool waypoint(const QByteArrayList &entry, QVector<Waypoint> &waypoints);
|
||||
bool task(const QByteArrayList &entry, const QVector<Waypoint> &waypoints,
|
||||
QList<RouteData> &routes);
|
||||
|
||||
QString _errorString;
|
||||
|
Reference in New Issue
Block a user