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 ***/ /*** Acces, read ***/
/* char* */ char*
/* easycsv_readvalue(const easycsv *csv, */ easycsv_read_value(const easycsv *csv,
/* const unsigned int col, */ unsigned int col,
/* const unsigned int row) */ unsigned int row)
/* { */ {
/* /\* ARGS CHECK *\/ */ char str_row[BUFSIZ];
/* if (row == 0) { */ size_t st = 0;
/* _easycsv_printerror(csv->csv, EASYCSV_ZEROROW); */ char *pch, *pch_end, *val;
/* _easycsv_printerror(csv->csv, EASYCSV_READVALUEFAIL); */ if (row == 0) {
/* return NULL; */ easycsv_error(EASYCSV_ZEROROW, NULL);
/* } */ return NULL;
}
/* if (col == 0) { */ if (col == 0) {
/* _easycsv_printerror(csv->csv, EASYCSV_ZEROCOL); */ easycsv_error(EASYCSV_ZEROCOL, NULL);
/* _easycsv_printerror(csv->csv, EASYCSV_READVALUEFAIL); */ return NULL;
/* 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; */
/* } */
/* char *val = _easycsv_getvalueinrow(csv->csv, str, col); */ /* Set file pointer to start */
/* if (val == NULL) { */ easycsv_rewind(csv);
/* _easycsv_printerror(csv->csv, EASYCSV_READVALUEFAIL); */
/* return NULL; */
/* } */
/* 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* */ /* char* */
/* easycsv_readcolumnvalue(const easycsv *csv, */ /* easycsv_readcolumnvalue(const easycsv *csv, */

View File

@ -67,7 +67,31 @@ START_TEST(test_easycsv_print_columns)
START_TEST(test_easycsv_print_rows) START_TEST(test_easycsv_print_rows)
{ {
easycsv *csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R); 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); easycsv_free(csv);
} }
@ -87,14 +111,15 @@ easycsv_access_suite(void)
Suite *s; Suite *s;
TCase *tc_find, *tc_read, *tc_print; TCase *tc_find, *tc_read, *tc_print;
s = suite_create ("access"); 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); */ /* tcase_add_test(tc_read, test_easycsv_readcolumnvalue); */
tc_print = tcase_create ("print"); tc_print = tcase_create ("print");
tcase_add_test(tc_print, test_easycsv_print_columns); tcase_add_test(tc_print, test_easycsv_print_columns);
tcase_add_test(tc_print, test_easycsv_print_rows); 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); suite_add_tcase(s, tc_print);
return s; return s;