diff options
-rw-r--r-- | dbfp.c | 72 | ||||
-rw-r--r-- | dbfp.h | 2 | ||||
-rw-r--r-- | dbfp_check.c | 7 |
3 files changed, 66 insertions, 15 deletions
@@ -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) @@ -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); } |