-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv_reader.h
617 lines (533 loc) · 12.4 KB
/
csv_reader.h
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#line 1 "csv_reader.rl"
/**
MIT License
Copyright (c) 2017 Josep Arús-Pous
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _CSV_READER_H_
#define _CSV_READER_H_
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <memory>
namespace csv {
namespace error {
struct base : std::exception {
virtual void formatErrorMessage() const = 0;
const char* what() const throw() {
this->formatErrorMessage();
return error_message.c_str();
}
mutable std::string error_message;
};
struct with_field_num {
with_field_num(size_t field_num): field_num(field_num) {}
size_t field_num;
};
struct with_record_num {
with_record_num(size_t record_num): record_num(record_num) {}
size_t record_num;
};
struct with_file_name {
with_file_name(std::string file_name): file_name(file_name) {}
std::string file_name;
};
// ERRORS
struct record_parsing :
base,
with_field_num {
record_parsing(size_t field_num): with_field_num(field_num) {}
virtual void formatErrorMessage() const {
std::ostringstream stream;
stream << "Parsing error in field " << this->field_num;
this->error_message = stream.str();
}
};
struct file_parsing :
base,
with_field_num,
with_record_num,
with_file_name {
file_parsing(const std::string &file_name, size_t record_num, size_t field_num):
with_field_num(field_num),
with_record_num(record_num),
with_file_name(file_name) {}
virtual void formatErrorMessage() const {
std::ostringstream stream;
stream << "Parsing error in file '" << this->file_name << "', record " << \
this->record_num << ", field " << this->field_num;
this->error_message = stream.str();
}
};
}
//
// RECORD PARSER
//
#line 150 "csv_reader.rl"
#line 117 "csv_reader.h"
static const int csv_record_parser_start = 1;
static const int csv_record_parser_first_final = 7;
static const int csv_record_parser_error = 0;
static const int csv_record_parser_en_main = 1;
#line 153 "csv_reader.rl"
class RecordParser {
public:
RecordParser() {}
// returns current position in buffer...
size_t parse(const char *begin, const char *end, std::vector<std::string> &fields) const {
// declare values
const char *p, *pe;
const char *ts, *te;
int cs;
// initialize all values
#line 140 "csv_reader.h"
{
cs = csv_record_parser_start;
}
#line 167 "csv_reader.rl"
p = begin;
pe = end;
bool finished = false;
size_t current_field = 0, chars_read = 0;
#line 153 "csv_reader.h"
{
if ( p == pe )
goto _test_eof;
switch ( cs )
{
tr1:
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st1;
tr4:
#line 112 "csv_reader.rl"
{
ts = p;
te = p;
}
#line 121 "csv_reader.rl"
{
fields.push_back(std::string(ts, te-ts));
current_field++;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st1;
tr9:
#line 121 "csv_reader.rl"
{
fields.push_back(std::string(ts, te-ts));
current_field++;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st1;
st1:
if ( ++p == pe )
goto _test_eof1;
case 1:
#line 196 "csv_reader.h"
switch( (*p) ) {
case 10: goto tr2;
case 32: goto tr1;
case 34: goto tr3;
case 44: goto tr4;
}
if ( 9 <= (*p) && (*p) <= 13 )
goto tr1;
goto tr0;
tr0:
#line 112 "csv_reader.rl"
{
ts = p;
te = p;
}
#line 117 "csv_reader.rl"
{
te++;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st2;
tr5:
#line 117 "csv_reader.rl"
{
te++;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st2;
st2:
if ( ++p == pe )
goto _test_eof2;
case 2:
#line 235 "csv_reader.h"
switch( (*p) ) {
case 10: goto tr7;
case 32: goto tr6;
case 34: goto st0;
case 44: goto tr9;
}
if ( 9 <= (*p) && (*p) <= 13 )
goto tr6;
goto tr5;
tr10:
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st3;
tr6:
#line 121 "csv_reader.rl"
{
fields.push_back(std::string(ts, te-ts));
current_field++;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st3;
st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
#line 266 "csv_reader.h"
switch( (*p) ) {
case 10: goto tr11;
case 32: goto tr10;
case 44: goto tr1;
}
if ( 9 <= (*p) && (*p) <= 13 )
goto tr10;
goto st0;
st0:
cs = 0;
goto _out;
tr2:
#line 112 "csv_reader.rl"
{
ts = p;
te = p;
}
#line 121 "csv_reader.rl"
{
fields.push_back(std::string(ts, te-ts));
current_field++;
}
#line 134 "csv_reader.rl"
{
finished = true;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st7;
tr7:
#line 121 "csv_reader.rl"
{
fields.push_back(std::string(ts, te-ts));
current_field++;
}
#line 134 "csv_reader.rl"
{
finished = true;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st7;
tr11:
#line 134 "csv_reader.rl"
{
finished = true;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st7;
st7:
if ( ++p == pe )
goto _test_eof7;
case 7:
#line 327 "csv_reader.h"
goto st0;
tr3:
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
#line 339 "csv_reader.h"
if ( (*p) == 34 )
goto tr13;
goto tr12;
tr12:
#line 112 "csv_reader.rl"
{
ts = p;
te = p;
}
#line 117 "csv_reader.rl"
{
te++;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st5;
tr14:
#line 117 "csv_reader.rl"
{
te++;
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st5;
st5:
if ( ++p == pe )
goto _test_eof5;
case 5:
#line 372 "csv_reader.h"
if ( (*p) == 34 )
goto tr15;
goto tr14;
tr13:
#line 112 "csv_reader.rl"
{
ts = p;
te = p;
}
#line 117 "csv_reader.rl"
{
te++;
}
#line 126 "csv_reader.rl"
{
// some little bit of lookahead
if(*(p + 1) != '"') {
fields.push_back(std::string(ts, te-ts));
current_field++;
}
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st6;
tr15:
#line 117 "csv_reader.rl"
{
te++;
}
#line 126 "csv_reader.rl"
{
// some little bit of lookahead
if(*(p + 1) != '"') {
fields.push_back(std::string(ts, te-ts));
current_field++;
}
}
#line 138 "csv_reader.rl"
{
chars_read++;
}
goto st6;
st6:
if ( ++p == pe )
goto _test_eof6;
case 6:
#line 421 "csv_reader.h"
switch( (*p) ) {
case 10: goto tr11;
case 32: goto tr10;
case 34: goto tr14;
case 44: goto tr1;
}
if ( 9 <= (*p) && (*p) <= 13 )
goto tr10;
goto st0;
}
_test_eof1: cs = 1; goto _test_eof;
_test_eof2: cs = 2; goto _test_eof;
_test_eof3: cs = 3; goto _test_eof;
_test_eof7: cs = 7; goto _test_eof;
_test_eof4: cs = 4; goto _test_eof;
_test_eof5: cs = 5; goto _test_eof;
_test_eof6: cs = 6; goto _test_eof;
_test_eof: {}
_out: {}
}
#line 174 "csv_reader.rl"
if(!finished) {
throw error::record_parsing(current_field);
} else {
this->_cleanFields(fields);
}
return chars_read;
}
private:
void _cleanFields(std::vector<std::string> &fields) const {
for(auto &field : fields) {
// remove end " if exists
if(field.back() == '"') {
field.erase(field.end() - 1);
// replace "" for " only in quoted fields
// TODO: there is maybe a better way to do that...
size_t index = 0;
while(true) {
index = field.find("\"\"", index);
if(index == std::string::npos) break;
field.replace(index, 2, "\"");
index += 2;
}
}
}
}
};
//
// FILE PARSER
//
namespace input {
class Reader {
public:
Reader() {}
virtual ~Reader() {}
virtual size_t read(char *buffer, size_t size) = 0;
virtual const std::string name() const = 0;
virtual bool valid() const = 0;
virtual bool end() const = 0;
};
class StreamReader : public Reader {
public:
StreamReader(std::istream &stream): stream(stream) {}
virtual ~StreamReader() {}
virtual size_t read(char *buffer, size_t size) {
this->stream.read(buffer, size);
return this->stream.gcount();
}
virtual bool valid() const {
return this->stream.good();
}
virtual bool end() const {
return this->stream.eof();
}
virtual const std::string name() const {
return "stream";
}
private:
std::istream &stream;
};
}
template
<
size_t BLOCK_SIZE = 1 << 24
>
class Reader {
public:
Reader(std::unique_ptr<input::Reader> reader):
reader(std::move(reader))
{
this->_initializeBuffer();
}
void readLine(std::vector<std::string> &row) {
if(!this->valid()) return;
// move buffer and read more stuff
this->_updateBuffer();
this->current_record++;
// parse line
try {
size_t read_chars = this->record_parser.parse(this->buffer.get() + this->current_pos, \
this->buffer.get() + this->buffer_size, row);
this->current_pos += read_chars;
} catch(const error::record_parsing& e) {
this->invalid_due_to_parse_error = true;
throw error::file_parsing(this->reader->name(), this->current_record, e.field_num);
}
}
bool valid() {
return !this->invalid_due_to_parse_error && (this->reader->valid() || (this->current_pos < this->buffer_size));
}
bool end() {
return this->reader->end() && (this->current_pos >= this->buffer_size);
}
private:
void _initializeBuffer() {
this->buffer = std::unique_ptr<char[]>(new char[3*BLOCK_SIZE]);
this->current_pos = 0;
this->buffer_size = this->reader->read(this->buffer.get(), 2*BLOCK_SIZE);
this->_fixLastLine();
}
void _updateBuffer() {
if(this->current_pos >= BLOCK_SIZE) {
// move buffer to the beginning
std::memcpy(this->buffer.get(), this->buffer.get() + BLOCK_SIZE, BLOCK_SIZE);
this->current_pos -= BLOCK_SIZE;
this->buffer_size -= BLOCK_SIZE;
// try to read another block
if(this->reader->valid()) {
this->buffer_size += this->reader->read(this->buffer.get() + BLOCK_SIZE, BLOCK_SIZE);
}
this->_fixLastLine();
}
}
void _fixLastLine() {
// force a \n in the last position, if it's not there...
if(!this->reader->valid() && this->buffer_size) {
// remove end character... not needed...
this->buffer_size--;
// add \n
if(this->buffer_size < 2*BLOCK_SIZE && \
*(this->buffer.get() + this->buffer_size - 1) != '\n') {
*(this->buffer.get() + this->buffer_size) = '\n';
this->buffer_size++;
}
}
}
std::unique_ptr<input::Reader> reader;
RecordParser record_parser;
// buffer and buffer pointers
std::unique_ptr<char[]> buffer;
size_t current_pos;
size_t buffer_size;
size_t current_record = 0;
bool invalid_due_to_parse_error = false;
};
}
#endif