aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2016-06-01 21:49:14 +0200
committerRobin Krahl <me@robin-krahl.de>2016-06-01 21:49:14 +0200
commit852d0f1cffd53d9327aa38630c295a552a2b0a8e (patch)
tree8a6e15d511d3e3d5aec15fc84754308d34611015
parent9512da3f7af8193bf381080cf52e1b0d29e71e6c (diff)
downloaddbfp-852d0f1cffd53d9327aa38630c295a552a2b0a8e.tar.gz
dbfp-852d0f1cffd53d9327aa38630c295a552a2b0a8e.tar.bz2
dbfp: move URL formatting to dbfp_format_url
-rw-r--r--dbfp.c38
1 files changed, 28 insertions, 10 deletions
diff --git a/dbfp.c b/dbfp.c
index 05343a1..57fbcad 100644
--- a/dbfp.c
+++ b/dbfp.c
@@ -8,6 +8,7 @@
#include <errno.h>
#include <expat.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -36,6 +37,8 @@ static int dbfp_parse(void *contents, size_t len, size_t n, void *data);
static int dbfp_request(struct dbfp *dbfp, char *service, char *query,
XML_Parser parser);
static int dbfp_url(struct dbfp *dbfp, char *service, char *query, char **url);
+static int dbfp_format_url(struct dbfp *dbfp, char **out, const char *format,
+ ...);
static int check_ele_error(const char *name, const char **atts);
static int read_string(const char **atts, size_t idx, void *value);
@@ -113,7 +116,6 @@ int dbfp_query_location_name(struct dbfp *dbfp, char *input,
char *input_enc = NULL;
char *query = NULL;
XML_Parser parser = NULL;
- int len;
struct location_data ld = { .n = 0, .locations = NULL };
struct parse_data pd = { .error = 0, .data = &ld };
@@ -128,16 +130,9 @@ int dbfp_query_location_name(struct dbfp *dbfp, char *input,
err = DBFP_ERROR_CURL;
goto cleanup;
}
- query = malloc(DBFP_URL_LEN);
- if (!query) {
- err = ENOMEM;
- goto cleanup;
- }
- len = snprintf(query, DBFP_URL_LEN, "input=%s", input_enc);
- if (len < 0 || len >= DBFP_URL_LEN) {
- err = DBFP_ERROR_FORMAT;
+ err = dbfp_format_url(dbfp, &query, "input=%s", input_enc);
+ if (err)
goto cleanup;
- }
parser = XML_ParserCreateNS(NULL, '\0');
XML_SetUserData(parser, &pd);
@@ -233,6 +228,29 @@ static int dbfp_url(struct dbfp *dbfp, char *service, char *query, char **url)
return 0;
}
+static int dbfp_format_url(struct dbfp *dbfp, char **out, const char *format,
+ ...)
+{
+ va_list args;
+ int len;
+
+ *out = malloc(DBFP_URL_LEN);
+ if (!*out)
+ return ENOMEM;
+
+ va_start(args, format);
+ len = vsnprintf(*out, DBFP_URL_LEN, format, args);
+ va_end(args);
+
+ if (len < 0 || len >= DBFP_URL_LEN) {
+ free(*out);
+ *out = NULL;
+ return DBFP_ERROR_FORMAT;
+ }
+
+ return 0;
+}
+
static int check_ele_error(const char *name, const char **atts)
{
if (strcmp(name, "Error") == 0) {