json_split.c
Go to the documentation of this file.
1 #include <options/options.h>
2 #include <string.h>
3 #include "../csm/csm_all.h"
4 
5 int main(int argc, const char * argv[]) {
6  sm_set_program_name(argv[0]);
7 
8  const char*input_filename;
9  const char*output_pattern_op;
10 
11  struct option* ops = options_allocate(3);
12  options_string(ops, "in", &input_filename, "stdin", "input file (JSON)");
13  options_string(ops, "out", &output_pattern_op, "./ld_split^02d.json", "output pattern; printf() pattern, but write '^' instead of '%'");
14 
15  if(!options_parse_args(ops, argc, argv)) {
16  fprintf(stderr, "%s : splits a JSON file into many files."
17  "\n\nOptions:\n", argv[0]);
18  options_print_help(ops, stderr);
19  return -1;
20  }
21 
22 
23  /* Substitute "$" with "%" */
24 
25  char output_pattern[256]; strcpy(output_pattern, output_pattern_op);
26  char *f = output_pattern;
27  while(*f) {
28  if(*f=='^') *f='%';
29  f++;
30  }
31 
32  fputs(output_pattern, stderr);
33 
34  FILE * input_stream = open_file_for_reading(input_filename);
35 
36  int count = 0;
37 
38  JO jo;
39  while( (jo = json_read_stream(input_stream)) ) {
40  char filename[1000];
41  sprintf(filename, output_pattern, count);
42  if(!count) {
43 
44  }
45 
46  sm_debug("Writing to file (%s) %s\n", output_pattern, filename);
47  FILE * f = open_file_for_writing(filename);
48  if(!f) return -1;
49  fputs(json_object_to_json_string(jo), f);
50  jo_free(jo);
51  fclose(f);
52 
53  count++;
54  }
55 
56 
57  return 0;
58 }
void sm_set_program_name(const char *name)
Definition: logging.c:21
struct option * ops
Definition: rb_sm.c:31
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
Definition: options.h:49
struct option * options_allocate(int n)
const char * json_object_to_json_string(struct json_object *this)
Definition: json_object.c:199
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
void sm_debug(const char *msg,...)
Definition: logging.c:88
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
JO json_read_stream(FILE *f)
int main(int argc, const char *argv[])
Definition: json_split.c:5
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
#define jo_free


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23