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);
/* 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;
}

View File

@ -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);
}