Add the whole-tree reader

This commit is contained in:
Eric Fischer 2014-02-07 17:02:33 -08:00
parent 0fd66048c8
commit 9979c70b1f
2 changed files with 39 additions and 3 deletions

View File

@ -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);
}
}

View File

@ -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;