From 8da063b597e6ce8515d76c842de684757a4f6547 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Fri, 19 Aug 2011 17:22:43 +0200 Subject: [PATCH] [lib-result] Add {algorithm,}result_to_csv() Add owl_result_to_csv() and owl_algorithm_result_to_csv(), that allow to get an owl_result or owl_algorithm_result as a CSV string. --- .../libowlps-resultreader-udp.c | 56 +++++++++++++++++++ .../owlps-resultreader-udp.h | 6 ++ 2 files changed, 62 insertions(+) diff --git a/libowlps-resultreader-udp/libowlps-resultreader-udp.c b/libowlps-resultreader-udp/libowlps-resultreader-udp.c index 37cfd48..ee0da8f 100644 --- a/libowlps-resultreader-udp/libowlps-resultreader-udp.c +++ b/libowlps-resultreader-udp/libowlps-resultreader-udp.c @@ -182,6 +182,62 @@ void owl_fill_result(owl_result **result, char *csv) } +/* + * Converts an owl_result back to a CSV string. + * 'dst' must be an allocated string of at least OWL_CSV_RESULT_STRLEN + * characters. + */ +void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN], + const owl_result *const src) +{ + size_t dst_len ; + char timestamp_str[OWL_TIMESTAMP_STRLEN] ; + + assert(src) ; + + owl_timestamp_to_string(timestamp_str, src->mobile_timestamp) ; + snprintf(dst, OWL_CSV_RESULT_REQUEST_STRLEN, + "%s;%"PRIu8";%s", + src->mobile_mac_addr, + src->request_type, + timestamp_str) ; + dst_len = strlen(dst) ; + + owl_algorithm_result *algo = src->results ; + while (algo) + { + char algo_str[OWL_CSV_ALGORITHM_RESULT_STRLEN] ; + owl_algorithm_result_to_csv(algo_str, algo) ; + dst[dst_len++] = ';' ; + strncpy(dst + dst_len, algo_str, OWL_CSV_ALGORITHM_RESULT_STRLEN) ; + dst_len += strlen(algo_str) ; + algo = algo->next ; + } +} + + +/* + * Converts an owl_algorithm_result back to a CSV string. + * 'dst' must be an allocated string of at least + * OWL_CSV_ALGORITHM_RESULT_STRLEN characters. + */ +void +owl_algorithm_result_to_csv(char dst[OWL_CSV_ALGORITHM_RESULT_STRLEN], + const owl_algorithm_result *const src) +{ + assert(src) ; + + snprintf(dst, OWL_CSV_ALGORITHM_RESULT_STRLEN, + "%s;%f;%f;%f;%f;%s", + src->algorithm, + src->x, + src->y, + src->z, + src->error, + src->area ? src->area : "") ; +} + + /* * Prints an owl_result to the given stream. */ diff --git a/libowlps-resultreader-udp/owlps-resultreader-udp.h b/libowlps-resultreader-udp/owlps-resultreader-udp.h index 9d72980..ca5960a 100644 --- a/libowlps-resultreader-udp/owlps-resultreader-udp.h +++ b/libowlps-resultreader-udp/owlps-resultreader-udp.h @@ -57,6 +57,12 @@ typedef struct _owl_result void owl_receive_position(int sockfd, owl_result **result) ; void owl_fill_result(owl_result **result, char *csv) ; +void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN], + const owl_result *const src) ; +void +owl_algorithm_result_to_csv(char dst[OWL_CSV_ALGORITHM_RESULT_STRLEN], + const owl_algorithm_result *const src) ; + void owl_fprint_result(FILE *stream, const owl_result *const src) ; void owl_fprint_algorithm_result(FILE *stream, const owl_algorithm_result *const src) ;