diff --git a/src/easycsv.c b/src/easycsv.c index d1fce78..d538ceb 100644 --- a/src/easycsv.c +++ b/src/easycsv.c @@ -111,8 +111,8 @@ easycsv_find_value(const easycsv* csv, const unsigned int cols = easycsv_print_columns(csv); const unsigned int rows = easycsv_print_rows(csv); - for (unsigned int row_l = 1; row_l < rows; row_l++) { - for (unsigned int col_l = 1; col_l < cols; col_l++) { + for (unsigned int row_l = 1; row_l <= rows; row_l++) { + for (unsigned int col_l = 1; col_l <= cols; col_l++) { str = easycsv_read_value(csv, col_l, row_l); if (strcmp(str, value) == 0) { *col = col_l; @@ -127,6 +127,27 @@ easycsv_find_value(const easycsv* csv, return -1; } +int +easycsv_find_num_value(const easycsv* csv, + const char* value) +{ + char *str; + int num = 0; + const unsigned int cols = easycsv_print_columns(csv); + const unsigned int rows = easycsv_print_rows(csv); + + for (unsigned int row_l = 1; row_l <= rows; row_l++) { + for (unsigned int col_l = 1; col_l <= cols; col_l++) { + str = easycsv_read_value(csv, col_l, row_l); + if (strcmp(str, value) == 0) { + num++; + } + free(str); + } + } + return num; +} + /*** Acces, read ***/ char* @@ -162,14 +183,18 @@ easycsv_read_value(const easycsv *csv, pch = strpbrk(pch + 1, ","); } - /* Get span from start of string to first occurence of comma */ - if (col > 1) pch++; - st = strcspn(pch, ","); - val = malloc(BUFSIZ); - // If 0, no string exists! - if (st > 0) { - strncpy(val, pch, st + 1); + if (pch == NULL) { + st = 0; + } + else { + /* Get span from start of string to first occurence of comma */ + if (col > 1) pch++; + st = strcspn(pch, ","); + // If 0, no string exists! + if (st > 0) { + strncpy(val, pch, st + 1); + } } val[st] = '\0';