[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.
This commit is contained in:
Matteo Cypriani 2013-09-27 11:33:44 -04:00
parent b39ccb8f1e
commit 2d6b4c11d4
2 changed files with 28 additions and 39 deletions

View File

@ -122,7 +122,7 @@ owl_result* owl_fill_result(char *csv)
strncpy(result->mobile_mac_addr, csv_field, OWL_ETHER_ADDR_STRLEN) ; strncpy(result->mobile_mac_addr, csv_field, OWL_ETHER_ADDR_STRLEN) ;
/* Request type */ /* Request type */
if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield)) if (owl_read_long_field(&csv, CSV_DELIMITER, &longfield))
{ {
fprintf(stderr, fprintf(stderr,
"Error reading the request type from the CSV string!\n") ; "Error reading the request type from the CSV string!\n") ;
@ -138,7 +138,7 @@ owl_result* owl_fill_result(char *csv)
/* Timestamp */ /* Timestamp */
// Seconds // Seconds
if (! owl_read_long_field(&csv, ".", &longfield)) if (owl_read_long_field(&csv, ".", &longfield))
{ {
fprintf(stderr, fprintf(stderr,
"Error reading the timestamp (seconds) from the CSV" "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 ; result->mobile_timestamp.tv_sec = longfield ;
// Nanoseconds // Nanoseconds
if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield)) if (owl_read_long_field(&csv, CSV_DELIMITER, &longfield))
{ {
fprintf(stderr, fprintf(stderr,
"Error reading the timestamp (nanoseconds) from the CSV" "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) ; strndup(csv_field, OWL_ALGORITHM_STRLEN) ;
// X coordinate // X coordinate
if (! owl_read_float_field(csv, &algo->x)) if (owl_read_float_field(csv, &algo->x))
{ {
fprintf(stderr, fprintf(stderr,
"Error reading the X coordinate from the CSV string!\n") ; "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 // Y coordinate
if (! owl_read_float_field(csv, &algo->y)) if (owl_read_float_field(csv, &algo->y))
{ {
fprintf(stderr, fprintf(stderr,
"Error reading the Y coordinate from the CSV string!\n") ; "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 // Z coordinate
if (! owl_read_float_field(csv, &algo->z)) if (owl_read_float_field(csv, &algo->z))
{ {
fprintf(stderr, fprintf(stderr,
"Error reading the Z coordinate from the CSV string!\n") ; "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 // Distance error
if (! owl_read_float_field(csv, &algo->error)) if (owl_read_float_field(csv, &algo->error))
{ {
fprintf(stderr, fprintf(stderr,
"Error reading the distance error from the CSV string!\n") ; "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 * 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 * in the `delim` string. `csv` will be modified to point to the next
* field. * field.
* Upon error, the value pointed by `ret` may or may not be modified.
* *
* @param[in,out] csv The CSV string. * @param[in,out] csv The CSV string.
* @param[in] delim The characters delimiting the fields in the string. * @param[in] delim The characters delimiting the fields in the string.
* @param[out] ret The integer value read from the field. * @param[out] ret The integer value read from the field.
* *
* @returns `true` in case of success. * @returns 0 in case of success.
* @returns `false` in case of error. * @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) long *const ret)
{ {
char *endptr = NULL, *csv_field = NULL ; char *endptr = NULL, *csv_field = NULL ;
csv_field = strsep(csv, delim) ; csv_field = strsep(csv, delim) ;
if (! csv_field) if (! csv_field)
{ return 1 ;
fprintf(stderr,
"Error reading a field from the CSV string!\n") ;
return false ;
}
*ret = strtol(csv_field, &endptr, 10) ; *ret = strtol(csv_field, &endptr, 10) ;
if (endptr == csv_field) if (endptr == csv_field)
{ return 2 ;
fprintf(stderr,
"The field does not contain a valid integer value!\n") ;
return false ;
}
return true ; return 0 ;
} }
/** /**
* Reads the first field from the `csv` string as a float and stores it * 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. * 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[in,out] csv The CSV string.
* @param[out] ret The float value read from the field. * @param[out] ret The float value read from the field.
* *
* @returns `true` in case of success. * @returns 0 in case of success.
* @returns `false` in case of error. * @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 ; char *endptr = NULL, *csv_field = NULL ;
csv_field = strsep(csv, CSV_DELIMITER) ; csv_field = strsep(csv, CSV_DELIMITER) ;
if (! csv_field) if (! csv_field)
{ return 1 ;
fprintf(stderr,
"Error reading a field from the CSV string!\n") ;
return false ;
}
*ret = strtof(csv_field, &endptr) ; *ret = strtof(csv_field, &endptr) ;
if (endptr == csv_field) if (endptr == csv_field)
{ return 2 ;
fprintf(stderr,
"The field does not contain a valid floating-point"
" value!\n") ;
return false ;
}
return true ; return 0 ;
} }

View File

@ -114,9 +114,9 @@ owl_result* owl_fill_result(char *csv) ;
/// Splits the `csv` string and stores it in a new `owl_algorithm_result` /// Splits the `csv` string and stores it in a new `owl_algorithm_result`
owl_algorithm_result* owl_fill_algorithm_result(char **csv) ; owl_algorithm_result* owl_fill_algorithm_result(char **csv) ;
/// Reads an integer from the `csv` string /// Reads an integer from the `csv` string
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) ; long *const ret) ;
bool owl_read_float_field(char **const csv, float *const ret) ; int owl_read_float_field(char **const csv, float *const ret) ;
/// Converts an `owl_result back` to a CSV string /// Converts an `owl_result back` to a CSV string
void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN], void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN],