2 * UCW Library -- Table printer
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
8 #include <ucw/string.h>
9 #include <ucw/stkstring.h>
11 #include <ucw/table.h>
12 #include <ucw/strtonum.h>
17 /* Forward declarations */
19 static void table_update_ll(struct table *tbl);
21 /*** Management of tables ***/
23 static struct table *table_make_instance(const struct table_template *tbl_template)
25 struct mempool *pool = mp_new(4096);
26 struct table *new_inst = mp_alloc_zero(pool, sizeof(struct table));
28 new_inst->pool = pool;
30 // initialize column definitions
31 uint col_count = 0; // count the number of columns in the struct table
33 if(tbl_template->columns[col_count].name == NULL &&
34 tbl_template->columns[col_count].width == 0 &&
35 tbl_template->columns[col_count].type_def == COL_TYPE_ANY)
37 ASSERT(tbl_template->columns[col_count].name != NULL);
38 ASSERT(tbl_template->columns[col_count].width != 0);
42 new_inst->column_count = col_count;
44 new_inst->columns = tbl_template->columns;
45 new_inst->ll_headers = mp_alloc(new_inst->pool, sizeof(int) * col_count);
46 for(uint i = 0; i < col_count; i++) {
47 new_inst->ll_headers[i] = -1;
50 // initialize column_order
51 if(tbl_template->column_order) {
52 new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
53 memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
54 for(uint i = 0; i < new_inst->cols_to_output; i++) {
55 new_inst->column_order[i].cell_content = NULL;
56 int col_idx = new_inst->column_order[i].idx;
57 new_inst->column_order[i].col_def = new_inst->columns + col_idx;
58 new_inst->column_order[i].output_type = tbl_template->columns[col_idx].fmt;
61 new_inst->cols_to_output = tbl_template->cols_to_output;
64 new_inst->col_delimiter = tbl_template->col_delimiter;
65 new_inst->print_header = 1;
67 new_inst->last_printed_col = -1;
68 new_inst->row_printing_started = 0;
69 new_inst->col_out = -1;
70 new_inst->formatter = tbl_template->formatter;
71 new_inst->data = NULL;
75 struct table *table_init(const struct table_template *tbl_template)
77 struct table *tbl = table_make_instance(tbl_template);
80 tbl->formatter = &table_fmt_human_readable;
83 tbl->print_header = 1; // by default, print header
87 void table_cleanup(struct table *tbl)
92 // TODO: test default column order
93 static void table_make_default_column_order(struct table *tbl)
95 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count); // FIXME: use stack instead of memory pool
96 for(int i = 0; i < tbl->column_count; i++) {
99 table_set_col_order(tbl, col_order_int, tbl->column_count);
102 void table_start(struct table *tbl, struct fastbuf *out)
104 tbl->last_printed_col = -1;
105 tbl->row_printing_started = 0;
108 ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
110 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
112 // update linked lists
113 table_update_ll(tbl);
115 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
117 mp_save(tbl->pool, &tbl->pool_state);
119 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
122 void table_end(struct table *tbl)
124 tbl->last_printed_col = -1;
125 tbl->row_printing_started = 0;
127 mp_restore(tbl->pool, &tbl->pool_state);
129 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
132 /*** Configuration ***/
134 void table_set_formatter(struct table *tbl, const struct table_formatter *fmt)
136 tbl->formatter = fmt;
139 int table_get_col_idx(struct table *tbl, const char *col_name)
141 for(int i = 0; i < tbl->column_count; i++) {
142 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
147 const char * table_get_col_list(struct table *tbl)
149 if(tbl->column_count == 0) return "";
151 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
153 for(int i = 1; i < tbl->column_count; i++) {
154 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
160 static void table_update_ll(struct table *tbl)
162 int cols_to_output = tbl->cols_to_output;
164 for(int i = 0; i < tbl->column_count; i++) {
165 tbl->ll_headers[i] = -1;
168 for(int i = 0; i < cols_to_output; i++) {
169 int idx = tbl->column_order[i].idx;
170 tbl->column_order[i].col_def = tbl->columns + idx;
173 for(int i = 0; i < cols_to_output; i++) {
174 int col_def_idx = tbl->column_order[i].idx;
175 int first = tbl->ll_headers[col_def_idx];
176 tbl->ll_headers[col_def_idx] = i;
177 tbl->column_order[i].next_column = first;
181 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
183 for(int i = 0; i < cols_to_output; i++) {
184 ASSERT_MSG(col_order[i] >= 0 && col_order[i] < tbl->column_count, "Column %d does not exist (column number should be between 0 and %d).", col_order[i], tbl->column_count - 1);
187 tbl->cols_to_output = cols_to_output;
188 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * cols_to_output);
189 for(int i = 0; i < cols_to_output; i++) {
190 int col_idx = col_order[i];
191 tbl->column_order[i].idx = col_idx;
192 tbl->column_order[i].col_def = tbl->columns + col_idx;
193 tbl->column_order[i].cell_content = NULL;
194 tbl->column_order[i].output_type = tbl->columns[col_idx].fmt;
196 table_update_ll(tbl);
199 bool table_col_is_printed(struct table *tbl, uint col_idx)
201 if(tbl->ll_headers[col_idx] == -1) return 0;
206 static char * table_parse_col_arg(char *col_def)
208 // FIXME: should be switched to str_sepsplit
209 char * left_br = strchr(col_def, '[');
210 if(left_br == NULL) return NULL;
213 char *right_br = strchr(left_br, ']');
219 * Setting options for basic table types (as defined in table.h)
221 int table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char **err)
223 const struct table_column *col_def = tbl->column_order[col_idx].col_def;
225 if(col_def->type_def == &xt_double) {
227 const char *tmp_err = str_to_uint(&precision, col_arg, NULL, 0);
229 *err = mp_printf(tbl->pool, "An error occured while parsing precision: %s.", tmp_err);
232 tbl->column_order[col_idx].output_type = precision; // FIXME: shift the value of precision
236 *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_idx);
241 * TODO: This function deliberately leaks memory. When it is called multiple times,
242 * previous column orders still remain allocated in the table's memory pool.
244 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
246 if(col_order_str[0] == '*') {
247 table_make_default_column_order(tbl);
251 if(!col_order_str[0]) {
252 tbl->column_order = mp_alloc(tbl->pool, 0);
253 tbl->cols_to_output = 0;
257 char *tmp_col_order = stk_strdup(col_order_str);
260 for(int i = 0; col_order_str[i] != 0; i++) {
261 if(col_order_str[i] == ',') {
266 tbl->cols_to_output = col_count;
267 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * col_count);
269 int curr_col_idx = 0;
270 char *name_start = tmp_col_order;
272 char *next = strchr(name_start, ',');
277 char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
278 int col_idx = table_get_col_idx(tbl, name_start);
281 return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
283 tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
284 tbl->column_order[curr_col_idx].idx = col_idx;
285 tbl->column_order[curr_col_idx].cell_content = NULL;
286 tbl->column_order[curr_col_idx].output_type = tbl->columns[col_idx].fmt;
287 if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_instance_option) {
289 tbl->columns[col_idx].set_col_instance_option(tbl, curr_col_idx, arg, &err);
290 if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
297 table_update_ll(tbl);
302 /*** Table cells ***/
304 static void table_set_all_inst_content(struct table *tbl, int col_templ, const char *col_content)
306 TBL_COL_ITER_START(tbl, col_templ, curr_col_ptr, curr_col) {
307 curr_col_ptr->cell_content = col_content;
311 void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type)
313 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
314 ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == tbl->columns[col].type_def);
315 tbl->last_printed_col = col;
316 tbl->row_printing_started = 1;
317 const char *cell_content = NULL;
318 TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
319 enum xtype_fmt fmt = curr_col->output_type;
320 cell_content = expected_type->format(value, fmt, tbl->pool);
321 curr_col->cell_content = cell_content;
325 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
327 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
328 tbl->last_printed_col = col;
329 tbl->row_printing_started = 1;
332 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
333 table_set_all_inst_content(tbl, col, cell_content);
337 TABLE_COL_BODY(int, int)
338 TABLE_COL_BODY(uint, uint)
339 TABLE_COL_BODY(double, double)
340 TABLE_COL_BODY(intmax, intmax_t)
341 TABLE_COL_BODY(uintmax, uintmax_t)
342 TABLE_COL_BODY(s64, s64)
343 TABLE_COL_BODY(u64, u64)
344 TABLE_COL_BODY(bool, bool)
346 void table_col_str(struct table *tbl, int col, const char *val) {
347 table_col_generic_format(tbl, col, (void*)val, &xt_str);
350 void table_reset_row(struct table *tbl)
352 for(uint i = 0; i < tbl->cols_to_output; i++) {
353 tbl->column_order[i].cell_content = NULL;
355 mp_restore(tbl->pool, &tbl->pool_state);
356 tbl->last_printed_col = -1;
357 tbl->row_printing_started = 0;
360 void table_end_row(struct table *tbl)
362 ASSERT(tbl->formatter->row_output);
363 if(tbl->row_printing_started == 0) return;
364 tbl->formatter->row_output(tbl);
365 table_reset_row(tbl);
368 /* Construction of a cell using a fastbuf */
370 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
372 fbpool_init(&tbl->fb_col_out);
373 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
375 return &tbl->fb_col_out.fb;
378 void table_col_fbend(struct table *tbl)
380 char *cell_content = fbpool_end(&tbl->fb_col_out);
381 table_set_all_inst_content(tbl, tbl->col_out, cell_content);
385 /*** Option parsing ***/
387 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
389 // Options with no value
390 if(value == NULL || (value != NULL && strlen(value) == 0)) {
391 if(strcmp(key, "noheader") == 0) {
392 tbl->print_header = 0;
397 // Options with a value
399 if(strcmp(key, "header") == 0) {
401 return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
402 uint tmp = value[0] - '0';
404 return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
405 tbl->print_header = tmp;
407 } else if(strcmp(key, "cols") == 0) {
408 return table_set_col_order_by_name(tbl, value);
409 } else if(strcmp(key, "fmt") == 0) {
410 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
411 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
412 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
414 return "Invalid argument to output-type option.";
417 } else if(strcmp(key, "cell-fmt") == 0) {
419 const char *err = xtype_parse_fmt(NULL, value, &fmt, tbl->pool);
420 if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
421 for(uint i = 0; i < tbl->cols_to_output; i++) {
422 tbl->column_order[i].output_type = fmt;
425 } else if(strcmp(key, "col-delim") == 0) {
426 char * d = mp_printf(tbl->pool, "%s", value);
427 tbl->col_delimiter = d;
433 if(tbl->formatter && tbl->formatter->process_option) {
434 const char *err = NULL;
435 if(tbl->formatter->process_option(tbl, key, value, &err)) {
440 // Unrecognized option
441 return mp_printf(tbl->pool, "Invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
444 const char *table_set_option(struct table *tbl, const char *opt)
446 char *key = stk_strdup(opt);
447 char *value = strchr(key, ':');
451 return table_set_option_value(tbl, key, value);
454 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
456 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
457 const char *rv = table_set_option(tbl, gary_table_opts[i]);
465 /*** Default formatter for human-readable output ***/
467 static void table_row_human_readable(struct table *tbl)
469 for(uint i = 0; i < tbl->cols_to_output; i++) {
470 const struct table_column *col_def = tbl->column_order[i].col_def;
472 bputs(tbl->out, tbl->col_delimiter);
474 int col_width = col_def->width & CELL_WIDTH_MASK;
475 if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
476 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
478 bputc(tbl->out, '\n');
481 static void table_write_header(struct table *tbl)
483 for(uint i = 0; i < tbl->cols_to_output; i++) {
484 const struct table_column *col_def = tbl->column_order[i].col_def;
486 bputs(tbl->out, tbl->col_delimiter);
488 int col_width = col_def->width & CELL_WIDTH_MASK;
489 if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
490 bprintf(tbl->out, "%*s", col_width, col_def->name);
492 bputc(tbl->out, '\n');
495 static void table_start_human_readable(struct table *tbl)
497 if(tbl->col_delimiter == NULL) {
498 tbl->col_delimiter = " ";
501 if(tbl->print_header != 0) {
502 table_write_header(tbl);
506 const struct table_formatter table_fmt_human_readable = {
507 .row_output = table_row_human_readable,
508 .table_start = table_start_human_readable,
511 /*** Default formatter for machine-readable output ***/
513 static void table_row_machine_readable(struct table *tbl)
515 for(uint i = 0; i < tbl->cols_to_output; i++) {
517 bputs(tbl->out, tbl->col_delimiter);
519 bputs(tbl->out, tbl->column_order[i].cell_content);
521 bputc(tbl->out, '\n');
524 static void table_start_machine_readable(struct table *tbl)
526 if(tbl->col_delimiter == NULL) {
527 tbl->col_delimiter = "\t";
530 if(tbl->print_header != 0 && tbl->cols_to_output > 0) {
531 bputs(tbl->out, tbl->column_order[0].col_def->name);
532 for(uint i = 1; i < tbl->cols_to_output; i++) {
533 bputs(tbl->out, tbl->col_delimiter);
534 bputs(tbl->out, tbl->column_order[i].col_def->name);
536 bputc(tbl->out, '\n');
540 const struct table_formatter table_fmt_machine_readable = {
541 .row_output = table_row_machine_readable,
542 .table_start = table_start_machine_readable,
546 /*** Blockline formatter ***/
548 static void table_row_blockline_output(struct table *tbl)
550 for(uint i = 0; i < tbl->cols_to_output; i++) {
551 const struct table_column *col_def = tbl->column_order[i].col_def;
552 bprintf(tbl->out, "%s: %s\n", col_def->name, tbl->column_order[i].cell_content);
554 bputc(tbl->out, '\n');
557 static void table_start_blockline(struct table *tbl)
559 if(tbl->col_delimiter == NULL) {
560 tbl->col_delimiter = "\n";
564 const struct table_formatter table_fmt_blockline = {
565 .row_output = table_row_blockline_output,
566 .table_start = table_start_blockline
575 enum test_table_cols {
576 TEST_COL0_STR, TEST_COL1_INT, TEST_COL2_UINT, TEST_COL3_BOOL, TEST_COL4_DOUBLE
579 static struct table_col_instance test_column_order[] = { TBL_COL(TEST_COL3_BOOL), TBL_COL(TEST_COL4_DOUBLE), TBL_COL(TEST_COL2_UINT), TBL_COL(TEST_COL1_INT), TBL_COL(TEST_COL0_STR) };
581 static struct table_template test_tbl = {
583 [TEST_COL0_STR] = TBL_COL_STR("col0_str", 20),
584 [TEST_COL1_INT] = TBL_COL_INT("col1_int", 8),
585 [TEST_COL2_UINT] = TBL_COL_UINT("col2_uint", 9),
586 [TEST_COL3_BOOL] = TBL_COL_BOOL("col3_bool", 9),
587 [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE("col4_double", 11),
590 TBL_COL_ORDER(test_column_order),
591 TBL_OUTPUT_HUMAN_READABLE,
592 TBL_COL_DELIMITER("\t"),
596 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
598 static void do_print1(struct table *test_tbl)
600 table_col_str(test_tbl, TEST_COL0_STR, "sdsdf");
601 table_col_int(test_tbl, TEST_COL1_INT, -10);
602 table_col_int(test_tbl, TEST_COL1_INT, 10000);
603 table_col_uint(test_tbl, TEST_COL2_UINT, 10);
604 table_col_printf(test_tbl, TEST_COL2_UINT, "XXX-%u", 22222);
605 table_col_bool(test_tbl, TEST_COL3_BOOL, true);
606 table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
607 table_col_printf(test_tbl, TEST_COL4_DOUBLE, "AAA");
608 table_end_row(test_tbl);
610 table_col_str(test_tbl, TEST_COL0_STR, "test");
611 table_col_int(test_tbl, TEST_COL1_INT, -100);
612 table_col_uint(test_tbl, TEST_COL2_UINT, 100);
613 table_col_bool(test_tbl, TEST_COL3_BOOL, false);
614 table_col_printf(test_tbl, TEST_COL4_DOUBLE, "%.2lf", 1.5);
615 table_end_row(test_tbl);
618 static void test_simple1(struct fastbuf *out)
620 struct table *tbl = table_init(&test_tbl);
622 // print table with header
623 table_set_col_order_by_name(tbl, "col3_bool");
624 table_start(tbl, out);
628 // print the same table as in the previous case without header
629 table_set_col_order_by_name(tbl, "col0_str,col2_uint,col1_int,col3_bool");
630 table_start(tbl, out);
634 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
635 tbl->print_header = 0;
636 table_start(tbl, out);
639 tbl->print_header = 1;
641 table_set_col_order_by_name(tbl, "col3_bool");
642 table_start(tbl, out);
646 table_set_col_order_by_name(tbl, "col3_bool,col0_str");
647 table_start(tbl, out);
651 table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint");
652 table_start(tbl, out);
656 table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
657 table_start(tbl, out);
661 table_set_col_order_by_name(tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
662 table_start(tbl, out);
669 enum test_any_table_cols {
670 TEST_ANY_COL0_INT, TEST_ANY_COL1_ANY
673 static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL_FMT(TEST_ANY_COL1_ANY, XTYPE_FMT_PRETTY) };
675 static struct table_template test_any_tbl = {
677 [TEST_ANY_COL0_INT] = TBL_COL_INT("col0_int", 8),
678 [TEST_ANY_COL1_ANY] = TBL_COL_ANY_FMT("col1_any", 9, XTYPE_FMT_PRETTY),
681 TBL_COL_ORDER(test_any_column_order),
682 TBL_OUTPUT_HUMAN_READABLE,
683 TBL_COL_DELIMITER("\t"),
686 static void test_any_type(struct fastbuf *out)
688 struct table *tbl = table_init(&test_any_tbl);
690 table_start(tbl, out);
692 table_col_int(tbl, TEST_ANY_COL0_INT, -10);
693 table_col_int(tbl, TEST_ANY_COL1_ANY, 10000);
696 table_col_int(tbl, TEST_ANY_COL0_INT, -10);
697 table_col_double(tbl, TEST_ANY_COL1_ANY, 1.4);
700 table_col_printf(tbl, TEST_ANY_COL0_INT, "%d", 10);
701 table_col_double(tbl, TEST_ANY_COL1_ANY, 1.4);
708 int main(int argc UNUSED, char **argv UNUSED)
711 out = bfdopen_shared(1, 4096);