json_extract.c
Go to the documentation of this file.
00001 #include <options/options.h>
00002 #include "../csm/csm_all.h"
00003 
00004 int main(int argc, const char * argv[]) {
00005         sm_set_program_name(argv[0]);
00006         
00007         int nth;
00008         const char*input_filename;
00009         const char*output_filename;
00010         
00011         struct option* ops = options_allocate(3);
00012         options_int(ops, "nth", &nth, 0, "Index of object to extract.");
00013         options_string(ops, "in", &input_filename, "stdin", "input file (JSON)");
00014         options_string(ops, "out", &output_filename, "stdout", "output file (JSON)");
00015         
00016         if(!options_parse_args(ops, argc, argv)) {
00017                 fprintf(stderr, "%s : extracts n-th JSON object from stream."
00018                         "\n\nOptions:\n", argv[0]);
00019                 options_print_help(ops, stderr);
00020                 return -1;
00021         }
00022         
00023         FILE * input_stream = open_file_for_reading(input_filename);
00024         FILE *output_stream = open_file_for_writing(output_filename);
00025         
00026         if(!input_stream || !output_stream) return -1;
00027         
00028         int i; for(i=0;i<nth;i++) {
00029                 if(!json_stream_skip(input_stream)) {
00030                         sm_error("Could not skip %d-th object\n", i);
00031                         return -2;
00032                 }
00033         }
00034         
00035         JO jo = json_read_stream(input_stream);
00036         if(!jo) {
00037                 fprintf(stderr, "Could not read %d-th object (after skipping %d)\n", 
00038                         nth, i);
00039                 return -2;
00040         }
00041         
00042         fputs(json_object_to_json_string(jo), output_stream);
00043         fputs("\n", output_stream);
00044         return 0;
00045 }


csm
Author(s): Andrea Censi
autogenerated on Fri May 17 2019 02:28:33