-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvmodel.cpp
108 lines (99 loc) · 1.77 KB
/
csvmodel.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <QFile>
#include "csvmodel.h"
CsvModel::CsvModel(QObject *parent) :
QAbstractItemModel(parent)
{
}
Qt::ItemFlags CsvModel::flags(const QModelIndex & index) const
{
return Qt::ItemIsSelectable|Qt::ItemIsDragEnabled|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable;
}
QVariant CsvModel::data(const QModelIndex &index, int role) const
{
return m_data[index.row()][index.column()];
}
QVariant CsvModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return m_dataHeaders[section];
}
int CsvModel::rowCount()
{
return m_data.keys().count();
}
int CsvModel::columnCount()
{
return m_dataHeaders.count();
}
bool CsvModel::setFile(QString filename, bool hasHeaders, QChar sep)
{
QFile f(filename);
if (f.open(QIODevice::ReadOnly))
{
if (hasHeaders)
{
QByteArray ba = f.readLine();
ba = ba.trimmed();
for (int i=0; i<ba.length(); i++)
{
m_headers = parseLine(ba);
}
}
return true;
}
return false;
}
QList<QStringList> parseData(QByteArray line, char sep)
{
QList<QStringList> out2;
QStringList out;
QByteArray myLine = line.trimmed();
QString val;
char q = 0;
while (i < myLine.length())
{
if (myLine[i] == '"')
{
if (q)
{
if (i+1 < len(myLine) && myLine[i+1] == '"')
{
val += '"';
i+=1;
} else {
q=0;
}
} else {
q='"';
}
} else {
if (myLine[i] == sep)
{
out.append(val);
val ='';
while (myLine[i+1] == ' ')
i += 1;
} else {
if (q==0)
{
if (myLine[i] == '\n')
{
out.append(val);
out2.append(out);
out.clear();
val='';
} else {
val += myLine[i];
}
} else {
val += myLine[i];
}
}
}
i+=1;
}
if (val.length())
out.append(val);
if (out.size())
out2.append(out);
return out2;
}