1 #include "lib/mlrutil.h"
2 #include "lib/mlr_globals.h"
3 #include "output/lrec_writers.h"
4 
lrec_writer_alloc(cli_writer_opts_t * popts)5 lrec_writer_t*  lrec_writer_alloc(cli_writer_opts_t* popts) {
6 	if (streq(popts->ofile_fmt, "dkvp")) {
7 		return lrec_writer_dkvp_alloc(popts->ors, popts->ofs, popts->ops);
8 
9 	} else if (streq(popts->ofile_fmt, "json")) {
10 		return lrec_writer_json_alloc(popts->stack_json_output_vertically,
11 			popts->wrap_json_output_in_outer_list, popts->json_quote_int_keys,
12 			popts->json_quote_non_string_values,
13 			popts->output_json_flatten_separator, popts->ors);
14 
15 	} else if (streq(popts->ofile_fmt, "csv")) {
16 		return lrec_writer_csv_alloc(popts->ors, popts->ofs, popts->oquoting,
17 			popts->headerless_csv_output);
18 
19 	} else if (streq(popts->ofile_fmt, "csvlite")) {
20 		return lrec_writer_csvlite_alloc(popts->ors, popts->ofs, popts->headerless_csv_output);
21 
22 	} else if (streq(popts->ofile_fmt, "markdown")) {
23 		return lrec_writer_markdown_alloc(popts->ors);
24 
25 	} else if (streq(popts->ofile_fmt, "nidx")) {
26 		return lrec_writer_nidx_alloc(popts->ors, popts->ofs);
27 
28 	} else if (streq(popts->ofile_fmt, "xtab")) {
29 		return lrec_writer_xtab_alloc(popts->ofs, popts->ops, popts->right_justify_xtab_value);
30 
31 	} else if (streq(popts->ofile_fmt, "pprint")) {
32 		if (strlen(popts->ofs) != 1) {
33 			fprintf(stderr, "%s: OFS for PPRINT format must be single-character; got \"%s\".\n",
34 				MLR_GLOBALS.bargv0, popts->ofs);
35 			return NULL;
36 		} else {
37 			return lrec_writer_pprint_alloc(popts->ors, popts->ofs[0], popts->right_align_pprint,
38 				popts->pprint_barred, popts->headerless_csv_output);
39 		}
40 
41 	} else {
42 		return NULL;
43 	}
44 }
45 
46 // ----------------------------------------------------------------
lrec_writer_alloc_or_die(cli_writer_opts_t * popts)47 lrec_writer_t* lrec_writer_alloc_or_die(cli_writer_opts_t* popts) {
48 	lrec_writer_t* plrec_writer = lrec_writer_alloc(popts);
49 	MLR_INTERNAL_CODING_ERROR_IF(plrec_writer == NULL);
50 	return plrec_writer;
51 }
52 
53 // ----------------------------------------------------------------
lrec_writer_print_all(lrec_writer_t * pwriter,FILE * fp,sllv_t * poutrecs,context_t * pctx)54 void lrec_writer_print_all(lrec_writer_t* pwriter, FILE* fp, sllv_t* poutrecs, context_t* pctx) {
55 	while (poutrecs->phead != NULL) {
56 		lrec_t* poutrec = sllv_pop(poutrecs);
57 		pwriter->pprocess_func(pwriter->pvstate, fp, poutrec, pctx);
58 	}
59 }
60