1 #include "cli/comment_handling.h"
2 #include "lib/mlrutil.h"
3 #include "lib/mlr_globals.h"
4 #include "input/lrec_readers.h"
5 #include "input/byte_readers.h"
6
lrec_reader_alloc(cli_reader_opts_t * popts)7 lrec_reader_t* lrec_reader_alloc(cli_reader_opts_t* popts) {
8 if (streq(popts->ifile_fmt, "gen")) {
9 generator_opts_t* pgopts = &popts->generator_opts;
10 return lrec_reader_gen_alloc(pgopts->field_name, pgopts->start, pgopts->stop, pgopts->step);
11 } else if (streq(popts->ifile_fmt, "dkvp")) {
12 return lrec_reader_stdio_dkvp_alloc(popts->irs, popts->ifs, popts->ips, popts->allow_repeat_ifs,
13 popts->comment_handling, popts->comment_string);
14 } else if (streq(popts->ifile_fmt, "csv")) {
15 return lrec_reader_stdio_csv_alloc(popts->irs, popts->ifs, popts->use_implicit_csv_header,
16 popts->allow_ragged_csv_input, popts->comment_handling, popts->comment_string);
17 } else if (streq(popts->ifile_fmt, "csvlite")) {
18 return lrec_reader_stdio_csvlite_alloc(popts->irs, popts->ifs, popts->allow_repeat_ifs,
19 popts->use_implicit_csv_header, popts->allow_ragged_csv_input, popts->comment_handling,
20 popts->comment_string);
21 } else if (streq(popts->ifile_fmt, "nidx")) {
22 return lrec_reader_stdio_nidx_alloc(popts->irs, popts->ifs, popts->allow_repeat_ifs,
23 popts->comment_handling, popts->comment_string);
24 } else if (streq(popts->ifile_fmt, "xtab")) {
25 return lrec_reader_stdio_xtab_alloc(popts->ifs, popts->ips, popts->allow_repeat_ips,
26 popts->comment_handling, popts->comment_string);
27 } else if (streq(popts->ifile_fmt, "json")) {
28 return lrec_reader_stdio_json_alloc(popts->input_json_flatten_separator,
29 popts->json_array_ingest, popts->irs, popts->comment_handling, popts->comment_string);
30 } else {
31 return NULL;
32 }
33 }
34
lrec_reader_alloc_or_die(cli_reader_opts_t * popts)35 lrec_reader_t* lrec_reader_alloc_or_die(cli_reader_opts_t* popts) {
36 lrec_reader_t* plrec_reader = lrec_reader_alloc(popts);
37 if (plrec_reader == NULL) {
38 fprintf(stderr, "%s: unrecognized input-file format \"%s\".\n",
39 MLR_GLOBALS.bargv0, popts->ifile_fmt);
40 exit(1);
41 }
42 return plrec_reader;
43 }
44