Browse Source

Add easycsv_find_value

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

29
src/easycsv.c

@ -101,6 +101,35 @@ easycsv_free(easycsv *csv)
/*** Acces, find ***/
int
easycsv_find_value(const easycsv* csv,
const char* value,
unsigned int* col,
unsigned int* row)
{
char *str;
const unsigned int cols = easycsv_print_columns(csv);
const unsigned int rows = easycsv_print_rows(csv);
/* Set file pointer to start */
easycsv_rewind(csv);
for (unsigned int row_l = 1; row_l < rows; row_l++) {
for (unsigned int col_l = 1; col_l < cols; col_l++) {
str = easycsv_read_value(csv, col_l, row_l);
if (strcmp(str, value) == 0) {
*col = col_l;
*row = row_l;
free(str);
return 0;
}
free(str);
}
}
easycsv_error(EASYCSV_FINDVALUEFAIL, value);
return -1;
}
/*** Acces, read ***/
char*

28
tests/check_easycsv.c

@ -95,6 +95,30 @@ START_TEST(test_easycsv_read_value)
easycsv_free(csv);
}
START_TEST(test_easycsv_find_value)
{
easycsv *csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R);
unsigned int col = 0, row = 0;
easycsv_find_value(csv, "FILEPATHA1", &col, &row);
ck_assert_int_eq(col, 1);
ck_assert_int_eq(row, 1);
easycsv_find_value(csv, "FILEPATHB1", &col, &row);
ck_assert_int_eq(col, 2);
ck_assert_int_eq(row, 1);
easycsv_find_value(csv, "FILEPATHA2", &col, &row);
ck_assert_int_eq(col, 1);
ck_assert_int_eq(row, 2);
easycsv_find_value(csv, "", &col, &row);
ck_assert_int_eq(col, 1);
ck_assert_int_eq(row, 3);
easycsv_free(csv);
}
/* START_TEST(test_easycsv_readcolumnvalue) */
/* { */
/* easycsv *csv = easycsv_init(SAMPLE1_PATH, EASYCSV_R); */
@ -111,6 +135,9 @@ easycsv_access_suite(void)
Suite *s;
TCase *tc_find, *tc_read, *tc_print;
s = suite_create ("access");
tc_find = tcase_create ("find");
tcase_add_test(tc_find, test_easycsv_find_value);
tc_read = tcase_create ("read");
tcase_add_test(tc_read, test_easycsv_read_value);
/* tcase_add_test(tc_read, test_easycsv_readcolumnvalue); */
@ -119,6 +146,7 @@ easycsv_access_suite(void)
tcase_add_test(tc_print, test_easycsv_print_columns);
tcase_add_test(tc_print, test_easycsv_print_rows);
suite_add_tcase(s, tc_find);
suite_add_tcase(s, tc_read);
suite_add_tcase(s, tc_print);

Loading…
Cancel
Save