@@ -86,6 +86,9 @@ CSV_Parser::CSV_Parser(const char * s, const char * fmt_, bool has_header_, char
86
86
keys = (char **)calloc (cols_count, sizeof (char *)); // calloc fills memory with 0's so then I can simply use "if(keys[i]) { do something with key[i] }"
87
87
values = (void **)calloc (cols_count, sizeof (void *));
88
88
89
+ for (int col = 0 ; col < cols_count; col++)
90
+ values[col] = malloc (getTypeSize (fmt[col]));
91
+
89
92
/* mem.check("constructor");
90
93
mem.check("after calloc 1, should be = " + String(cols_count * sizeof(char*)));
91
94
mem.check("after calloc 2, should be = " + String(cols_count * sizeof(void*)));*/
@@ -123,9 +126,11 @@ bool CSV_Parser::parseRow() {
123
126
// (by "original way of parsing" I mean: by using "cp <<" operator or by supplying whole csv at once)
124
127
// parseRow() requires feedRowParser() and rowParserFinished() to be defined by the user
125
128
// It then allows to read the first row in the same way as default parsing way, like:
126
- // while (cp.parseRow()) {
127
- // char *str = ((char**)cp["my_strings"])[0];
128
- // }
129
+ //
130
+ // char **strings = (char**)cp[0]; // string based indexing can't be used here because header was not supplied yet
131
+ // while (cp.parseRow()) {
132
+ // char *str = strings[0];
133
+ // }
129
134
130
135
if (rowParserFinished ())
131
136
return false ;
@@ -441,7 +446,8 @@ void CSV_Parser::supplyChunk(const char *s) {
441
446
keys[current_col] = strdup_trimmed (val);
442
447
} else {
443
448
// mem.check("values[" + String(current_col) + "]");
444
- values[current_col] = (char *)realloc (values[current_col], (rows_count+1 ) * getTypeSize (fmt[current_col]));
449
+ if (rows_count > 0 )
450
+ values[current_col] = (char *)realloc (values[current_col], (rows_count+1 ) * getTypeSize (fmt[current_col]));
445
451
saveNewValue (val, fmt[current_col], rows_count, current_col, is_fmt_unsigned[current_col]);
446
452
}
447
453
}
@@ -500,7 +506,8 @@ void CSV_Parser::parseLeftover() {
500
506
int chars_occupied = 0 ;
501
507
if (char * val = parseStringValue (leftover, &chars_occupied)) {
502
508
if (fmt[current_col] != ' -' ) {
503
- values[current_col] = realloc (values[current_col], (rows_count+1 ) * getTypeSize (fmt[current_col]));
509
+ if (rows_count > 0 )
510
+ values[current_col] = realloc (values[current_col], (rows_count+1 ) * getTypeSize (fmt[current_col]));
504
511
saveNewValue (val, fmt[current_col], rows_count, current_col, is_fmt_unsigned[current_col]);
505
512
}
506
513
if (++current_col == cols_count) {
0 commit comments