diff options
author | Timothy J. Wood <tjw@omnigroup.com> | 2009-02-26 15:58:13 -0800 |
---|---|---|
committer | Timothy J. Wood <tjw@omnigroup.com> | 2009-02-26 15:58:13 -0800 |
commit | 934b9827046e0d31689996a13faf1635ffce088a (patch) | |
tree | 43450859750e3ec994718d9c3894fe510168e0cb /reformatter | |
parent | f9dbf2a27b46edc28c0e5df19d7a1bb25488f32e (diff) | |
download | yajl-934b9827046e0d31689996a13faf1635ffce088a.tar.gz |
Update json_reformat and json_verify to use the new yajl_parse_complete()
at the end of their input.
Diffstat (limited to 'reformatter')
-rw-r--r-- | reformatter/json_reformat.c | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/reformatter/json_reformat.c b/reformatter/json_reformat.c index 01a30c0..b1c2ee7 100644 --- a/reformatter/json_reformat.c +++ b/reformatter/json_reformat.c @@ -140,7 +140,8 @@ main(int argc, char ** argv) size_t rd; /* allow comments */ yajl_parser_config cfg = { 1, 1 }; - + int done = 0; + /* check arguments. We expect exactly one! */ if (argc == 2) { if (!strcmp("-m", argv[1])) { @@ -160,34 +161,37 @@ main(int argc, char ** argv) /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, &cfg, (void *) g); - for (;;) { + while (!done) { rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin); if (rd == 0) { - if (feof(stdin)) { - break; - } else { + if (!feof(stdin)) { fprintf(stderr, "error on file read.\n"); break; } - } else { - fileData[rd] = 0; - + done = 1; + } + fileData[rd] = 0; + + if (done) + /* parse any remaining buffered data */ + stat = yajl_parse_complete(hand); + else /* read file data, pass to parser */ stat = yajl_parse(hand, fileData, rd); - if (stat != yajl_status_ok && - stat != yajl_status_insufficient_data) - { - unsigned char * str = yajl_get_error(hand, 1, fileData, rd); - fprintf(stderr, (const char *) str); - yajl_free_error(str); - } else { - const unsigned char * buf; - unsigned int len; - yajl_gen_get_buf(g, &buf, &len); - fwrite(buf, 1, len, stdout); - yajl_gen_clear(g); - } + + if (stat != yajl_status_ok && + stat != yajl_status_insufficient_data) + { + unsigned char * str = yajl_get_error(hand, 1, fileData, rd); + fprintf(stderr, (const char *) str); + yajl_free_error(str); + } else { + const unsigned char * buf; + unsigned int len; + yajl_gen_get_buf(g, &buf, &len); + fwrite(buf, 1, len, stdout); + yajl_gen_clear(g); } } |