Skip to content
Browse files

Merge branch 'master' of git://github.com/tjw/yajl

  • Loading branch information...
2 parents 22af413 + 934b982 commit af305526e77828621de645c4dd4c93f2d76b821b @lloyd committed Feb 26, 2009
Showing with 46 additions and 39 deletions.
  1. +25 −21 reformatter/json_reformat.c
  2. +21 −18 verify/json_verify.c
View
46 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);
}
}
View
39 verify/json_verify.c
@@ -56,7 +56,7 @@ main(int argc, char ** argv)
yajl_handle hand;
static unsigned char fileData[65536];
int quiet = 0;
- int retval = 0;
+ int retval = 0, done = 0;
yajl_parser_config cfg = { 0, 1 };
/* check arguments.*/
@@ -82,37 +82,40 @@ main(int argc, char ** argv)
/* allocate a parser */
hand = yajl_alloc(NULL, &cfg, NULL);
- for (;;) {
+ while (!done) {
rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
retval = 0;
if (rd == 0) {
- if (feof(stdin)) {
- break;
- } else {
+ if (!feof(stdin)) {
if (!quiet) {
fprintf(stderr, "error encountered on file read\n");
}
retval = 1;
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)
- {
- if (!quiet) {
- unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
- fprintf(stderr, (const char *) str);
- yajl_free_error(str);
- }
- retval = 1;
- break;
+
+ if (stat != yajl_status_ok &&
+ stat != yajl_status_insufficient_data)
+ {
+ if (!quiet) {
+ unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
+ fprintf(stderr, (const char *) str);
+ yajl_free_error(str);
}
+ retval = 1;
+ break;
}
}

0 comments on commit af30552

Please sign in to comment.
Something went wrong with that request. Please try again.