diff options
author | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-21 09:36:26 -0600 |
---|---|---|
committer | Lloyd Hilaiel <lloyd@hilaiel.com> | 2011-04-21 13:55:45 -0600 |
commit | 0858bb341280956947df1cfae33eb5ac6ecfe6f7 (patch) | |
tree | 2f61e004f7a892f00f6f34d38a8a194618400b59 /reformatter | |
parent | 6aefdaab38d81f33c46e33353c5a156884ea022d (diff) | |
download | yajl-0858bb341280956947df1cfae33eb5ac6ecfe6f7.tar.gz |
rework programmatic configuration of yajl. both generator and parser now have a yajl_XXX_config() function that accepts varargs so that configuration is simple, and new config options can be added in the future that preserve backwards binary compatibility. closes #23.
Diffstat (limited to 'reformatter')
-rw-r--r-- | reformatter/json_reformat.c | 65 |
1 files changed, 32 insertions, 33 deletions
diff --git a/reformatter/json_reformat.c b/reformatter/json_reformat.c index 070ca46..ddcab01 100644 --- a/reformatter/json_reformat.c +++ b/reformatter/json_reformat.c @@ -119,13 +119,18 @@ main(int argc, char ** argv) yajl_handle hand; static unsigned char fileData[65536]; /* generator config */ - yajl_gen_config conf = { 1, " " }; - yajl_gen g; + yajl_gen g; yajl_status stat; size_t rd; - /* allow comments */ - yajl_parser_config cfg = { 1, 1 }; - int retval = 0, done = 0; + int retval = 0; + + g = yajl_gen_alloc(NULL); + yajl_gen_config(g, yajl_gen_beautify, 1); + + /* ok. open file. let's read and parse */ + hand = yajl_alloc(&callbacks, NULL, (void *) g); + /* and let's allow comments by default */ + yajl_config(hand, yajl_allow_comments, 1); /* check arguments.*/ int a = 1; @@ -134,10 +139,10 @@ main(int argc, char ** argv) for ( i=1; i < strlen(argv[a]); i++) { switch (argv[a][i]) { case 'm': - conf.beautify = 0; + yajl_gen_config(g, yajl_gen_beautify, 0); break; case 'u': - cfg.checkUTF8 = 0; + yajl_config(hand, yajl_dont_validate_strings, 1); break; default: fprintf(stderr, "unrecognized option: '%c'\n\n", argv[a][i]); @@ -149,41 +154,26 @@ main(int argc, char ** argv) if (a < argc) { usage(argv[0]); } - - g = yajl_gen_alloc(&conf, NULL); - /* ok. open file. let's read and parse */ - hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g); - - while (!done) { + + + for (;;) { rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin); - + if (rd == 0) { if (!feof(stdin)) { fprintf(stderr, "error on file read.\n"); retval = 1; - break; } - done = 1; + break; } 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) + + stat = yajl_parse(hand, fileData, rd); + + if (stat != yajl_status_ok) break; + { - unsigned char * str = yajl_get_error(hand, 1, fileData, rd); - fprintf(stderr, "%s", (const char *) str); - yajl_free_error(hand, str); - retval = 1; - break; - } else { const unsigned char * buf; size_t len; yajl_gen_get_buf(g, &buf, &len); @@ -192,8 +182,17 @@ main(int argc, char ** argv) } } + stat = yajl_parse_complete(hand); + + if (stat != yajl_status_ok) { + unsigned char * str = yajl_get_error(hand, 1, fileData, rd); + fprintf(stderr, "%s", (const char *) str); + yajl_free_error(hand, str); + retval = 1; + } + yajl_gen_free(g); yajl_free(hand); - + return retval; } |