1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-06-26 19:19:16 +02:00

Allow advanced CSV formating in CRS files

This commit is contained in:
2023-04-15 03:18:52 +02:00
parent f7865556ae
commit 2824751615
14 changed files with 149 additions and 150 deletions

69
src/common/csv.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <QByteArrayList>
#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(QByteArrayList &list)
{
int state = 0;
char c;
QByteArray field;
list.clear();
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));
}

22
src/common/csv.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef CSV_H
#define CSV_H
#include <QIODevice>
class CSV
{
public:
CSV(QIODevice *device, char delimiter = ',')
: _device(device), _delimiter(delimiter), _line(1) {}
bool readEntry(QByteArrayList &list);
bool atEnd() const {return _device->atEnd();}
int line() const {return _line;}
private:
QIODevice *_device;
char _delimiter;
int _line;
};
#endif // CSV_H