From b0e3062d59badff4a9b105671a2bde94ebea4806 Mon Sep 17 00:00:00 2001 From: Pradana AUMARS Date: Sat, 17 Jul 2021 16:46:19 +0200 Subject: [PATCH] Add easycsv_get_row and easycsv_set_charp_to_value Signed-off-by: Pradana AUMARS --- src/easycsv_p.c | 78 +++++++++++++++---------------------------------- src/easycsv_p.h | 36 ++++++++++++++--------- 2 files changed, 46 insertions(+), 68 deletions(-) diff --git a/src/easycsv_p.c b/src/easycsv_p.c index 39bc7af..7c42ea9 100644 --- a/src/easycsv_p.c +++ b/src/easycsv_p.c @@ -88,46 +88,21 @@ easycsv_columns(const easycsv *csv) return col; } -/* char* */ -/* _easycsv_getrow(const _easycsv *csv, */ -/* const unsigned int row) */ -/* { */ -/* /\* ARGS CHECK *\/ */ -/* if (csv == NULL) { */ -/* _easycsv_printerror(csv, EASYCSV_NULLCSV); */ -/* return NULL; */ -/* } */ +char* +easycsv_get_row(const easycsv *csv, + unsigned int row) +{ + char *str = malloc(BUFSIZ); + easycsv_rewind(csv); -/* if (row > csv->rows) { */ -/* _easycsv_printerror(csv, EASYCSV_OVERMAXROW); */ -/* return NULL; */ -/* } */ + for (int i = 1; i < row; i++) { + fscanf(csv->file, "%*[^\n]\n", NULL); + } -/* if (row == 0) { */ -/* _easycsv_printerror(csv, EASYCSV_ZEROROW); */ -/* return NULL; */ -/* } */ -/* /\* END ARGS CHECK *\/ */ - -/* /\* Allocate memory *\/ */ -/* char *str = malloc(BUFSIZ); */ - -/* /\* Set file pointer to start *\/ */ -/* rewind(csv->file); */ + fscanf(csv->file, "%s\n", str); -/* for (int i = 1; i < row; i++) */ -/* { */ -/* /\* skip until row is reached *\/ */ -/* fscanf(csv->file, "%*[^\n]\n", NULL); */ -/* } */ - -/* /\* Grab the row and store it in str *\/ */ -/* fscanf(csv->file, "%s\n", str); */ - -/* // printf("row: %s\n", str); */ - -/* return str; */ -/* } */ + return str; +} int easycsv_rewind(const easycsv *csv) @@ -247,24 +222,19 @@ int _easycsv_checkifvalue(struct easycsv *csv, const int col, const int row) /* return val; */ /* } */ -/* char* */ -/* _easycsv_setcharptovalue(const _easycsv *_priv, */ -/* const char *rowstr, */ -/* const unsigned int col) */ -/* { */ -/* char *pch = rowstr; */ +char* +easycsv_set_charp_to_value(const char *rowstr, + unsigned int col) +{ + /* Get first occurance of comma in str, the first value is ommited but not the comma */ + char *pch = (char*) rowstr; + /* 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 = strchr(rowstr, ','); */ -/* if (pch == NULL) { */ -/* _easycsv_printerror(_priv, EASYCSV_NULLPTR); */ -/* return NULL; */ -/* } */ -/* pch++; */ -/* } */ - -/* return pch; */ -/* } */ + return pch; +} /* int */ /* _easycsv_checkcsvandstring_one(const _easycsv *csv, */ diff --git a/src/easycsv_p.h b/src/easycsv_p.h index 8bed55c..af40b1a 100644 --- a/src/easycsv_p.h +++ b/src/easycsv_p.h @@ -5,15 +5,17 @@ /*** Accès ***/ -/* /\** */ -/* * Returns string of a specific row, including '\n' character */ -/* * @param[in] const pointer to _easycsv structure */ -/* * @param[in] row number */ -/* * @return string of row, NULL if error or none */ -/* *\/ */ -/* char* */ -/* _easycsv_getrow(const _easycsv*, */ -/* unsigned int); */ +/** + * Returns string of a specific row, including '\n' character + * This function does not check parameters! + * This function does not produce errors! + * @param[in] const pointer to easycsv structure + * @param[in] row number + * @return string of row (heap-allocated) + */ +char* +easycsv_get_row(const easycsv*, + unsigned int); /* /\** */ /* * Returns column number of a named column *\/ */ @@ -39,11 +41,17 @@ /* const char*, */ /* const unsigned int); */ -/* /\* Returns char pointer to start of value in rowstr *\/ */ -/* char* */ -/* _easycsv_setcharptovalue(const _easycsv*, */ -/* const char*, */ -/* const unsigned int); */ +/** + * Returns char pointer towards the n-th occurence of ',' in a given string + * This function does not check parameters! + * This function does not produce errors! + * @param[in] string of row (string with multiple occurences of ',') + * @param[in] n-th occurence to place the char pointer + * @return char pointer towards the n-th occurence of ',' in a given string +*/ +char* +easycsv_set_charp_to_value(const char*, + unsigned int); /* /\* Insert value in row in specific column *\/ */ /* char* */