Fix easycsv_read_value with more tests

Signed-off-by: Pradana AUMARS <paumars@courrier.dev>
This commit is contained in:
Pradana AUMARS 2021-07-16 20:57:03 +02:00
parent 2fa4ca384c
commit 3304163ab0
2 changed files with 18 additions and 21 deletions

View File

@ -129,27 +129,24 @@ easycsv_read_value(const easycsv *csv,
} }
fscanf(csv->file, "%s\n", str_row); fscanf(csv->file, "%s\n", str_row);
/* Get first occurance of comma in str, /* Get first occurance of comma in str, the first value is ommited but not the comma */
the first value is ommited but not the comma */
pch = str_row; pch = str_row;
/* Repeat until desired col is found */ /* Repeat until desired col is found */
for (unsigned int i = 1; i < col; i++) { for (unsigned int i = 1; i < col; i++) {
pch = strpbrk(pch + 1, ","); pch = strpbrk(pch + 1, ",");
} }
/* Get span from start of string to first occurence /* Get span from start of string to first occurence of comma */
of comma */ if (col > 1) pch++;
st = strcspn(pch, ","); st = strcspn(pch, ",");
val = malloc(BUFSIZ); val = malloc(BUFSIZ);
// If 0, no string exists! // If 0, no string exists!
if (st == 0) { if (st > 0) {
val = "";
}
else {
strncpy(val, pch, st + 1); strncpy(val, pch, st + 1);
val[st] = '\0';
} }
val[st] = '\0';
return val; return val;
} }

View File

@ -80,17 +80,17 @@ START_TEST(test_easycsv_read_value)
ck_assert_str_eq(str, "FILEPATHA1"); ck_assert_str_eq(str, "FILEPATHA1");
free(str); free(str);
/* str = easycsv_read_value(csv, 1, 2); */ str = easycsv_read_value(csv, 1, 2);
/* ck_assert_str_eq(str, "FILEPATHB1"); */ ck_assert_str_eq(str, "FILEPATHB1");
/* free(str); */ free(str);
/* str = easycsv_read_value(csv, 1, 3); */ str = easycsv_read_value(csv, 2, 1);
/* ck_assert_str_eq(str, NULL); */ ck_assert_str_eq(str, "FILEPATHA2");
/* free(str); */ free(str);
/* str = easycsv_read_value(csv, 2, 1); */ str = easycsv_read_value(csv, 1, 3);
/* ck_assert_str_eq(str, "FILEPATHA2"); */ ck_assert_str_eq(str, "");
/* free(str); */ free(str);
easycsv_free(csv); easycsv_free(csv);
} }