fix limit in locations api

This commit is contained in:
Jan-Piet Mens 2016-02-09 18:21:03 +01:00
parent de60beb5d6
commit 6523294416
3 changed files with 30 additions and 2 deletions

6
http.c
View File

@ -443,6 +443,7 @@ static int dispatch(struct mg_connection *conn, const char *uri)
if ((json = lister(u, d, s_lo, s_hi, (limit > 0) ? TRUE : FALSE)) != NULL) {
JsonNode *arr, *fields = NULL;
char *flds = field(conn, "fields");
int i_have = 0;
if (flds != NULL) {
fields = json_splitter(flds, ",");
@ -455,6 +456,11 @@ static int dispatch(struct mg_connection *conn, const char *uri)
JsonNode *f;
json_foreach(f, arr) {
locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, fields, NULL, NULL);
if (limit) {
i_have += limit;
if (i_have >= limit)
break;
}
// printf("%s\n", f->string_);
}
}

7
ocat.c
View File

@ -462,10 +462,17 @@ int main(int argc, char **argv)
*/
if ((json = lister(username, device, s_lo, s_hi, (limit > 0) ? TRUE : FALSE)) != NULL) {
int i_have = 0;
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, username, device);
locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, fields, NULL, NULL);
if (limit) {
i_have += limit;
if (i_have >= limit)
break;
}
// printf("%s\n", f->string_);
}
}

View File

@ -722,10 +722,25 @@ static int candidate_line(char *line, void *param)
}
}
}
if (limit > 0 && otype == RAW) {
} else if (limit > 0 && otype == RAW) {
printf("%s\n", line);
return (1); /* make it 'count' or tac() will not decrement line counter and continue until EOF */
} else if (limit > 0) {
/* reading backwards; check time */
char *p;
struct tm tmline;
time_t secs;
if ((p = strptime(line, "%Y-%m-%dT%H:%M:%SZ", &tmline)) == NULL) {
fprintf(stderr, "no strptime on %s", line);
return (0);
}
secs = mktime(&tmline);
if (secs <= s_lo || secs >= s_hi) {
return (0);
}
}
/* Do we have location line? */