From c0de0120441043d7f33f6eb06646cab2cdbd63aa Mon Sep 17 00:00:00 2001 From: Markus Koch Date: Sun, 3 May 2020 20:13:25 +0200 Subject: [PATCH] Clean up --- src/dijkstrasearch.c | 31 +++++++++++++++++++------------ src/dijkstraserv.c | 1 - 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/dijkstrasearch.c b/src/dijkstrasearch.c index d06d5ca..3ec75e0 100644 --- a/src/dijkstrasearch.c +++ b/src/dijkstrasearch.c @@ -203,7 +203,14 @@ GList *dijkstra_search_get_route(struct dijkstra_search *search, return l; } -int printb(char **buf, int *offset, const char *__restrict __format, ...) +/*! + * \brief sdprintf printf to a dynamic buffer + * \param buf The character buffer. Will append if not NULL; + * \param offset Should be set to strlen when continuing a string. Will be incremented automatically. + * \param __format printf format string + * \return the number of bytes written + */ +int sdprintf(char **buf, int *offset, const char *__restrict __format, ...) { va_list vargs; int len; @@ -236,7 +243,7 @@ int dijkstra_search_route_to_geojson(struct dijkstra_search *search, *buf = NULL; - printb(buf, &offset, "["); + sdprintf(buf, &offset, "["); for (l = route; l != NULL; l = l->next) { node = (struct dijkstra_node*) l->data; @@ -258,31 +265,31 @@ int dijkstra_search_route_to_geojson(struct dijkstra_search *search, format_str = "Go %s onto %s."; if (node_last != search->start) - printb(buf, &offset, ","); - printb(buf, &offset, "\n"); - printb(buf, &offset, "{\"type\": \"Feature\",\n" + sdprintf(buf, &offset, ","); + sdprintf(buf, &offset, "\n"); + sdprintf(buf, &offset, "{\"type\": \"Feature\",\n" " \"geometry\": {\n" " \"type\": \"LineString\", \"coordinates\": [[%f,%f],[%f,%f]]\n" " },\n", node_last->position.x, node_last->position.y, node->position.x, node->position.y); - printb(buf, &offset, " \"properties\": {\n" + sdprintf(buf, &offset, " \"properties\": {\n" " \"heading\": \"%f\",\n" " \"through\": \"%s\",\n", heading, search->states[node->uid]->cheapest_path->name); - printb(buf, &offset, " \"description\": \""); - printb(buf, &offset, format_str, relative_direction_str, search->states[node->uid]->cheapest_path->name); - printb(buf, &offset, "\"\n"); + sdprintf(buf, &offset, " \"description\": \""); + sdprintf(buf, &offset, format_str, relative_direction_str, search->states[node->uid]->cheapest_path->name); + sdprintf(buf, &offset, "\"\n"); - printb(buf, &offset, " }\n"); - printb(buf, &offset, "}"); + sdprintf(buf, &offset, " }\n"); + sdprintf(buf, &offset, "}"); last_heading = heading; } node_last = node; } - printb(buf, &offset, "\n]\n"); + sdprintf(buf, &offset, "\n]\n"); return offset; } diff --git a/src/dijkstraserv.c b/src/dijkstraserv.c index 4255be1..b973d1b 100644 --- a/src/dijkstraserv.c +++ b/src/dijkstraserv.c @@ -117,7 +117,6 @@ static void dijkstra_serv_soup_callback (SoupServer *server, soup_message_set_status (msg, SOUP_STATUS_OK); } - soup_message_set_response (msg, "application/json", SOUP_MEMORY_COPY, body, body_len); free(body);