json_decimate.c
Go to the documentation of this file.
1 #include <options/options.h>
2 #include "../csm/csm_all.h"
3 
4 int main(int argc, const char * argv[]) {
5  sm_set_program_name(argv[0]);
6 
7  int period; int phase;
8  const char*input_filename;
9  const char*output_filename;
10 
11  struct option* ops = options_allocate(3);
12  options_int(ops, "period", &period, 1, "Period of objects to extract.");
13  options_int(ops, "phase", &phase, 0, "Phase (=0 starts with the first object)");
14  options_string(ops, "in", &input_filename, "stdin", "input file (JSON)");
15  options_string(ops, "out", &output_filename, "stdout", "output file (JSON)");
16 
17  if(!options_parse_args(ops, argc, argv)) {
18  fprintf(stderr, "%s : decimates a JSON stream."
19  "\n\nOptions:\n", argv[0]);
20  options_print_help(ops, stderr);
21  return -1;
22  }
23 
24  if(period < 1) {
25  sm_error("Period must be >= 1.\n");
26  return 2;
27  }
28 
29  FILE * input_stream = open_file_for_reading(input_filename);
30  FILE *output_stream = open_file_for_writing(output_filename);
31 
32  if(!input_stream || !output_stream) return -1;
33 
34 
35  int count = 0;
36  while(1) {
37  JO jo = json_read_stream(input_stream);
38  if(!jo) {
39  if(feof(input_stream)) break;
40  sm_error("Malformed JSON\n");
41  return -1;
42  }
43 
44  if( (count - phase) % period == 0) {
45  const char * s = json_object_to_json_string(jo);
46  fputs(s,output_stream); fputs("\n",output_stream);
47  }
48 
49  jo_free(jo);
50  count++;
51  }
52 
53  return 0;
54 }
open_file_for_writing
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
options.h
sm_set_program_name
void sm_set_program_name(const char *name)
Definition: logging.c:21
main
int main(int argc, const char *argv[])
Definition: json_decimate.c:4
jo_free
#define jo_free
Definition: json_more_utils.h:16
options_allocate
struct option * options_allocate(int n)
Definition: options_interface.c:16
options_int
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
Definition: options_interface.c:39
json_read_stream
JO json_read_stream(FILE *f)
Definition: json_more_utils.c:48
open_file_for_reading
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
ops
struct option * ops
Definition: rb_sm.c:31
option
Definition: options.h:49
options_print_help
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
sm_error
void sm_error(const char *msg,...)
Definition: logging.c:49
json_object
Definition: json_object_private.h:21
options_string
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
Definition: options_interface.c:72
options_parse_args
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
json_object_to_json_string
const char * json_object_to_json_string(struct json_object *this)
Definition: json_object.c:199


csm
Author(s): Andrea Censi
autogenerated on Wed Aug 17 2022 02:50:33