Fix easycsv_read_value

Signed-off-by: Pradana AUMARS <paumars@courrier.dev>
This commit is contained in:
Pradana AUMARS 2021-07-16 20:25:30 +02:00
parent bb1e4aef4f
commit 2fa4ca384c
2 changed files with 74 additions and 34 deletions

View File

@ -103,40 +103,55 @@ easycsv_free(easycsv *csv)
/*** Acces, read ***/
/* char* */
/* easycsv_readvalue(const easycsv *csv, */
/* const unsigned int col, */
/* const unsigned int row) */
/* { */
/* /\* ARGS CHECK *\/ */
/* if (row == 0) { */
/* _easycsv_printerror(csv->csv, EASYCSV_ZEROROW); */
/* _easycsv_printerror(csv->csv, EASYCSV_READVALUEFAIL); */
/* return NULL; */
/* } */
char*
easycsv_read_value(const easycsv *csv,
unsigned int col,
unsigned int row)
{
char str_row[BUFSIZ];
size_t st = 0;
char *pch, *pch_end, *val;
if (row == 0) {
easycsv_error(EASYCSV_ZEROROW, NULL);
return NULL;
}
/* if (col == 0) { */
/* _easycsv_printerror(csv->csv, EASYCSV_ZEROCOL); */
/* _easycsv_printerror(csv->csv, EASYCSV_READVALUEFAIL); */
/* return NULL; */
/* } */
/* /\* END ARGS CHECK *\/ */
/* char *str = _easycsv_getrow(csv->csv, row); */
/* if (str == NULL) { */
/* _easycsv_printerror(csv->csv, EASYCSV_ROWNOTEXIST); */
/* _easycsv_printerror(csv->csv, EASYCSV_READVALUEFAIL); */
/* return NULL; */
/* } */
if (col == 0) {
easycsv_error(EASYCSV_ZEROCOL, NULL);
return NULL;
}
/* char *val = _easycsv_getvalueinrow(csv->csv, str, col); */
/* if (val == NULL) { */
/* _easycsv_printerror(csv->csv, EASYCSV_READVALUEFAIL); */
/* return NULL; */
/* } */
/* Set file pointer to start */
easycsv_rewind(csv);
/* return val; */
/* } */
for (unsigned int i = 1; i < row; i++) {
fscanf(csv->file, "%*[^\n]\n", NULL);
}
fscanf(csv->file, "%s\n", str_row);
/* 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, ",");
}
/* Get span from start of string to first occurence
of comma */
st = strcspn(pch, ",");
val = malloc(BUFSIZ);
// If 0, no string exists!
if (st == 0) {
val = "";
}
else {
strncpy(val, pch, st + 1);
val[st] = '\0';
}
return val;
}
/* char* */
/* easycsv_readcolumnvalue(const easycsv *csv, */

View File

@ -67,7 +67,31 @@ START_TEST(test_easycsv_print_columns)
START_TEST(test_easycsv_print_rows)
{
easycsv *csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R);
ck_assert_int_eq(easycsv_print_rows(csv), 5);
ck_assert_int_eq(easycsv_print_rows(csv), 4);
easycsv_free(csv);
}
START_TEST(test_easycsv_read_value)
{
easycsv *csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R);
char* str;
str = easycsv_read_value(csv, 1, 1);
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, 3); */
/* ck_assert_str_eq(str, NULL); */
/* free(str); */
/* str = easycsv_read_value(csv, 2, 1); */
/* ck_assert_str_eq(str, "FILEPATHA2"); */
/* free(str); */
easycsv_free(csv);
}
@ -87,14 +111,15 @@ easycsv_access_suite(void)
Suite *s;
TCase *tc_find, *tc_read, *tc_print;
s = suite_create ("access");
/* tc_read = tcase_create ("read"); */
tc_read = tcase_create ("read");
tcase_add_test(tc_read, test_easycsv_read_value);
/* tcase_add_test(tc_read, test_easycsv_readcolumnvalue); */
tc_print = tcase_create ("print");
tcase_add_test(tc_print, test_easycsv_print_columns);
tcase_add_test(tc_print, test_easycsv_print_rows);
/* suite_add_tcase(s, tc_read); */
suite_add_tcase(s, tc_read);
suite_add_tcase(s, tc_print);
return s;