json_decimate.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 period; int phase;
00008         const char*input_filename;
00009         const char*output_filename;
00010         
00011         struct option* ops = options_allocate(3);
00012         options_int(ops, "period", &period, 1, "Period of objects to extract.");
00013         options_int(ops, "phase", &phase, 0, "Phase (=0 starts with the first object)");
00014         options_string(ops, "in", &input_filename, "stdin", "input file (JSON)");
00015         options_string(ops, "out", &output_filename, "stdout", "output file (JSON)");
00016         
00017         if(!options_parse_args(ops, argc, argv)) {
00018                 fprintf(stderr, "%s : decimates a JSON stream."
00019                         "\n\nOptions:\n", argv[0]);
00020                 options_print_help(ops, stderr);
00021                 return -1;
00022         }
00023         
00024         if(period < 1) {
00025                 sm_error("Period must be >= 1.\n");
00026                 return 2;
00027         }
00028         
00029         FILE * input_stream = open_file_for_reading(input_filename);
00030         FILE *output_stream = open_file_for_writing(output_filename);
00031         
00032         if(!input_stream || !output_stream) return -1;
00033         
00034         
00035         int count = 0;
00036         while(1) { 
00037                 JO jo = json_read_stream(input_stream);
00038                 if(!jo) {
00039                         if(feof(input_stream)) break;
00040                         sm_error("Malformed JSON\n");
00041                         return -1;
00042                 }
00043                 
00044                 if( (count - phase) % period == 0) {
00045                         const char * s = json_object_to_json_string(jo);
00046                         fputs(s,output_stream); fputs("\n",output_stream);
00047                 } 
00048                 
00049                 jo_free(jo);
00050                 count++;
00051         }
00052         
00053         return 0;
00054 }


csm
Author(s): Andrea Censi
autogenerated on Mon Jan 16 2017 03:48:29