diff --git a/src/easycsv.c b/src/easycsv.c index 50cfb12..6aadfc2 100644 --- a/src/easycsv.c +++ b/src/easycsv.c @@ -129,27 +129,24 @@ easycsv_read_value(const easycsv *csv, } fscanf(csv->file, "%s\n", str_row); - /* Get first occurance of comma in str, - the first value is ommited but not the comma */ + /* Get first occurance of comma in str, the first value is ommited but not the comma */ pch = str_row; /* Repeat until desired col is found */ - for (unsigned int i = 1; i < col; i++) { - pch = strpbrk(pch + 1, ","); - } + for (unsigned int i = 1; i < col; i++) { + pch = strpbrk(pch + 1, ","); + } - /* Get span from start of string to first occurence - of comma */ + /* 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) { - val = ""; - } - else { + if (st > 0) { strncpy(val, pch, st + 1); - val[st] = '\0'; } + val[st] = '\0'; + return val; } diff --git a/tests/check_easycsv.c b/tests/check_easycsv.c index 0fd560e..252ece1 100644 --- a/tests/check_easycsv.c +++ b/tests/check_easycsv.c @@ -80,17 +80,17 @@ START_TEST(test_easycsv_read_value) ck_assert_str_eq(str, "FILEPATHA1"); free(str); - /* str = easycsv_read_value(csv, 1, 2); */ - /* ck_assert_str_eq(str, "FILEPATHB1"); */ - /* free(str); */ + str = easycsv_read_value(csv, 1, 2); + ck_assert_str_eq(str, "FILEPATHB1"); + free(str); - /* str = easycsv_read_value(csv, 1, 3); */ - /* ck_assert_str_eq(str, NULL); */ - /* free(str); */ + str = easycsv_read_value(csv, 2, 1); + ck_assert_str_eq(str, "FILEPATHA2"); + free(str); - /* str = easycsv_read_value(csv, 2, 1); */ - /* ck_assert_str_eq(str, "FILEPATHA2"); */ - /* free(str); */ + str = easycsv_read_value(csv, 1, 3); + ck_assert_str_eq(str, ""); + free(str); easycsv_free(csv); }