Browse Source

Fix easycsv_read_value with more tests

Signed-off-by: Pradana AUMARS <paumars@courrier.dev>
redesign
Pradana AUMARS 10 months ago
parent
commit
3304163ab0
  1. 21
      src/easycsv.c
  2. 18
      tests/check_easycsv.c

21
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;
}

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

Loading…
Cancel
Save