@@ -86,6 +86,9 @@ CSV_Parser::CSV_Parser(const char * s, const char * fmt_, bool has_header_, char
8686 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] }"
8787 values = (void **)calloc (cols_count, sizeof (void *));
8888
89+ for (int col = 0 ; col < cols_count; col++)
90+ values[col] = malloc (getTypeSize (fmt[col]));
91+
8992 /* mem.check("constructor");
9093 mem.check("after calloc 1, should be = " + String(cols_count * sizeof(char*)));
9194 mem.check("after calloc 2, should be = " + String(cols_count * sizeof(void*)));*/
@@ -123,9 +126,11 @@ bool CSV_Parser::parseRow() {
123126 // (by "original way of parsing" I mean: by using "cp <<" operator or by supplying whole csv at once)
124127 // parseRow() requires feedRowParser() and rowParserFinished() to be defined by the user
125128 // 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+ // }
129134
130135 if (rowParserFinished ())
131136 return false ;
@@ -441,7 +446,8 @@ void CSV_Parser::supplyChunk(const char *s) {
441446 keys[current_col] = strdup_trimmed (val);
442447 } else {
443448 // 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]));
445451 saveNewValue (val, fmt[current_col], rows_count, current_col, is_fmt_unsigned[current_col]);
446452 }
447453 }
@@ -500,7 +506,8 @@ void CSV_Parser::parseLeftover() {
500506 int chars_occupied = 0 ;
501507 if (char * val = parseStringValue (leftover, &chars_occupied)) {
502508 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]));
504511 saveNewValue (val, fmt[current_col], rows_count, current_col, is_fmt_unsigned[current_col]);
505512 }
506513 if (++current_col == cols_count) {
0 commit comments