"Fossies" - the Fresh Open Source Software Archive

Member "yajl-2.1.0/verify/json_verify.c" (19 Mar 2014, 3452 Bytes) of package /linux/www/yajl-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "json_verify.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
    3  *
    4  * Permission to use, copy, modify, and/or distribute this software for any
    5  * purpose with or without fee is hereby granted, provided that the above
    6  * copyright notice and this permission notice appear in all copies.
    7  *
    8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   15  */
   16 
   17 #include <yajl/yajl_parse.h>
   18 
   19 #include <stdio.h>
   20 #include <stdlib.h>
   21 #include <string.h>
   22 
   23 static void
   24 usage(const char * progname)
   25 {
   26     fprintf(stderr, "%s: validate json from stdin\n"
   27                     "usage: json_verify [options]\n"
   28                     "    -c allow comments\n"
   29                     "    -q quiet mode\n"
   30                     "    -s verify a stream of multiple json entities\n"
   31                     "    -u allow invalid utf8 inside strings\n",
   32             progname);
   33     exit(1);
   34 }
   35 
   36 int
   37 main(int argc, char ** argv)
   38 {
   39     yajl_status stat;
   40     size_t rd;
   41     yajl_handle hand;
   42     static unsigned char fileData[65536];
   43     int quiet = 0;
   44     int retval = 0;
   45     int a = 1;
   46 
   47     /* allocate a parser */
   48     hand = yajl_alloc(NULL, NULL, NULL);
   49 
   50     /* check arguments.*/
   51     while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) {
   52         unsigned int i;
   53         for ( i=1; i < strlen(argv[a]); i++) {
   54             switch (argv[a][i]) {
   55                 case 'q':
   56                     quiet = 1;
   57                     break;
   58                 case 'c':
   59                     yajl_config(hand, yajl_allow_comments, 1);
   60                     break;
   61                 case 'u':
   62                     yajl_config(hand, yajl_dont_validate_strings, 1);
   63                     break;
   64                 case 's':
   65                     yajl_config(hand, yajl_allow_multiple_values, 1);
   66                     break;
   67                 default:
   68                     fprintf(stderr, "unrecognized option: '%c'\n\n", argv[a][i]);
   69                     usage(argv[0]);
   70             }
   71         }
   72         ++a;
   73     }
   74     if (a < argc) {
   75         usage(argv[0]);
   76     }
   77 
   78     for (;;) {
   79         rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
   80 
   81         retval = 0;
   82 
   83         if (rd == 0) {
   84             if (!feof(stdin)) {
   85                 if (!quiet) {
   86                     fprintf(stderr, "error encountered on file read\n");
   87                 }
   88                 retval = 1;
   89             }
   90             break;
   91         }
   92         fileData[rd] = 0;
   93 
   94         /* read file data, pass to parser */
   95         stat = yajl_parse(hand, fileData, rd);
   96 
   97         if (stat != yajl_status_ok) break;
   98     }
   99 
  100     /* parse any remaining buffered data */
  101     stat = yajl_complete_parse(hand);
  102 
  103     if (stat != yajl_status_ok)
  104     {
  105         if (!quiet) {
  106             unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
  107             fprintf(stderr, "%s", (const char *) str);
  108             yajl_free_error(hand, str);
  109         }
  110         retval = 1;
  111     }
  112 
  113     yajl_free(hand);
  114 
  115     if (!quiet) {
  116         printf("JSON is %s\n", retval ? "invalid" : "valid");
  117     }
  118 
  119     return retval;
  120 }