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>
16 /* Forward declarations */
18 static void table_update_ll(struct table *tbl);
20 /*** Management of tables ***/
22 void table_init(struct table *tbl)
24 int col_count = 0; // count the number of columns in the struct table
27 if(tbl->columns[col_count].name == NULL &&
28 tbl->columns[col_count].fmt == NULL &&
29 tbl->columns[col_count].width == 0 &&
30 tbl->columns[col_count].type == COL_TYPE_LAST)
32 ASSERT(tbl->columns[col_count].name != NULL);
33 ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl->columns[col_count].fmt != NULL);
34 ASSERT(tbl->columns[col_count].width != 0);
38 tbl->pool = mp_new(4096);
40 tbl->column_count = col_count;
43 tbl->formatter = &table_fmt_human_readable;
46 tbl->print_header = 1; // by default, print header
49 void table_cleanup(struct table *tbl)
52 memset(tbl, 0, sizeof(struct table));
55 // TODO: test default column order
56 static void table_make_default_column_order(struct table *tbl)
58 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count);
59 for(int i = 0; i < tbl->column_count; i++) {
62 table_set_col_order(tbl, col_order_int, tbl->column_count);
65 void table_start(struct table *tbl, struct fastbuf *out)
67 tbl->last_printed_col = -1;
68 tbl->row_printing_started = 0;
71 ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
73 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
75 // update linked lists in case that the lists were initialized in static parts
78 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
80 mp_save(tbl->pool, &tbl->pool_state);
82 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
85 void table_end(struct table *tbl)
87 tbl->last_printed_col = -1;
88 tbl->row_printing_started = 0;
90 mp_restore(tbl->pool, &tbl->pool_state);
92 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
95 /*** Configuration ***/
97 void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
102 int table_get_col_idx(struct table *tbl, const char *col_name)
104 for(int i = 0; i < tbl->column_count; i++) {
105 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
110 const char * table_get_col_list(struct table *tbl)
112 if(tbl->column_count == 0) return "";
114 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
116 for(int i = 1; i < tbl->column_count; i++) {
117 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
124 static void table_update_ll(struct table *tbl)
126 int cols_to_output = tbl->cols_to_output;
128 for(int i = 0; i < tbl->column_count; i++) {
129 tbl->columns[i].first_column = -1;
130 tbl->columns[i].last_column = -1;
133 for(int i = 0; i < cols_to_output; i++) {
134 int col_idx = tbl->column_order[i].idx;
135 int last = tbl->columns[col_idx].last_column;
137 tbl->columns[col_idx].last_column = i;
138 tbl->column_order[last].next_column = i;
140 tbl->columns[col_idx].last_column = i;
141 tbl->columns[col_idx].first_column = i;
143 tbl->column_order[i].next_column = -1;
147 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
149 for(int i = 0; i < cols_to_output; i++) {
150 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);
153 tbl->cols_to_output = cols_to_output;
154 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * cols_to_output);
155 for(int i = 0; i < cols_to_output; i++) {
156 int col_idx = col_order[i];
157 tbl->column_order[i].idx = col_idx;
158 tbl->column_order[i].cell_content = NULL;
159 tbl->column_order[i].output_type = CELL_OUT_UNINITIALIZED;
161 table_update_ll(tbl);
164 bool table_col_is_printed(struct table *tbl, uint col_idx)
166 if(tbl->columns[col_idx].first_column == -1) return 0;
172 * TODO: This function deliberately leaks memory. When it is called multiple times,
173 * previous column orders still remain allocated in the table's memory pool.
175 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
177 if(col_order_str[0] == '*') {
178 int *col_order_int = alloca(sizeof(int) * tbl->column_count);
179 for(int i = 0; i < tbl->column_count; i++) {
180 col_order_int[i] = i;
182 table_set_col_order(tbl, col_order_int, tbl->column_count);
187 if(!col_order_str[0]) {
188 tbl->column_order = mp_alloc(tbl->pool, 0);
189 tbl->cols_to_output = 0;
193 char *tmp_col_order = stk_strdup(col_order_str);
196 for(int i = 0; col_order_str[i] != 0; i++) {
197 if(col_order_str[i] == ',') {
202 int *col_order_int = alloca(sizeof(int) * col_count);
203 int curr_col_order_int = 0;
204 const char *name_start = tmp_col_order;
206 char *next = strchr(name_start, ',');
211 int idx = table_get_col_idx(tbl, name_start);
213 return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
215 col_order_int[curr_col_order_int++] = idx;
220 table_set_col_order(tbl, col_order_int, curr_col_order_int);
224 /*** Table cells ***/
226 static void table_set_all_cols_content(struct table *tbl, int col, char *col_content, int override)
228 int curr_col = tbl->columns[col].first_column;
229 while(curr_col != -1) {
230 if(override == 0 && tbl->column_order[curr_col].output_type != CELL_OUT_UNINITIALIZED ) {
231 fprintf(stdout, "curr_col: %d\n", curr_col);
233 die("Error while setting content of all cells of a single type column, cell format should not be overriden.");
235 tbl->column_order[curr_col].cell_content = col_content;
236 curr_col = tbl->column_order[curr_col].next_column;
240 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
242 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
243 tbl->last_printed_col = col;
244 tbl->row_printing_started = 1;
247 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
248 table_set_all_cols_content(tbl, col, cell_content, 1);
252 static const char *table_col_default_fmts[] = {
253 [COL_TYPE_STR] = "%s",
254 [COL_TYPE_INT] = "%d",
255 [COL_TYPE_S64] = "%lld",
256 [COL_TYPE_INTMAX] = "%jd",
257 [COL_TYPE_UINT] = "%u",
258 [COL_TYPE_U64] = "%llu",
259 [COL_TYPE_UINTMAX] = "%ju",
260 [COL_TYPE_BOOL] = "%d",
261 [COL_TYPE_DOUBLE] = "%.2lf",
262 [COL_TYPE_ANY] = NULL,
263 [COL_TYPE_LAST] = NULL
266 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
268 const char *fmt = tbl->columns[col].fmt;\
269 if(tbl->columns[col].type == COL_TYPE_ANY) {\
270 fmt = table_col_default_fmts[_typeconst_];\
272 table_col_##_name_##_fmt(tbl, col, fmt, val);\
275 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
277 int col = table_get_col_idx(tbl, col_name);\
278 table_col_##_name_(tbl, col, val);\
281 #define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
283 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
284 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
285 ASSERT(fmt != NULL);\
286 tbl->last_printed_col = col;\
287 tbl->row_printing_started = 1;\
288 char *cell_content = mp_printf(tbl->pool, fmt, val);\
289 table_set_all_cols_content(tbl, col, cell_content, 0);\
292 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_);\
293 TABLE_COL_STR(_name_, _type_, _typeconst_);\
294 TABLE_COL_FMT(_name_, _type_, _typeconst_);
296 TABLE_COL_BODIES(int, int, COL_TYPE_INT)
297 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
298 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
299 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
300 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
301 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
302 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
304 // column type double is a special case
305 TABLE_COL(double, double, COL_TYPE_DOUBLE);
306 TABLE_COL_STR(double, double, COL_TYPE_DOUBLE);
310 #undef TABLE_COL_BODIES
312 void table_col_double_fmt(struct table *tbl, int col, const char *fmt, double val)
314 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
315 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_DOUBLE == tbl->columns[col].type);
317 tbl->last_printed_col = col;
318 tbl->row_printing_started = 1;
319 char *cell_content = mp_printf(tbl->pool, fmt, val);
320 int curr_col = tbl->columns[col].first_column;
321 while(curr_col != -1) {
322 if(tbl->column_order[curr_col].output_type < 0) tbl->column_order[curr_col].cell_content = cell_content;
324 char *cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val);
325 tbl->column_order[curr_col].cell_content = cell_content_tmp;
327 curr_col = tbl->column_order[curr_col].next_column;
331 void table_col_bool(struct table *tbl, int col, uint val)
333 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
336 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
338 int col = table_get_col_idx(tbl, col_name);
339 table_col_bool(tbl, col, val);
342 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
344 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
345 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
347 tbl->last_printed_col = col;
348 tbl->row_printing_started = 1;
350 int curr_col = tbl->columns[col].first_column;
351 while(curr_col != -1) {
352 switch(tbl->column_order[curr_col].output_type) {
353 case CELL_OUT_HUMAN_READABLE:
354 case CELL_OUT_UNINITIALIZED:
355 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "true" : "false");
357 case CELL_OUT_MACHINE_READABLE:
358 // FIXME: this is just an example of printing in different formats
359 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "1" : "0");
362 die("Unsupported output type.");
364 curr_col = tbl->column_order[curr_col].next_column;
367 // FIXME: add to printing of all columns
370 void table_reset_row(struct table *tbl)
372 for(uint i = 0; i < tbl->cols_to_output; i++) {
373 tbl->column_order[i].cell_content = NULL;
375 mp_restore(tbl->pool, &tbl->pool_state);
376 tbl->last_printed_col = -1;
377 tbl->row_printing_started = 0;
380 void table_end_row(struct table *tbl)
382 ASSERT(tbl->formatter->row_output);
383 if(tbl->row_printing_started == 0) return;
384 tbl->formatter->row_output(tbl);
385 table_reset_row(tbl);
388 /* Construction of a cell using a fastbuf */
390 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
392 fbpool_init(&tbl->fb_col_out);
393 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
395 return &tbl->fb_col_out.fb;
398 void table_col_fbend(struct table *tbl)
400 char *cell_content = fbpool_end(&tbl->fb_col_out);
401 table_set_all_cols_content(tbl, tbl->col_out, cell_content, 1);
405 /*** Option parsing ***/
407 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
409 // Options with no value
410 if(value == NULL || (value != NULL && strlen(value) == 0)) {
411 if(strcmp(key, "noheader") == 0) {
412 tbl->print_header = 0;
417 // Options with a value
419 if(strcmp(key, "header") == 0) {
421 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
422 uint tmp = value[0] - '0';
424 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
425 tbl->print_header = tmp;
427 } else if(strcmp(key, "cols") == 0) {
428 const char *err = table_set_col_order_by_name(tbl, value);
430 return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
433 } else if(strcmp(key, "fmt") == 0) {
434 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
435 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
436 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
438 return "Tableprinter: invalid argument to output-type option.";
441 } else if(strcmp(key, "col-delim") == 0) {
442 char * d = mp_printf(tbl->pool, "%s", value);
443 tbl->col_delimiter = d;
449 if(tbl->formatter && tbl->formatter->process_option) {
450 const char *err = NULL;
451 if (tbl->formatter->process_option(tbl, key, value, &err)) {
456 // Unrecognized option
457 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
460 const char *table_set_option(struct table *tbl, const char *opt)
462 char *key = stk_strdup(opt);
463 char *value = strchr(key, ':');
467 return table_set_option_value(tbl, key, value);
470 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
472 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
473 const char *rv = table_set_option(tbl, gary_table_opts[i]);
481 /*** Default formatter for human-readable output ***/
483 static void table_row_human_readable(struct table *tbl)
485 for(uint i = 0; i < tbl->cols_to_output; i++) {
486 int col_idx = tbl->column_order[i].idx;
488 bputs(tbl->out, tbl->col_delimiter);
490 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
491 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
492 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
494 bputc(tbl->out, '\n');
497 static void table_write_header(struct table *tbl)
499 for(uint i = 0; i < tbl->cols_to_output; i++) {
500 int col_idx = tbl->column_order[i].idx;
502 bputs(tbl->out, tbl->col_delimiter);
504 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
505 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
506 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
508 bputc(tbl->out, '\n');
511 static void table_start_human_readable(struct table *tbl)
513 if(tbl->col_delimiter == NULL) {
514 tbl->col_delimiter = " ";
517 if(tbl->print_header != 0) {
518 table_write_header(tbl);
522 struct table_formatter table_fmt_human_readable = {
523 .row_output = table_row_human_readable,
524 .table_start = table_start_human_readable,
527 /*** Default formatter for machine-readable output ***/
529 static void table_row_machine_readable(struct table *tbl)
531 for(uint i = 0; i < tbl->cols_to_output; i++) {
533 bputs(tbl->out, tbl->col_delimiter);
535 bputs(tbl->out, tbl->column_order[i].cell_content);
537 bputc(tbl->out, '\n');
540 static void table_start_machine_readable(struct table *tbl)
542 if(tbl->col_delimiter == NULL) {
543 tbl->col_delimiter = "\t";
546 if(tbl->print_header != 0) {
547 uint col_idx = tbl->column_order[0].idx;
548 bputs(tbl->out, tbl->columns[col_idx].name);
549 for(uint i = 1; i < tbl->cols_to_output; i++) {
550 col_idx = tbl->column_order[i].idx;
551 bputs(tbl->out, tbl->col_delimiter);
552 bputs(tbl->out, tbl->columns[col_idx].name);
554 bputc(tbl->out, '\n');
558 struct table_formatter table_fmt_machine_readable = {
559 .row_output = table_row_machine_readable,
560 .table_start = table_start_machine_readable,
564 /*** Blockline formatter ***/
566 static void table_row_blockline_output(struct table *tbl)
568 for(uint i = 0; i < tbl->cols_to_output; i++) {
569 int col_idx = tbl->column_order[i].idx;
570 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->column_order[i].cell_content);
572 bputc(tbl->out, '\n');
575 static void table_start_blockline(struct table *tbl)
577 if(tbl->col_delimiter == NULL) {
578 tbl->col_delimiter = "\n";
582 struct table_formatter table_fmt_blockline = {
583 .row_output = table_row_blockline_output,
584 .table_start = table_start_blockline
595 enum test_table_cols {
596 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
599 static struct table_col_info 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) };
601 static struct table test_tbl = {
603 [test_col0_str] = TBL_COL_STR("col0_str", 20),
604 [test_col1_int] = TBL_COL_INT("col1_int", 8),
605 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
606 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
607 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
610 TBL_COL_ORDER(test_column_order),
611 TBL_OUTPUT_HUMAN_READABLE,
612 TBL_COL_DELIMITER("\t"),
616 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
618 static void do_print1(struct table *test_tbl)
620 table_col_str(test_tbl, test_col0_str, "sdsdf");
621 table_col_int(test_tbl, test_col1_int, -10);
622 table_col_int(test_tbl, test_col1_int, 10000);
623 table_col_uint(test_tbl, test_col2_uint, 10);
624 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
625 table_col_bool(test_tbl, test_col3_bool, 1);
626 table_col_double(test_tbl, test_col4_double, 1.5);
627 table_col_printf(test_tbl, test_col4_double, "AAA");
628 table_end_row(test_tbl);
630 table_col_str(test_tbl, test_col0_str, "test");
631 table_col_int(test_tbl, test_col1_int, -100);
632 table_col_uint(test_tbl, test_col2_uint, 100);
633 table_col_bool(test_tbl, test_col3_bool, 0);
634 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
635 table_end_row(test_tbl);
638 static void test_simple1(struct fastbuf *out)
640 table_init(&test_tbl);
642 // print table with header
643 table_set_col_order_by_name(&test_tbl, "col3_bool");
644 table_start(&test_tbl, out);
645 do_print1(&test_tbl);
646 table_end(&test_tbl);
648 // print the same table as in the previous case without header
649 table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
650 table_start(&test_tbl, out);
651 do_print1(&test_tbl);
652 table_end(&test_tbl);
654 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
655 test_tbl.print_header = 0;
656 table_start(&test_tbl, out);
657 do_print1(&test_tbl);
658 table_end(&test_tbl);
659 test_tbl.print_header = 1;
661 table_set_col_order_by_name(&test_tbl, "col3_bool");
662 table_start(&test_tbl, out);
663 do_print1(&test_tbl);
664 table_end(&test_tbl);
666 table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
667 table_start(&test_tbl, out);
668 do_print1(&test_tbl);
669 table_end(&test_tbl);
671 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
672 table_start(&test_tbl, out);
673 do_print1(&test_tbl);
674 table_end(&test_tbl);
676 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
677 table_start(&test_tbl, out);
678 do_print1(&test_tbl);
679 table_end(&test_tbl);
681 table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
682 table_start(&test_tbl, out);
683 do_print1(&test_tbl);
684 table_end(&test_tbl);
686 table_cleanup(&test_tbl);
689 enum test_any_table_cols {
690 test_any_col0_int, test_any_col1_any
693 static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
695 static struct table test_any_tbl = {
697 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
698 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
701 TBL_COL_ORDER(test_any_column_order),
702 TBL_OUTPUT_HUMAN_READABLE,
703 TBL_COL_DELIMITER("\t"),
706 static void test_any_type(struct fastbuf *out)
708 table_init(&test_any_tbl);
710 table_start(&test_any_tbl, out);
712 table_col_int(&test_any_tbl, test_any_col0_int, -10);
713 table_col_int(&test_any_tbl, test_any_col1_any, 10000);
714 table_end_row(&test_any_tbl);
716 table_col_int(&test_any_tbl, test_any_col0_int, -10);
717 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
718 table_end_row(&test_any_tbl);
720 table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
721 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
722 table_end_row(&test_any_tbl);
724 table_end(&test_any_tbl);
725 table_cleanup(&test_any_tbl);
728 int main(int argc UNUSED, char **argv UNUSED)
731 out = bfdopen_shared(1, 4096);