aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--dbfp.c72
-rw-r--r--dbfp.h2
-rw-r--r--dbfp_check.c7
3 files changed, 66 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)
diff --git a/dbfp.h b/dbfp.h
index ca92ed1..b2537ad 100644
--- a/dbfp.h
+++ b/dbfp.h
@@ -24,6 +24,8 @@ struct dbfp {
struct dbfp_location {
char *name;
char *id;
+ float lon;
+ float lat;
};
int dbfp_init(struct dbfp *dbfp, char *key);
diff --git a/dbfp_check.c b/dbfp_check.c
index 2c048d8..5e8e225 100644
--- a/dbfp_check.c
+++ b/dbfp_check.c
@@ -48,6 +48,7 @@ START_TEST(test_dbfp_location_simple)
struct dbfp dbfp;
struct dbfp_location *locations = NULL;
size_t n;
+ size_t i;
err = dbfp_init(&dbfp, api_key);
ck_assert_int_eq(err, 0);
@@ -60,7 +61,13 @@ START_TEST(test_dbfp_location_simple)
ck_assert_str_eq(locations[0].name, "Freiburg(Breisgau) Hbf");
ck_assert_str_eq(locations[0].id, "008000107");
+ ck_assert(locations[0].lon == 7.841173f);
+ ck_assert(locations[0].lat == 47.997696f);
+ for (i = 0; i < n; i++) {
+ free(locations[i].name);
+ free(locations[i].id);
+ }
free(locations);
dbfp_close(&dbfp);
}