json_split.c
Go to the documentation of this file.
00001 #include <options/options.h>
00002 #include <string.h>
00003 #include "../csm/csm_all.h"
00004 
00005 int main(int argc, const char * argv[]) {
00006         sm_set_program_name(argv[0]);
00007         
00008         const char*input_filename;
00009         const char*output_pattern_op;
00010         
00011         struct option* ops = options_allocate(3);
00012         options_string(ops, "in", &input_filename, "stdin", "input file (JSON)");
00013         options_string(ops, "out", &output_pattern_op, "./ld_split^02d.json", "output pattern; printf() pattern, but write '^' instead of '%'");
00014         
00015         if(!options_parse_args(ops, argc, argv)) {
00016                 fprintf(stderr, "%s : splits a JSON file into many files."
00017                         "\n\nOptions:\n", argv[0]);
00018                 options_print_help(ops, stderr);
00019                 return -1;
00020         }
00021         
00022         
00023         /* Substitute "$" with "%" */
00024         
00025         char output_pattern[256]; strcpy(output_pattern, output_pattern_op);
00026         char *f = output_pattern;
00027         while(*f) {
00028                 if(*f=='^') *f='%';
00029                 f++;
00030         }
00031 
00032         fputs(output_pattern, stderr);
00033         
00034         FILE * input_stream = open_file_for_reading(input_filename);
00035 
00036         int count = 0;
00037         
00038         JO jo;
00039         while( (jo = json_read_stream(input_stream)) ) {
00040                 char filename[1000];
00041                 sprintf(filename, output_pattern, count);
00042                 if(!count) {
00043                         
00044                 }
00045                 
00046                 sm_debug("Writing to file (%s) %s\n", output_pattern, filename);
00047                 FILE * f = open_file_for_writing(filename);
00048                 if(!f) return -1;
00049                 fputs(json_object_to_json_string(jo), f);
00050                 jo_free(jo);
00051                 fclose(f);
00052                 
00053                 count++;
00054         }
00055         
00056         
00057         return 0;
00058 }


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