From 2d6b4c11d4fb9eb3b76061b45c94276cdd430332 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Fri, 27 Sep 2013 11:33:44 -0400 Subject: [PATCH] [lib-result] read_*_field(): quit printing errors owl_read_*_field() do not print detailed error messages any more, but return a different error code depending on the error that occurred. --- libowlps-resultreader/libowlps-resultreader.c | 61 ++++++++----------- libowlps-resultreader/owlps-resultreader.h | 6 +- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/libowlps-resultreader/libowlps-resultreader.c b/libowlps-resultreader/libowlps-resultreader.c index accc35b..4c51582 100644 --- a/libowlps-resultreader/libowlps-resultreader.c +++ b/libowlps-resultreader/libowlps-resultreader.c @@ -122,7 +122,7 @@ owl_result* owl_fill_result(char *csv) strncpy(result->mobile_mac_addr, csv_field, OWL_ETHER_ADDR_STRLEN) ; /* Request type */ - if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield)) + if (owl_read_long_field(&csv, CSV_DELIMITER, &longfield)) { fprintf(stderr, "Error reading the request type from the CSV string!\n") ; @@ -138,7 +138,7 @@ owl_result* owl_fill_result(char *csv) /* Timestamp */ // Seconds - if (! owl_read_long_field(&csv, ".", &longfield)) + if (owl_read_long_field(&csv, ".", &longfield)) { fprintf(stderr, "Error reading the timestamp (seconds) from the CSV" @@ -147,7 +147,7 @@ owl_result* owl_fill_result(char *csv) } result->mobile_timestamp.tv_sec = longfield ; // Nanoseconds - if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield)) + if (owl_read_long_field(&csv, CSV_DELIMITER, &longfield)) { fprintf(stderr, "Error reading the timestamp (nanoseconds) from the CSV" @@ -226,7 +226,7 @@ owl_algorithm_result* owl_fill_algorithm_result(char **csv) strndup(csv_field, OWL_ALGORITHM_STRLEN) ; // X coordinate - if (! owl_read_float_field(csv, &algo->x)) + if (owl_read_float_field(csv, &algo->x)) { fprintf(stderr, "Error reading the X coordinate from the CSV string!\n") ; @@ -234,7 +234,7 @@ owl_algorithm_result* owl_fill_algorithm_result(char **csv) } // Y coordinate - if (! owl_read_float_field(csv, &algo->y)) + if (owl_read_float_field(csv, &algo->y)) { fprintf(stderr, "Error reading the Y coordinate from the CSV string!\n") ; @@ -242,7 +242,7 @@ owl_algorithm_result* owl_fill_algorithm_result(char **csv) } // Z coordinate - if (! owl_read_float_field(csv, &algo->z)) + if (owl_read_float_field(csv, &algo->z)) { fprintf(stderr, "Error reading the Z coordinate from the CSV string!\n") ; @@ -250,7 +250,7 @@ owl_algorithm_result* owl_fill_algorithm_result(char **csv) } // Distance error - if (! owl_read_float_field(csv, &algo->error)) + if (owl_read_float_field(csv, &algo->error)) { fprintf(stderr, "Error reading the distance error from the CSV string!\n") ; @@ -275,71 +275,60 @@ owl_algorithm_result* owl_fill_algorithm_result(char **csv) * stores it in `ret`. The fields of `csv` are separated by the symbols * in the `delim` string. `csv` will be modified to point to the next * field. + * Upon error, the value pointed by `ret` may or may not be modified. * * @param[in,out] csv The CSV string. * @param[in] delim The characters delimiting the fields in the string. * @param[out] ret The integer value read from the field. * - * @returns `true` in case of success. - * @returns `false` in case of error. + * @returns 0 in case of success. + * @returns 1 if the field could not be read. + * @returns 2 if the field could be read but did not contain a valid + * value. */ -bool owl_read_long_field(char **const csv, const char *const delim, +int owl_read_long_field(char **const csv, const char *const delim, long *const ret) { char *endptr = NULL, *csv_field = NULL ; csv_field = strsep(csv, delim) ; if (! csv_field) - { - fprintf(stderr, - "Error reading a field from the CSV string!\n") ; - return false ; - } + return 1 ; *ret = strtol(csv_field, &endptr, 10) ; if (endptr == csv_field) - { - fprintf(stderr, - "The field does not contain a valid integer value!\n") ; - return false ; - } + return 2 ; - return true ; + return 0 ; } /** * Reads the first field from the `csv` string as a float and stores it * in `ret`. `csv` will be modified to point to the next field. + * Upon error, the value pointed by `ret` may or may not be modified. * * @param[in,out] csv The CSV string. * @param[out] ret The float value read from the field. * - * @returns `true` in case of success. - * @returns `false` in case of error. + * @returns 0 in case of success. + * @returns 1 if the field could not be read. + * @returns 2 if the field could be read but did not contain a valid + * value. */ -bool owl_read_float_field(char **const csv, float *const ret) +int owl_read_float_field(char **const csv, float *const ret) { char *endptr = NULL, *csv_field = NULL ; csv_field = strsep(csv, CSV_DELIMITER) ; if (! csv_field) - { - fprintf(stderr, - "Error reading a field from the CSV string!\n") ; - return false ; - } + return 1 ; *ret = strtof(csv_field, &endptr) ; if (endptr == csv_field) - { - fprintf(stderr, - "The field does not contain a valid floating-point" - " value!\n") ; - return false ; - } + return 2 ; - return true ; + return 0 ; } diff --git a/libowlps-resultreader/owlps-resultreader.h b/libowlps-resultreader/owlps-resultreader.h index 601cb38..b4c4f74 100644 --- a/libowlps-resultreader/owlps-resultreader.h +++ b/libowlps-resultreader/owlps-resultreader.h @@ -114,9 +114,9 @@ owl_result* owl_fill_result(char *csv) ; /// Splits the `csv` string and stores it in a new `owl_algorithm_result` owl_algorithm_result* owl_fill_algorithm_result(char **csv) ; /// Reads an integer from the `csv` string -bool owl_read_long_field(char **const csv, const char *const delim, - long *const ret) ; -bool owl_read_float_field(char **const csv, float *const ret) ; +int owl_read_long_field(char **const csv, const char *const delim, + long *const ret) ; +int owl_read_float_field(char **const csv, float *const ret) ; /// Converts an `owl_result back` to a CSV string void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN],