[lib-result] owl_receive_position(): return a pointer

Return a pointer instead of using a result argument.
This commit is contained in:
Matteo Cypriani 2011-08-20 08:57:29 +02:00
parent 8da063b597
commit a7ec779231
3 changed files with 38 additions and 32 deletions

View File

@ -11,16 +11,14 @@
/*
* Receives a result from the socket of file descriptor 'sockfd' and
* fills an owl_result structure 'result'.
* Receives a result from the socket of file descriptor 'sockfd', fills
* an owl_result structure with the received information, and returns a
* pointer to it (or NULL in case of error).
*
* Note that *result will be set by this function, and **result will
* point on a valid owl_result structure if everything goes well.
* You will have to free *result yourself with owl_free_result().
*
* In case of error, *result is set to NULL.
* Note that the new owl_result is allocated with malloc() and must be
* deleted using free().
*/
void owl_receive_position(int sockfd, owl_result **result)
owl_result* owl_receive_position(int sockfd)
{
ssize_t nread ; // recvfrom return value
char csv[OWL_CSV_RESULT_STRLEN] ; // Read string
@ -31,30 +29,35 @@ void owl_receive_position(int sockfd, owl_result **result)
if (nread <= 0)
{
perror("No request received from listener") ;
*result = NULL ;
return ;
return NULL ;
}
owl_fill_result(result, csv) ;
return owl_fill_result(csv) ;
}
/* Splits the 'csv' string received from OwlPS Positioning and stores
* the fields in 'result' (which is allocated).
/*
* Splits the 'csv' string received from OwlPS Positioning and stores
* the fields in a new owl_result, and returns a pointer to it (or NULL
* in case of error).
*
* Note that the new owl_result is allocated with malloc() and must be
* deleted using free().
*
* Handled CSV format:
* Mobile_MAC;Request_type;Request_timestamp;Algorithm;X;Y;Z;Error;Area
* The Request_timestamp format is:
* seconds.nanoseconds
*/
void owl_fill_result(owl_result **result, char *csv)
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)) ;
(*result)->results = NULL ;
result = malloc(sizeof(owl_result)) ;
memset(result, 0, sizeof(*result)) ;
/* Mobile MAC address */
csv_field = strsep(&csv, CSV_DELIMITER) ;
@ -65,7 +68,7 @@ void owl_fill_result(owl_result **result, char *csv)
" string (empty string?)!\n") ;
goto error ;
}
(*result)->mobile_mac_addr =
result->mobile_mac_addr =
strndup(csv_field, OWL_ETHER_ADDR_STRLEN) ;
/* Request type */
@ -76,7 +79,7 @@ void owl_fill_result(owl_result **result, char *csv)
"Error reading the request type from the CSV string!\n") ;
goto error ;
}
(*result)->request_type = atoi(csv_field) ;
result->request_type = atoi(csv_field) ;
/* Timestamp */
// Seconds
@ -88,7 +91,7 @@ void owl_fill_result(owl_result **result, char *csv)
" string!\n") ;
goto error ;
}
(*result)->mobile_timestamp.tv_sec = atol(csv_field) ;
result->mobile_timestamp.tv_sec = atol(csv_field) ;
// Nanoseconds
csv_field = strsep(&csv, CSV_DELIMITER) ;
if (! csv_field)
@ -98,7 +101,7 @@ void owl_fill_result(owl_result **result, char *csv)
" string!\n") ;
goto error ;
}
(*result)->mobile_timestamp.tv_nsec = atol(csv_field) ;
result->mobile_timestamp.tv_nsec = atol(csv_field) ;
/* Algorithm results */
csv_field = strsep(&csv, CSV_DELIMITER) ;
@ -106,6 +109,7 @@ void owl_fill_result(owl_result **result, char *csv)
{
++nb_algorithms ;
current_algo = malloc(sizeof(owl_algorithm_result)) ;
memset(current_algo, 0, sizeof(*current_algo)) ;
// Algorithm name
if (! csv_field)
@ -168,17 +172,17 @@ void owl_fill_result(owl_result **result, char *csv)
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 ;
current_algo->next = result->results ;
result->results = current_algo ;
}
while ((csv_field = strsep(&csv, CSV_DELIMITER))) ;
return ; // Success
return result ; // Success
error:
owl_free_result(*result) ;
*result = NULL ;
free(current_algo) ;
owl_free_result(result) ;
owl_free_algorithm_result(current_algo) ;
return NULL ;
}
@ -296,7 +300,9 @@ void owl_fprint_algorithm_result(FILE *stream,
/*
* Frees the memory allocated by an owl_result.
* Frees the memory allocated by an owl_result. The 'results' and
* 'mobile_mac_addr' fields *must* be defined, either to NULL or to a
* valid memory block allocated with malloc().
* Note that the pointer will not set to NULL.
*/
void owl_free_result(owl_result *result)
@ -316,7 +322,8 @@ void owl_free_result(owl_result *result)
/*
* Frees the memory allocated by a single owl_algorithm_result (*not*
* recursively).
* recursively). The 'algorithm' and 'area' fields *must* be defined,
* either to NULL or to a valid memory block allocated with malloc().
* Note that the pointer will not set to NULL.
*/
void owl_free_algorithm_result(owl_algorithm_result *algo)

View File

@ -20,8 +20,7 @@ int main(void)
while (1)
{
owl_receive_position(sockfd, &result) ;
if (result == NULL)
if (! (result = owl_receive_position(sockfd)))
return 1 ;
owl_print_result(result) ;
owl_free_result(result) ;

View File

@ -54,8 +54,8 @@ typedef struct _owl_result
} owl_result ;
void owl_receive_position(int sockfd, owl_result **result) ;
void owl_fill_result(owl_result **result, char *csv) ;
owl_result* owl_receive_position(int sockfd) ;
owl_result* owl_fill_result(char *csv) ;
void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN],
const owl_result *const src) ;