From 815ea6908c3bb133b9c3e8ef7bc1bd7d00e3ba26 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Fri, 2 Oct 2015 16:26:21 +0200 Subject: [PATCH] add user/device to location JSON for multilister() closes #49 --- http.c | 2 +- ocat.c | 5 +++-- storage.c | 22 +++++++++++++++++++++- storage.h | 2 +- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/http.c b/http.c index 93829f6..1f96d65 100644 --- a/http.c +++ b/http.c @@ -386,7 +386,7 @@ static int dispatch(struct mg_connection *conn, const char *uri) if ((arr = json_find_member(json, "results")) != NULL) { JsonNode *f; json_foreach(f, arr) { - locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, NULL); + locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, NULL, NULL, NULL); // printf("%s\n", f->string_); } } diff --git a/ocat.c b/ocat.c index 344074b..b67d85b 100644 --- a/ocat.c +++ b/ocat.c @@ -519,7 +519,7 @@ int main(int argc, char **argv) int n; for (n = 0; n < argc; n++) { - locations(argv[n], obj, locs, s_lo, s_hi, otype, 0, fields); + locations(argv[n], obj, locs, s_lo, s_hi, otype, 0, fields, NULL, NULL); } } else { JsonNode *arr, *f; @@ -532,7 +532,8 @@ int main(int argc, char **argv) if ((json = lister(username, device, s_lo, s_hi, (limit > 0) ? TRUE : FALSE)) != NULL) { if ((arr = json_find_member(json, "results")) != NULL) { // get array json_foreach(f, arr) { - locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, fields); + // locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, fields, username, device); + locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, fields, NULL, NULL); // printf("%s\n", f->string_); } } diff --git a/storage.c b/storage.c index a4864b9..20a8b2d 100644 --- a/storage.c +++ b/storage.c @@ -513,6 +513,8 @@ struct jparam { output_type otype; int limit; /* if non-zero, we're searching backwards */ JsonNode *fields; + char *username; + char *device; }; /* @@ -600,6 +602,8 @@ static int candidate_line(char *line, void *param) time_t s_hi = jarg->s_hi; JsonNode *fields = jarg->fields; output_type otype = jarg->otype; + char *username = jarg->username; + char *device = jarg->device; if (obj == NULL || obj->tag != JSON_OBJECT) return (-1); @@ -658,6 +662,16 @@ static int candidate_line(char *line, void *param) // fprintf(stderr, "-->[%s]\n", line); if ((o = line_to_location(line)) != NULL) { + + /* + * Username/device are added typically for multilister() only. + */ + + if (username) + json_append_member(o, "username", json_mkstring(username)); + if (device) + json_append_member(o, "device", json_mkstring(device)); + if (fields) { /* Create a new object, copying members we're interested in into it */ JsonNode *f, *node; @@ -672,6 +686,7 @@ static int candidate_line(char *line, void *param) } json_delete(o); o = newo; + } json_append_element(locs, o); ++counter; @@ -689,9 +704,12 @@ static int candidate_line(char *line, void *param) * contains `arr'. * If limit is zero, we're going forward, else backwards. * Fields, if not NULL, is a JSON array of desired element names. + * + * If username & device are not NULL, populate the JSON locations + * with them for multilister(). */ -void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit, JsonNode *fields) +void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit, JsonNode *fields, char *username, char *device) { struct jparam jarg; @@ -705,6 +723,8 @@ void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t jarg.otype = otype; jarg.limit = limit; jarg.fields = fields; + jarg.username = username; + jarg.device = device; if (limit == 0) { cat(filename, candidate_line, &jarg); diff --git a/storage.h b/storage.h index b48f175..3c3a269 100644 --- a/storage.h +++ b/storage.h @@ -37,7 +37,7 @@ typedef enum { JsonNode *lister(char *username, char *device, time_t s_lo, time_t s_hi, int reverse); JsonNode *multilister(JsonNode *udpairs, time_t s_lo, time_t s_hi, int reverse); -void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit, JsonNode *fields); +void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit, JsonNode *fields, char *username, char *device); int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to); JsonNode *geo_json(JsonNode *json); JsonNode *geo_linestring(JsonNode *location_array);