forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvparser.cpp
More file actions
298 lines (258 loc) · 10.7 KB
/
Copy pathcsvparser.cpp
File metadata and controls
298 lines (258 loc) · 10.7 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "csvparser.h"
#include <QTextStream>
CSVParser::CSVParser(bool trimfields, char fieldseparator, char quotechar)
: m_bTrimFields(trimfields)
, m_cFieldSeparator(fieldseparator)
, m_cQuoteChar(quotechar)
, m_pCSVProgress(nullptr)
, m_nBufferSize(4096)
{
}
CSVParser::~CSVParser()
{
delete m_pCSVProgress;
}
namespace {
// This function adds a character to an existing field structure. If necessary, it extends the buffer size.
inline void addChar(CSVField* field, char c)
{
// Increase buffer size if it is too small
if(field->buffer_length >= field->buffer_max_length)
{
field->buffer_max_length += 64;
field->buffer = static_cast<char*>(realloc(field->buffer, field->buffer_max_length));
}
// Add char to the end of the buffer and increase length by one
field->buffer[field->buffer_length++] = c;
}
// This function increases the size of the field list of an existing row. However, it only does so if the field list is currently full.
inline void increaseRowSize(CSVRow& r)
{
// Check if field list is full
if(r.num_fields >= r.max_num_fields)
{
// Increase field list size
r.max_num_fields += 5;
r.fields = static_cast<CSVField*>(realloc(r.fields, r.max_num_fields*sizeof(CSVField)));
// Initialise the newly created field structures
for(size_t i=r.num_fields;i<r.max_num_fields;i++)
{
r.fields[i].buffer = nullptr;
r.fields[i].buffer_length = 0;
r.fields[i].buffer_max_length = 0;
}
}
}
// This function adds a parsed field to a row structure. It returns a pointer to a new field object which can be used for the storing the
// contents of the next field. The function doesn't "add" the field to the row but assumes that the field has been constructed in-place.
// All it does is finish the field structure and update the row structure to take the new field into account.
inline CSVField* addColumn(CSVRow& r, CSVField* field, bool trim)
{
// Make the parsed data start and length identical with the buffer start and length
field->data = field->buffer;
field->data_length = field->buffer_length;
// If we have to trim the field, do this by manipulating the data start and data length variables
if(trim)
{
// Check for trailing spaces and omit them
while(field->data_length && isspace(*field->data))
{
field->data++;
field->data_length--;
}
// Check for pending spaces and omit them
while(field->data_length && isspace(field->data[field->data_length-1]))
field->data_length--;
}
// We assume here that the field object has been constructed in-place. So all we need to do for adding it to the row structure
// is increasing the field count by one to make sure the newly constructed field object is used.
r.num_fields++;
// Clear field buffer for next use
field->buffer_length = 0;
// Increase field list size if it is too small
increaseRowSize(r);
// Return pointer to the next field
return &r.fields[r.num_fields];
}
// This function takes a parsed CSV row and hands it back to the caller of the CSV parser. It returns a null pointer if the parsing should be
// aborted, otherwise it returns a pointer to a new field object that can be used for storing the contents of the first field of the next row.
inline CSVField* addRow(CSVParser::csvRowFunction& f, CSVRow& r, size_t& rowCount)
{
// Call row function
if(!f(rowCount, r))
return nullptr;
// Reset the field list by setting the field count to 0. No need to deconstruct anything else.
r.num_fields = 0;
// Increase row count by one, as we're now starting to parse the next row
rowCount++;
// Return a pointer to the first field in the row object because we're starting with the first field of the next row now
return r.fields;
}
}
CSVParser::ParserResult CSVParser::parse(csvRowFunction insertFunction, QTextStream& stream, size_t nMaxRecords)
{
ParseStates state = StateNormal; // State of the parser
QByteArray sBuffer; // Buffer for reading the file
CSVRow record; // Buffer for parsing the current row
size_t parsedRows = 0; // Number of rows parsed so far
CSVField* field; // Buffer for parsing the current field
if(m_pCSVProgress)
m_pCSVProgress->start();
// Initialise row buffer and get pointer to the first field
record = { nullptr, 0, 0 };
increaseRowSize(record);
field = record.fields;
// Make sure all buffers are freed when we're done here
class FieldBufferDealloc
{
public:
explicit FieldBufferDealloc(CSVRow& row) : m_row(row) {}
~FieldBufferDealloc()
{
for(size_t i=0;i<m_row.max_num_fields;i++)
free(m_row.fields[i].buffer);
free(m_row.fields);
}
private:
CSVRow& m_row;
};
FieldBufferDealloc dealloc(record);
qint64 bufferPos = 0;
while(!stream.atEnd())
{
sBuffer = stream.read(m_nBufferSize).toUtf8();
bufferPos += sBuffer.length();
auto sBufferEnd = sBuffer.constEnd();
for(auto it = sBuffer.constBegin(); it != sBufferEnd; ++it)
{
// Get next char
char c = *it;
switch(state)
{
case StateNormal:
{
if(c == m_cFieldSeparator)
{
field = addColumn(record, field, m_bTrimFields);
}
else if(c == m_cQuoteChar)
{
state = StateInQuote;
}
else if(c == '\r')
{
// look ahead to check for linefeed
auto nit = it + 1;
// In order to check what the next byte is we must make sure that that byte is already loaded. Assume we're at an m_nBufferSize
// boundary but not at the end of the file when we hit a \r character. Now we're going to be at the end of the sBuffer string
// because of the m_nBufferSize boundary. But this means that the following check won't work properly because we can't check the
// next byte when we really should be able to do so because there's more data coming. To fix this we'll check for this particular
// case and, if this is what's happening, we'll just load an extra byte.
if(nit == sBufferEnd && !stream.atEnd())
{
// Load one more byte
sBuffer.append(stream.read(1));
sBufferEnd = sBuffer.constEnd();
// Restore both iterators. sBufferEnd points to the imagined char after the last one in the string. So the extra byte we've
// just loaded is the one before that, i.e. the actual last one, and the original last char is the one before that.
it = sBufferEnd - 2;
nit = sBufferEnd - 1;
}
// no linefeed, so assume that CR represents a newline
if(nit != sBufferEnd && *nit != '\n')
{
field = addColumn(record, field, m_bTrimFields);
if(!(field = addRow(insertFunction, record, parsedRows)))
return ParserResult::ParserResultError;
}
}
else if(c == '\n')
{
field = addColumn(record, field, m_bTrimFields);
if(!(field = addRow(insertFunction, record, parsedRows)))
return ParserResult::ParserResultError;
}
else
{
addChar(field, c);
}
}
break;
case StateInQuote:
{
if(c == m_cQuoteChar)
{
state = StateEndQuote;
}
else
{
addChar(field, c);
}
}
break;
case StateEndQuote:
{
if(c == m_cQuoteChar)
{
state = StateInQuote;
addChar(field, c);
}
else if(c == m_cFieldSeparator)
{
state = StateNormal;
field = addColumn(record, field, m_bTrimFields);
}
else if(c == '\n')
{
state = StateNormal;
field = addColumn(record, field, m_bTrimFields);
if(!(field = addRow(insertFunction, record, parsedRows)))
return ParserResult::ParserResultError;
}
else if(c == '\r')
{
// look ahead to check for linefeed
auto nit = it + 1;
// See above for details on this.
if(nit == sBufferEnd && !stream.atEnd())
{
sBuffer.append(stream.read(1));
sBufferEnd = sBuffer.constEnd();
it = sBufferEnd - 2;
nit = sBufferEnd - 1;
}
// no linefeed, so assume that CR represents a newline
if(nit != sBufferEnd && *nit != '\n')
{
field = addColumn(record, field, m_bTrimFields);
if(!(field = addRow(insertFunction, record, parsedRows)))
return ParserResult::ParserResultError;
}
}
else
{
state = StateNormal;
addChar(field, c);
}
}
break;
}
if(nMaxRecords > 0 && parsedRows >= nMaxRecords)
return ParserResult::ParserResultSuccess;
}
if(m_pCSVProgress && parsedRows % 100 == 0)
{
if(!m_pCSVProgress->update(bufferPos))
return ParserResult::ParserResultCancelled;
}
}
if(record.num_fields)
{
field = addColumn(record, field, m_bTrimFields);
if(!(field = addRow(insertFunction, record, parsedRows)))
return ParserResult::ParserResultError;
}
if(m_pCSVProgress)
m_pCSVProgress->end();
return (state == StateNormal) ? ParserResult::ParserResultSuccess : ParserResult::ParserResultError;
}