-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathcsvhelper.cpp
92 lines (81 loc) · 2.21 KB
/
csvhelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "csvhelper.h"
#include <QStringList>
#include <QChar>
#include "every_cpp.h"
namespace BrowserAutomationStudioFramework
{
CsvHelper::CsvHelper(QObject *parent) :
ICsvHelper(parent)
{
}
QString CsvHelper::Generate(const QStringList& List, QChar Separator)
{
QString res;
bool First = true;
foreach(QString Item,List)
{
QString add = Item;
if(Item.contains(";") || Item.contains(":") || Item.contains(","))
{
add = QString("\"") + add.replace("\"","\"\"") + QString("\"");
}
if(!First)
res.append(Separator);
res.append(add);
First = false;
}
return res;
}
QStringList CsvHelper::Parse(const QString& Line)
{
QStringList res;
int index = 0, len = Line.length();
bool InsideQuotes = false;
QString CurrentElement;
while(index<len)
{
QChar c = Line.at(index);
QChar n;
bool HasNext = index + 1 < len;
if(HasNext)
n = Line.at(index + 1);
index ++;
if(c == '\"')
{
if(InsideQuotes)
{
if(!HasNext || n != '\"')
{
InsideQuotes = false;
continue;
}else
{
CurrentElement.append("\"");
index++;
continue;
}
}else
{
InsideQuotes = true;
continue;
}
}
if(c == ':' || c == ';' || c == ',')
{
if(InsideQuotes)
{
CurrentElement.append(c);
continue;
}else
{
res.append(CurrentElement);
CurrentElement.clear();
continue;
}
}
CurrentElement.append(c);
}
res.append(CurrentElement);
return res;
}
}