From 6fa903b14383090c3de1493aa308c4b74969ab50 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sat, 20 Aug 2011 10:03:50 +0200 Subject: [PATCH] [lib-result] Add owl_fill_algorithm_result() Create owl_fill_algorithm_result() from owl_fill_result(). --- .../libowlps-resultreader-udp.c | 159 ++++++++++-------- .../owlps-resultreader-udp.h | 1 + 2 files changed, 94 insertions(+), 66 deletions(-) diff --git a/libowlps-resultreader-udp/libowlps-resultreader-udp.c b/libowlps-resultreader-udp/libowlps-resultreader-udp.c index 19e01d1..c384309 100644 --- a/libowlps-resultreader-udp/libowlps-resultreader-udp.c +++ b/libowlps-resultreader-udp/libowlps-resultreader-udp.c @@ -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 ; } diff --git a/libowlps-resultreader-udp/owlps-resultreader-udp.h b/libowlps-resultreader-udp/owlps-resultreader-udp.h index f835cd1..8e730e3 100644 --- a/libowlps-resultreader-udp/owlps-resultreader-udp.h +++ b/libowlps-resultreader-udp/owlps-resultreader-udp.h @@ -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) ;