aboutsummaryrefslogtreecommitdiff
path: root/dbfp.c
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2016-03-10 16:18:19 +0100
committerRobin Krahl <me@robin-krahl.de>2016-03-10 16:18:19 +0100
commit4d9a2fba4987cd27b07db426fea921efcf8c9fc3 (patch)
treed7e0580ff8db63221a537c6d9e4c9834b3755b4c /dbfp.c
parent913fc9d2c2935e0faf34657747286ce83af21853 (diff)
downloaddbfp-4d9a2fba4987cd27b07db426fea921efcf8c9fc3.tar.gz
dbfp-4d9a2fba4987cd27b07db426fea921efcf8c9fc3.tar.bz2
dbfp: add lon and lat to dbfp_location and to the query
The location.name query (dbfp_query_location_name) also returns the coordinates of the location. This commit adds them to the dbfp_location structure and to the query itself.
Diffstat (limited to 'dbfp.c')
-rw-r--r--dbfp.c72
1 files changed, 57 insertions, 15 deletions
diff --git a/dbfp.c b/dbfp.c
index 8aed661..9f2dd02 100644
--- a/dbfp.c
+++ b/dbfp.c
@@ -33,9 +33,12 @@ static int dbfp_request(struct dbfp *dbfp, char *service, char *query,
static int dbfp_url(struct dbfp *dbfp, char *service, char *query, char **url);
static int check_ele_error(const char *name, const char **atts);
+static int read_string(const char **atts, size_t idx, char **str);
+static int read_float(const char **atts, size_t idx, float *f);
static int add_location(struct location_data *ld, const char **atts,
- size_t idx_name, size_t idx_id);
+ size_t idx_name, size_t idx_id, size_t idx_lon,
+ size_t idx_lat);
static void location_start(void *data, const char *name, const char **atts);
static void location_end(void *data, const char *name);
@@ -188,20 +191,51 @@ static int check_ele_error(const char *name, const char **atts)
return 0;
}
+static int read_string(const char **atts, size_t idx, char **str)
+{
+ *str = strdup(atts[idx]);
+ if (!*str)
+ return ENOMEM;
+
+ return 0;
+}
+
+static int read_float(const char **atts, size_t idx, float *f)
+{
+ char *endptr = NULL;
+
+ errno = 0;
+ *f = strtof(atts[idx], &endptr);
+ if (atts[idx] == endptr || errno != 0)
+ return DBFP_ERROR_STRUCTURE;
+
+ return 0;
+}
+
static int add_location(struct location_data *ld, const char **atts,
- size_t idx_name, size_t idx_id)
+ size_t idx_name, size_t idx_id, size_t idx_lon,
+ size_t idx_lat)
{
int err = 0;
- char *val_name = NULL;
- char *val_id = NULL;
+ char *name = NULL;
+ char *id = NULL;
+ float lon;
+ float lat;
- val_name = strdup(atts[idx_name]);
- val_id = strdup(atts[idx_id]);
- if (!val_name || !val_id) {
- err = ENOMEM;
+ err = read_string(atts, idx_name, &name);
+ if (err)
+ goto cleanup;
+ err = read_string(atts, idx_id, &id);
+ if (err)
+ goto cleanup;
+ err = read_float(atts, idx_lon, &lon);
+ if (err)
+ goto cleanup;
+ err = read_float(atts, idx_lat, &lat);
+ if (err)
goto cleanup;
- }
+ /* TODO(robin.krahl): don't give up the old memory if realloc fails */
ld->locations = realloc(ld->locations,
(ld->n + 1) * sizeof(*ld->locations));
if (!ld->locations) {
@@ -209,14 +243,16 @@ static int add_location(struct location_data *ld, const char **atts,
goto cleanup;
}
+ ld->locations[ld->n].name = name;
+ ld->locations[ld->n].id = id;
+ ld->locations[ld->n].lon = lon;
+ ld->locations[ld->n].lat = lat;
ld->n++;
- ld->locations[ld->n - 1].name = val_name;
- ld->locations[ld->n - 1].id = val_id;
cleanup:
if (err) {
- free(val_name);
- free(val_id);
+ free(name);
+ free(id);
}
return err;
@@ -229,6 +265,8 @@ static void location_start(void *data, const char *name, const char **atts)
struct location_data *ld = (struct location_data *)pd->data;
size_t idx_name = 0;
size_t idx_id = 0;
+ size_t idx_lon = 0;
+ size_t idx_lat = 0;
size_t i;
err = check_ele_error(name, atts);
@@ -243,14 +281,18 @@ static void location_start(void *data, const char *name, const char **atts)
idx_name = i + 1;
else if (strcmp(atts[i], "id") == 0)
idx_id = i + 1;
+ else if (strcmp(atts[i], "lon") == 0)
+ idx_lon = i + 1;
+ else if (strcmp(atts[i], "lat") == 0)
+ idx_lat = i + 1;
}
- if (!idx_name || !idx_id) {
+ if (!idx_name || !idx_id || !idx_lon || !idx_lat) {
err = DBFP_ERROR_STRUCTURE;
goto cleanup;
}
- err = add_location(ld, atts, idx_name, idx_id);
+ err = add_location(ld, atts, idx_name, idx_id, idx_lon, idx_lat);
cleanup:
if (err)