From 9979c70b1fa5238a09902d9a61f1a19e2675a039 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Fri, 7 Feb 2014 17:02:33 -0800 Subject: [PATCH] Add the whole-tree reader --- jsoncat.c | 26 +++++++++++++++++++++++--- jsonpull.c | 16 ++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/jsoncat.c b/jsoncat.c index d3f3b8d0..e5a5e6dd 100644 --- a/jsoncat.c +++ b/jsoncat.c @@ -122,7 +122,7 @@ void process_callback(FILE *f, char *fname) { } } -void process(FILE *f, char *fname) { +void process_incremental(FILE *f, char *fname) { json_pull *jp = json_begin_file(f); json_object *j; @@ -139,9 +139,29 @@ void process(FILE *f, char *fname) { } } +void process_tree(FILE *f, char *fname) { + json_pull *jp = json_begin_file(f); + + while (1) { + json_object *j = json_read_tree(jp); + + if (j == NULL) { + if (jp->error != NULL) { + fprintf(stderr, "%s: %d: %s\n", fname, jp->line, jp->error); + } + + break; + } + + json_print(j, 0); + json_free(j); + printf("\n"); + } +} + int main(int argc, char **argv) { if (argc == 1) { - process(stdin, "standard input"); + process_tree(stdin, "standard input"); } else { int i; for (i = 1; i < argc; i++) { @@ -150,7 +170,7 @@ int main(int argc, char **argv) { perror(argv[i]); exit(EXIT_FAILURE); } - process(f, argv[i]); + process_tree(f, argv[i]); fclose(f); } } diff --git a/jsonpull.c b/jsonpull.c index aa2c8ed8..8a62019b 100644 --- a/jsonpull.c +++ b/jsonpull.c @@ -55,6 +55,10 @@ json_pull *json_begin_string(char *s) { return json_begin(read_string, peek_string, s); } +void json_end(json_pull *p) { + free(p); +} + static int read_wrap(json_pull *j) { int c = j->read(j); @@ -475,6 +479,18 @@ json_object *json_read(json_pull *j) { return json_read_with_separators(j, NULL, NULL); } +json_object *json_read_tree(json_pull *p) { + json_object *j; + + while ((j = json_read(p)) != NULL) { + if (j->parent == NULL) { + return j; + } + } + + return NULL; +} + void json_free(json_object *o) { int i;