-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_parser.cpp
452 lines (311 loc) · 14.5 KB
/
csv_parser.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
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
/* INCLUDING HEADER FILES */
#include "csv_parser.h"
/* BEGIN DEFINITION FOR PUBLIC METHODS */
bool csv_parser::init(FILE * input_file_pointer) {
input_fp = input_file_pointer;
if (input_fp == NULL) {
fprintf(stderr, "Fatal error : unable to open input file from file pointer\n");
return false;
}
/* Resetting the internal pointer to the beginning of the stream */
rewind(input_fp);
more_rows = true;
_skip_lines();
return true;
}
bool csv_parser::init(const char * input_file) {
const size_t filename_length = strlen(input_file);
if (!filename_length) {
fprintf(stderr, "Fatal error : invalid input file %s\n", input_file);
return false;
}
input_filename = (char *) malloc(filename_length + 1);
if (input_filename == NULL) {
fprintf(stderr, "Fatal error : unable to allocate memory for file name buffer %s\n", input_file);
return false;
}
memset(input_filename, 0, filename_length + 1);
strcpy(input_filename, input_file);
input_fp = fopen(input_file, "r");
if (input_fp == NULL) {
fprintf(stderr, "Fatal error : unable to open input file %s\n", input_file);
CSV_PARSER_FREE_BUFFER_PTR(input_filename);
return false;
}
more_rows = true;
_skip_lines();
return true;
}
void csv_parser::set_enclosed_char(char fields_enclosed_by, enclosure_type_t enclosure_mode) {
if (fields_enclosed_by != 0) {
enclosed_char = fields_enclosed_by;
enclosed_length = 1U;
enclosure_type = enclosure_mode;
}
}
void csv_parser::set_field_term_char(char fields_terminated_by) {
if (fields_terminated_by != 0) {
field_term_char = fields_terminated_by;
field_term_length = 1U;
}
}
void csv_parser::set_line_term_char(char lines_terminated_by) {
if (lines_terminated_by != 0) {
line_term_char = lines_terminated_by;
line_term_length = 1U;
}
}
csv_row csv_parser::get_row(void) {
csv_row current_row;
/* This will store the length of the buffer */
unsigned int line_length = 0U;
/* Character array buffer for the current record */
char * line = NULL;
/* Grab one record */
_read_single_line(&line, &line_length);
/* Select the most suitable field extractor based on the enclosure length */
switch(enclosure_type) {
case ENCLOSURE_NONE : /* The fields are not enclosed by any character */
_get_fields_without_enclosure(¤t_row, line, &line_length);
break;
case ENCLOSURE_REQUIRED : /* The fields are enclosed by a character */
_get_fields_with_enclosure(¤t_row, line, &line_length);
break;
case ENCLOSURE_OPTIONAL : /* The fields may or may not be enclosed */
_get_fields_with_optional_enclosure(¤t_row, line, &line_length);
break;
default :
_get_fields_with_optional_enclosure(¤t_row, line, &line_length);
break;
}
/* Deallocate the current buffer */
CSV_PARSER_FREE_BUFFER_PTR(line);
/* Keeps track of how many times this has method has been called */
record_count++;
return current_row;
}
/* BEGIN DEFINITION FOR PROTECTED METHODS */
/* BEGIN DEFINITION FOR PRIVATE METHODS */
void csv_parser::_skip_lines(void) {
/* Just in case the user accidentally sets ignore_num_lines to a negative number */
unsigned int number_of_lines_to_ignore = abs((int) ignore_num_lines);
while(has_more_rows() && number_of_lines_to_ignore) {
const csv_row row = get_row();
number_of_lines_to_ignore--;
}
record_count = 0U;
}
void csv_parser::_get_fields_without_enclosure(csv_row_ptr row, const char * line, const unsigned int * line_length) {
char * field = NULL;
if (*line_length > 0) {
field = (char *) malloc(*line_length);
memset(field, 0, *line_length);
register unsigned int field_start = 0U;
register unsigned int field_end = 0U;
register unsigned int char_pos = 0U;
while(char_pos < *line_length) {
char curr_char = line[char_pos];
if (curr_char == field_term_char) {
field_end = char_pos;
const char * field_starts_at = line + field_start;
/* Field width must exclude field delimiter characters */
const unsigned int field_width = field_end - field_start;
/* Copy exactly field_width bytes from field_starts_at to field */
memcpy(field, field_starts_at, field_width);
/* This must be a null-terminated character array */
field[field_width] = 0x00;
string field_string_obj = field;
row->push_back(field_string_obj);
/* This is the starting point of the next field */
field_start = char_pos + 1;
} else if (curr_char == line_term_char) {
field_end = char_pos;
const char * field_starts_at = line + field_start;
/* Field width must exclude line terminating characters */
const unsigned int field_width = field_end - field_start;
/* Copy exactly field_width bytes from field_starts_at to field */
memcpy(field, field_starts_at, field_width);
/* This must be a null-terminated character array */
field[field_width] = 0x00;
string field_string_obj = field;
row->push_back(field_string_obj);
}
/* Move to the next character in the current line */
char_pos++;
}
/* Deallocate memory for field buffer */
CSV_PARSER_FREE_BUFFER_PTR(field);
}
}
void csv_parser::_get_fields_with_enclosure(csv_row_ptr row, const char * line, const unsigned int * line_length) {
char * field = NULL;
if (*line_length > 0) {
field = (char *) malloc(*line_length);
memset(field, 0, *line_length);
register unsigned int current_state = 0U;
register unsigned int field_start = 0U;
register unsigned int field_end = 0U;
register unsigned int char_pos = 0U;
while(char_pos < *line_length) {
char curr_char = line[char_pos];
if (curr_char == enclosed_char) {
current_state++;
/* Lets find out if the enclosure character encountered is
* a 'real' enclosure character or if it is an embedded character that
* has been escaped within the field.
*/
register char previous_char = 0x00;
if (char_pos > 0U) {
/* The escaped char will have to be the 2rd or later character. */
previous_char = line[char_pos - 1];
if (previous_char == escaped_char) {
--current_state;
}
}
if (current_state == 1U && previous_char != escaped_char) {
/* This marks the beginning of the column */
field_start = char_pos;
} else if (current_state == 2U) {
/* We have found the end of the current field */
field_end = char_pos;
/* We do not need the enclosure characters */
const char * field_starts_at = line + field_start + 1U;
/* Field width must exclude beginning and ending enclosure characters */
const unsigned int field_width = field_end - field_start - 1U;
/* Copy exactly field_width bytes from field_starts_at to field */
memcpy(field, field_starts_at, field_width);
/* This must be a null-terminated character array */
field[field_width] = 0x00;
string field_string_obj = field;
row->push_back(field_string_obj);
/* Reset the state to zero value for the next field */
current_state = 0U;
}
}
/* Move to the next character in the current line */
char_pos++;
}
/* If no enclosures were found in this line, the entire line becomes the only field. */
if (0 == row->size()) {
string entire_line = line;
row->push_back(entire_line);
} else if (current_state == 1U) {
/* The beginning enclosure character was found but
* we could not locate the closing enclosure in the current line
* So we need to copy the remainder of the line into the last field.
*/
/* We do not need the starting enclosure character */
const char * field_starts_at = line + field_start + 1U;
/* Field width must exclude beginning characters */
const unsigned int field_width = *line_length - field_start - 1U;
/* Copy exactly field_width bytes from field_starts_at to field */
memcpy(field, field_starts_at, field_width);
/* This must be a null-terminated character array */
field[field_width] = 0x00;
string field_string_obj = field;
row->push_back(field_string_obj);
}
/* Release the buffer for the field */
CSV_PARSER_FREE_BUFFER_PTR(field);
}
}
void csv_parser::_get_fields_with_optional_enclosure(csv_row_ptr row, const char * line, const unsigned int * line_length) {
char * field = NULL;
/*
* How to extract the fields, when the enclosure char is optional.
*
* This is very similar to parsing the document without enclosure but with the following conditions.
*
* If the beginning char is an enclosure character, adjust the starting position of the string by + 1.
* If the ending char is an enclosure character, adjust the ending position by -1
*/
if (*line_length > 0) {
field = (char *) malloc(*line_length);
memset(field, 0, *line_length);
register unsigned int field_start = 0U;
register unsigned int field_end = 0U;
register unsigned int char_pos = 0U;
while(char_pos < *line_length) {
char curr_char = line[char_pos];
if (curr_char == field_term_char) {
field_end = char_pos;
const char * field_starts_at = line + field_start;
/* Field width must exclude field delimiter characters */
unsigned int field_width = field_end - field_start;
const char line_first_char = field_starts_at[0];
const char line_final_char = field_starts_at[field_width - 1];
/* If the enclosure char is found at either ends of the string */
unsigned int first_adjustment = (line_first_char == enclosed_char) ? 1U : 0U;
unsigned int final_adjustment = (line_final_char == enclosed_char) ? 2U : 0U;
/* We do not want to have any negative or zero field widths */
field_width = (field_width > 2U) ? (field_width - final_adjustment) : field_width;
/* Copy exactly field_width bytes from field_starts_at to field */
memcpy(field, field_starts_at + first_adjustment, field_width);
/* This must be a null-terminated character array */
field[field_width] = 0x00;
string field_string_obj = field;
row->push_back(field_string_obj);
/* This is the starting point of the next field */
field_start = char_pos + 1;
} else if (curr_char == line_term_char) {
field_end = char_pos;
const char * field_starts_at = line + field_start;
/* Field width must exclude line terminating characters */
unsigned int field_width = field_end - field_start;
const char line_first_char = field_starts_at[0];
const char line_final_char = field_starts_at[field_width - 1];
/* If the enclosure char is found at either ends of the string */
unsigned int first_adjustment = (line_first_char == enclosed_char) ? 1U : 0U;
unsigned int final_adjustment = (line_final_char == enclosed_char) ? 2U : 0U;
/* We do not want to have any negative or zero field widths */
field_width = (field_width > 2U) ? (field_width - final_adjustment) : field_width;
/* Copy exactly field_width bytes from field_starts_at to field */
memcpy(field, field_starts_at + first_adjustment, field_width);
/* This must be a null-terminated character array */
field[field_width] = 0x00;
string field_string_obj = field;
row->push_back(field_string_obj);
}
/* Move to the next character in the current line */
char_pos++;
}
/* Deallocate memory for field buffer */
CSV_PARSER_FREE_BUFFER_PTR(field);
}
}
void csv_parser::_read_single_line(char ** buffer, unsigned int * buffer_len) {
long int original_pos = ftell(input_fp);
long int current_pos = original_pos;
register int current_char = 0;
/* Checking one character at a time until the end of a line is found */
while(true) {
current_char = fgetc(input_fp);
if (current_char == EOF) {
/* We have reached the end of the file */
more_rows = false;
break;
} else if (current_char == line_term_char) {
/* We have reached the end of the row */
current_pos++;
break;
} else {
current_pos++;
}
}
/* Let's try to peek one character ahead to see if we are at the end of the file */
if (more_rows) {
current_char = fgetc(input_fp);
more_rows = (current_char == EOF) ? false : true;
}
/* Find out how long this row is */
const size_t length_of_row = current_pos - original_pos;
if (length_of_row > 0) {
*buffer_len = length_of_row * sizeof(char) + 1;
*buffer = (char *) realloc(*buffer, *buffer_len);
memset(*buffer, 0, *buffer_len);
/* Reset the internal pointer to the original position */
fseek(input_fp, original_pos, SEEK_SET);
/* Copy the contents of the line into the buffer */
fread(*buffer, 1, length_of_row, input_fp);
}
}