json_extract.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 nth;
8  const char*input_filename;
9  const char*output_filename;
10 
11  struct option* ops = options_allocate(3);
12  options_int(ops, "nth", &nth, 0, "Index of object to extract.");
13  options_string(ops, "in", &input_filename, "stdin", "input file (JSON)");
14  options_string(ops, "out", &output_filename, "stdout", "output file (JSON)");
15 
16  if(!options_parse_args(ops, argc, argv)) {
17  fprintf(stderr, "%s : extracts n-th JSON object from stream."
18  "\n\nOptions:\n", argv[0]);
19  options_print_help(ops, stderr);
20  return -1;
21  }
22 
23  FILE * input_stream = open_file_for_reading(input_filename);
24  FILE *output_stream = open_file_for_writing(output_filename);
25 
26  if(!input_stream || !output_stream) return -1;
27 
28  int i; for(i=0;i<nth;i++) {
29  if(!json_stream_skip(input_stream)) {
30  sm_error("Could not skip %d-th object\n", i);
31  return -2;
32  }
33  }
34 
35  JO jo = json_read_stream(input_stream);
36  if(!jo) {
37  fprintf(stderr, "Could not read %d-th object (after skipping %d)\n",
38  nth, i);
39  return -2;
40  }
41 
42  fputs(json_object_to_json_string(jo), output_stream);
43  fputs("\n", output_stream);
44  return 0;
45 }
void sm_set_program_name(const char *name)
Definition: logging.c:21
struct option * ops
Definition: rb_sm.c:31
int json_stream_skip(FILE *f)
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
int main(int argc, const char *argv[])
Definition: json_extract.c:4
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
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
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 options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
JO json_read_stream(FILE *f)
void sm_error(const char *msg,...)
Definition: logging.c:49
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66


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