[lib-result] Add read_long_field() and use it
This new function allows to read a CSV field containing an integer value and checks the value read.
This commit is contained in:
parent
d4cd2e6a63
commit
21e624728c
|
@ -90,6 +90,7 @@ owl_result* owl_receive_position(const int sockfd)
|
||||||
owl_result* owl_fill_result(char *csv)
|
owl_result* owl_fill_result(char *csv)
|
||||||
{
|
{
|
||||||
char *csv_field = NULL ;
|
char *csv_field = NULL ;
|
||||||
|
long longfield ; // Return value of owl_read_long_field()
|
||||||
owl_result *result = NULL ;
|
owl_result *result = NULL ;
|
||||||
|
|
||||||
result = malloc(sizeof(owl_result)) ;
|
result = malloc(sizeof(owl_result)) ;
|
||||||
|
@ -113,36 +114,33 @@ owl_result* owl_fill_result(char *csv)
|
||||||
strndup(csv_field, OWL_ETHER_ADDR_STRLEN) ;
|
strndup(csv_field, OWL_ETHER_ADDR_STRLEN) ;
|
||||||
|
|
||||||
/* Request type */
|
/* Request type */
|
||||||
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield))
|
||||||
if (! csv_field)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Error reading the request type from the CSV string!\n") ;
|
"Error reading the request type from the CSV string!\n") ;
|
||||||
goto error ;
|
goto error ;
|
||||||
}
|
}
|
||||||
result->request_type = atoi(csv_field) ;
|
result->request_type = longfield ;
|
||||||
|
|
||||||
/* Timestamp */
|
/* Timestamp */
|
||||||
// Seconds
|
// Seconds
|
||||||
csv_field = strsep(&csv, ".") ;
|
if (! owl_read_long_field(&csv, ".", &longfield))
|
||||||
if (! csv_field)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Error reading the timestamp (seconds) from the CSV"
|
"Error reading the timestamp (seconds) from the CSV"
|
||||||
" string!\n") ;
|
" string!\n") ;
|
||||||
goto error ;
|
goto error ;
|
||||||
}
|
}
|
||||||
result->mobile_timestamp.tv_sec = atol(csv_field) ;
|
result->mobile_timestamp.tv_sec = longfield ;
|
||||||
// Nanoseconds
|
// Nanoseconds
|
||||||
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield))
|
||||||
if (! csv_field)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Error reading the timestamp (nanoseconds) from the CSV"
|
"Error reading the timestamp (nanoseconds) from the CSV"
|
||||||
" string!\n") ;
|
" string!\n") ;
|
||||||
goto error ;
|
goto error ;
|
||||||
}
|
}
|
||||||
result->mobile_timestamp.tv_nsec = atol(csv_field) ;
|
result->mobile_timestamp.tv_nsec = longfield ;
|
||||||
|
|
||||||
/* Algorithm results */
|
/* Algorithm results */
|
||||||
do
|
do
|
||||||
|
@ -266,6 +264,44 @@ owl_algorithm_result* owl_fill_algorithm_result(char **csv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the first field from the `csv` string as a long integer and
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @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.
|
||||||
|
*/
|
||||||
|
bool 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 ;
|
||||||
|
}
|
||||||
|
|
||||||
|
*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 true ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an `owl_result` back to a CSV string.
|
* Converts an `owl_result` back to a CSV string.
|
||||||
* `dst` must be an allocated string of at least `OWL_CSV_RESULT_STRLEN`
|
* `dst` must be an allocated string of at least `OWL_CSV_RESULT_STRLEN`
|
||||||
|
|
|
@ -113,6 +113,9 @@ owl_result* owl_receive_position(const int sockfd) ;
|
||||||
owl_result* owl_fill_result(char *csv) ;
|
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
|
||||||
|
bool owl_read_long_field(char **const csv, const char *const delim,
|
||||||
|
long *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],
|
||||||
|
|
Loading…
Reference in New Issue