[lib-result] Add owl_fill_algorithm_result()

Create owl_fill_algorithm_result() from owl_fill_result().
This commit is contained in:
Matteo Cypriani 2011-08-20 10:03:50 +02:00
parent a7ec779231
commit 6fa903b143
2 changed files with 94 additions and 66 deletions

View File

@ -48,13 +48,13 @@ owl_result* owl_receive_position(int sockfd)
* Mobile_MAC;Request_type;Request_timestamp;Algorithm;X;Y;Z;Error;Area
* The Request_timestamp format is:
* seconds.nanoseconds
* The Area field can be empty.
*/
owl_result* owl_fill_result(char *csv)
{
char *csv_field = NULL ;
owl_result *result = NULL ;
int nb_algorithms = 0 ;
owl_algorithm_result *current_algo = NULL ;
result = malloc(sizeof(owl_result)) ;
memset(result, 0, sizeof(*result)) ;
@ -104,84 +104,111 @@ owl_result* owl_fill_result(char *csv)
result->mobile_timestamp.tv_nsec = atol(csv_field) ;
/* Algorithm results */
csv_field = strsep(&csv, CSV_DELIMITER) ;
do
{
++nb_algorithms ;
current_algo = malloc(sizeof(owl_algorithm_result)) ;
memset(current_algo, 0, sizeof(*current_algo)) ;
// Algorithm name
if (! csv_field)
owl_algorithm_result *current_algo =
owl_fill_algorithm_result(&csv) ;
if (current_algo == NULL)
{
fprintf(stderr,
"Error reading the algorithm name from the CSV"
" string (algorithm #%d)!\n", nb_algorithms) ;
goto error ;
fprintf(stderr, "Error reading the algorithm #%d!\n",
nb_algorithms) ;
break ;
}
current_algo->algorithm =
strndup(csv_field, OWL_ALGORITHM_STRLEN) ;
// X coordinate
csv_field = strsep(&csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the X coordinate from the CSV"
" string (algorithm #%d)!\n", nb_algorithms) ;
goto error ;
}
current_algo->x = atof(csv_field) ;
// Y coordinate
csv_field = strsep(&csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the Y coordinate from the CSV"
" string (algorithm #%d)!\n", nb_algorithms) ;
goto error ;
}
current_algo->y = atof(csv_field) ;
// Z coordinate
csv_field = strsep(&csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the Z coordinate from the CSV"
" string (algorithm #%d)!\n", nb_algorithms) ;
goto error ;
}
current_algo->z = atof(csv_field) ;
// Distance error
csv_field = strsep(&csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the distance error from the CSV"
" string (algorithm #%d)!\n", nb_algorithms) ;
goto error ;
}
current_algo->error = atof(csv_field) ;
// Area name (optional)
csv_field = strsep(&csv, CSV_DELIMITER) ;
if (csv_field)
current_algo->area = strndup(csv_field, OWL_AREA_STRLEN) ;
// Insert the current algorithm at the begining of the list
current_algo->next = result->results ;
result->results = current_algo ;
++nb_algorithms ;
}
while ((csv_field = strsep(&csv, CSV_DELIMITER))) ;
while (csv) ;
return result ; // Success
error:
owl_free_result(result) ;
owl_free_algorithm_result(current_algo) ;
return NULL ;
}
/*
* Splits the 'csv' string, stores the fields in a new
* owl_algorithm_result, and returns a pointer to it (or NULL
* in case of error).
*
* Note that the new owl_algorithm_result is allocated with malloc()
* and must be deleted using free().
*
* 'csv' must follow this CSV format:
* Algorithm;X;Y;Z;Error;Area
* The Area field can be empty.
*/
owl_algorithm_result* owl_fill_algorithm_result(char **csv)
{
owl_algorithm_result *algo ;
char *csv_field = NULL ;
algo = malloc(sizeof(owl_algorithm_result)) ;
memset(algo, 0, sizeof(*algo)) ;
// Algorithm name
csv_field = strsep(csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the algorithm name from the CSV string!\n") ;
goto error ;
}
algo->algorithm =
strndup(csv_field, OWL_ALGORITHM_STRLEN) ;
// X coordinate
csv_field = strsep(csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the X coordinate from the CSV string!\n") ;
goto error ;
}
algo->x = atof(csv_field) ;
// Y coordinate
csv_field = strsep(csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the Y coordinate from the CSV string!\n") ;
goto error ;
}
algo->y = atof(csv_field) ;
// Z coordinate
csv_field = strsep(csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the Z coordinate from the CSV string!\n") ;
goto error ;
}
algo->z = atof(csv_field) ;
// Distance error
csv_field = strsep(csv, CSV_DELIMITER) ;
if (! csv_field)
{
fprintf(stderr,
"Error reading the distance error from the CSV string!\n") ;
goto error ;
}
algo->error = atof(csv_field) ;
// Area name (optional)
csv_field = strsep(csv, CSV_DELIMITER) ;
if (csv_field)
algo->area = strndup(csv_field, OWL_AREA_STRLEN) ;
return algo ; // Success
error:
owl_free_algorithm_result(algo) ;
return NULL ;
}

View File

@ -56,6 +56,7 @@ typedef struct _owl_result
owl_result* owl_receive_position(int sockfd) ;
owl_result* owl_fill_result(char *csv) ;
owl_algorithm_result* owl_fill_algorithm_result(char **csv) ;
void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN],
const owl_result *const src) ;