[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)
|
||||
{
|
||||
char *csv_field = NULL ;
|
||||
long longfield ; // Return value of owl_read_long_field()
|
||||
owl_result *result = NULL ;
|
||||
|
||||
result = malloc(sizeof(owl_result)) ;
|
||||
|
@ -113,36 +114,33 @@ owl_result* owl_fill_result(char *csv)
|
|||
strndup(csv_field, OWL_ETHER_ADDR_STRLEN) ;
|
||||
|
||||
/* Request type */
|
||||
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
||||
if (! csv_field)
|
||||
if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield))
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Error reading the request type from the CSV string!\n") ;
|
||||
goto error ;
|
||||
}
|
||||
result->request_type = atoi(csv_field) ;
|
||||
result->request_type = longfield ;
|
||||
|
||||
/* Timestamp */
|
||||
// Seconds
|
||||
csv_field = strsep(&csv, ".") ;
|
||||
if (! csv_field)
|
||||
if (! owl_read_long_field(&csv, ".", &longfield))
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Error reading the timestamp (seconds) from the CSV"
|
||||
" string!\n") ;
|
||||
goto error ;
|
||||
}
|
||||
result->mobile_timestamp.tv_sec = atol(csv_field) ;
|
||||
result->mobile_timestamp.tv_sec = longfield ;
|
||||
// Nanoseconds
|
||||
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
||||
if (! csv_field)
|
||||
if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield))
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Error reading the timestamp (nanoseconds) from the CSV"
|
||||
" string!\n") ;
|
||||
goto error ;
|
||||
}
|
||||
result->mobile_timestamp.tv_nsec = atol(csv_field) ;
|
||||
result->mobile_timestamp.tv_nsec = longfield ;
|
||||
|
||||
/* Algorithm results */
|
||||
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.
|
||||
* `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) ;
|
||||
/// 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) ;
|
||||
|
||||
/// Converts an `owl_result back` to a CSV string
|
||||
void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN],
|
||||
|
|
Loading…
Reference in New Issue