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>
15 /*** Management of tables ***/
17 void table_init(struct table *tbl)
19 int col_count = 0; // count the number of columns in the struct table
22 if(tbl->columns[col_count].name == NULL &&
23 tbl->columns[col_count].fmt == NULL &&
24 tbl->columns[col_count].width == 0 &&
25 tbl->columns[col_count].type == COL_TYPE_LAST)
27 ASSERT(tbl->columns[col_count].name != NULL);
28 ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl->columns[col_count].fmt != NULL);
29 ASSERT(tbl->columns[col_count].width != 0);
32 tbl->pool = mp_new(4096);
34 tbl->column_count = col_count;
37 tbl->formatter = &table_fmt_human_readable;
40 tbl->print_header = 1; // by default, print header
43 void table_cleanup(struct table *tbl)
46 memset(tbl, 0, sizeof(struct table));
49 // TODO: test default column order
50 static void table_make_default_column_order(struct table *tbl)
52 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count);
53 for(int i = 0; i < tbl->column_count; i++) {
56 table_set_col_order(tbl, col_order_int, tbl->column_count);
59 void table_start(struct table *tbl, struct fastbuf *out)
61 tbl->last_printed_col = -1;
62 tbl->row_printing_started = 0;
65 ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
67 if(!tbl->col_str_ptrs) {
68 tbl->col_str_ptrs = mp_alloc_zero(tbl->pool, sizeof(char *) * tbl->column_count);
71 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
73 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
75 mp_save(tbl->pool, &tbl->pool_state);
77 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
78 ASSERT_MSG(tbl->append_delimiter, "Append delimiter not specified.");
81 void table_end(struct table *tbl)
83 tbl->last_printed_col = -1;
84 tbl->row_printing_started = 0;
86 mp_restore(tbl->pool, &tbl->pool_state);
88 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
91 /*** Configuration ***/
93 void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
98 int table_get_col_idx(struct table *tbl, const char *col_name)
100 for(int i = 0; i < tbl->column_count; i++) {
101 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
106 const char * table_get_col_list(struct table *tbl)
108 if(tbl->column_count == 0) return "";
110 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
112 for(int i = 1; i < tbl->column_count; i++) {
113 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
119 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
121 for(int i = 0; i < cols_to_output; i++) {
122 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);
125 tbl->cols_to_output = cols_to_output;
126 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * cols_to_output);
127 for(int i = 0; i < cols_to_output; i++) {
128 tbl->column_order[i].idx = col_order[i];
133 bool table_col_is_printed(struct table *tbl, uint col_idx)
135 for(uint i = 0; i < tbl->cols_to_output; i++) {
136 if(tbl->column_order[i].idx == col_idx) return 1;
143 * TODO: This function deliberately leaks memory. When it is called multiple times,
144 * previous column orders still remain allocated in the table's memory pool.
146 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
148 if(col_order_str[0] == '*') {
149 tbl->column_order = mp_alloc(tbl->pool, sizeof(struct table_col_info) * tbl->column_count);
150 tbl->cols_to_output = tbl->column_count;
151 for(uint i = 0; i < tbl->cols_to_output; i++) {
152 tbl->column_order[i].idx = i;
153 tbl->column_order[i].output_type = 0;
159 if(!col_order_str[0]) {
160 tbl->column_order = mp_alloc(tbl->pool, 0);
161 tbl->cols_to_output = 0;
165 char *tmp_col_order = stk_strdup(col_order_str);
168 for(int i = 0; col_order_str[i] != 0; i++) {
169 if(col_order_str[i] == ',') {
174 int *col_order_int = alloca(sizeof(int) * col_count); //mp_alloc_zero(tbl->pool, sizeof(int) * col_count);
175 int curr_col_order_int = 0;
176 const char *name_start = tmp_col_order;
178 char *next = strchr(name_start, ',');
183 int idx = table_get_col_idx(tbl, name_start);
185 return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
187 col_order_int[curr_col_order_int++] = idx;
192 //tbl->column_order = col_order_int;
193 //tbl->cols_to_output = curr_col_order_int;
194 table_set_col_order(tbl, col_order_int, curr_col_order_int);
198 /*** Table cells ***/
200 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
202 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
203 tbl->last_printed_col = col;
204 tbl->row_printing_started = 1;
207 tbl->col_str_ptrs[col] = mp_vprintf(tbl->pool, fmt, args);
211 static const char *table_col_default_fmts[] = {
212 [COL_TYPE_STR] = "%s",
213 [COL_TYPE_INT] = "%d",
214 [COL_TYPE_S64] = "%lld",
215 [COL_TYPE_INTMAX] = "%jd",
216 [COL_TYPE_UINT] = "%u",
217 [COL_TYPE_U64] = "%llu",
218 [COL_TYPE_UINTMAX] = "%ju",
219 [COL_TYPE_BOOL] = "%d",
220 [COL_TYPE_DOUBLE] = "%.2lf",
221 [COL_TYPE_ANY] = NULL,
222 [COL_TYPE_LAST] = NULL
225 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val) \
227 const char *fmt = tbl->columns[col].fmt;\
228 if(tbl->columns[col].type == COL_TYPE_ANY) {\
229 fmt = table_col_default_fmts[_typeconst_];\
231 table_col_##_name_##_fmt(tbl, col, fmt, val);\
234 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val) \
236 int col = table_get_col_idx(tbl, col_name);\
237 table_col_##_name_(tbl, col, val);\
240 #define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
242 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
243 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
244 ASSERT(fmt != NULL);\
245 tbl->last_printed_col = col;\
246 tbl->row_printing_started = 1;\
247 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val);\
250 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_);\
251 TABLE_COL_STR(_name_, _type_, _typeconst_);\
252 TABLE_COL_FMT(_name_, _type_, _typeconst_);
254 TABLE_COL_BODIES(int, int, COL_TYPE_INT)
255 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
256 TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
257 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
258 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
259 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
260 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
261 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
265 #undef TABLE_COL_BODIES
267 void table_col_bool(struct table *tbl, int col, uint val)
269 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
272 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
274 int col = table_get_col_idx(tbl, col_name);
275 table_col_bool(tbl, col, val);
278 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
280 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
281 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
283 tbl->last_printed_col = col;
284 tbl->row_printing_started = 1;
285 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val ? "true" : "false");
288 #define TABLE_APPEND(_name_, _type_, _typeconst_) void table_append_##_name_(struct table *tbl, _type_ val) \
290 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);\
291 ASSERT(_typeconst_ == tbl->columns[tbl->last_printed_col].type);\
292 int col = tbl->last_printed_col;\
293 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);\
294 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val);\
297 TABLE_APPEND(int, int, COL_TYPE_INT)
298 TABLE_APPEND(uint, uint, COL_TYPE_UINT)
299 TABLE_APPEND(double, double, COL_TYPE_DOUBLE)
300 TABLE_APPEND(str, const char *, COL_TYPE_STR)
301 TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
302 TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
303 TABLE_APPEND(u64, u64, COL_TYPE_U64)
306 void table_append_bool(struct table *tbl, int val)
308 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
309 ASSERT(COL_TYPE_BOOL == tbl->columns[tbl->last_printed_col].type);
311 int col = tbl->last_printed_col;
313 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
315 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val ? "true" : "false");
318 void table_append_printf(struct table *tbl, const char *fmt, ...)
320 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
321 int col = tbl->last_printed_col;
326 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
327 tbl->col_str_ptrs[col] = mp_vprintf_append(tbl->pool, tbl->col_str_ptrs[col], fmt, args);
332 void table_reset_row(struct table *tbl)
334 memset(tbl->col_str_ptrs, 0, sizeof(char *) * tbl->column_count);
335 mp_restore(tbl->pool, &tbl->pool_state);
336 tbl->last_printed_col = -1;
337 tbl->row_printing_started = 0;
340 void table_end_row(struct table *tbl)
342 ASSERT(tbl->formatter->row_output);
343 if(tbl->row_printing_started == 0) return;
344 tbl->formatter->row_output(tbl);
345 table_reset_row(tbl);
348 /* Construction of a cell using a fastbuf */
350 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
352 fbpool_init(&tbl->fb_col_out);
353 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
355 return &tbl->fb_col_out.fb;
358 void table_col_fbend(struct table *tbl)
360 tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
364 /*** Option parsing ***/
366 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
368 // Options with no value
369 if(value == NULL || (value != NULL && strlen(value) == 0)) {
370 if(strcmp(key, "noheader") == 0) {
371 tbl->print_header = 0;
376 // Options with a value
378 if(strcmp(key, "header") == 0) {
380 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
381 uint tmp = value[0] - '0';
383 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
384 tbl->print_header = tmp;
386 } else if(strcmp(key, "cols") == 0) {
387 const char *err = table_set_col_order_by_name(tbl, value);
389 return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
392 } else if(strcmp(key, "fmt") == 0) {
393 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
394 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
395 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
397 return "Tableprinter: invalid argument to output-type option.";
400 } else if(strcmp(key, "col-delim") == 0) {
401 char * d = mp_printf(tbl->pool, "%s", value);
402 tbl->col_delimiter = d;
408 if(tbl->formatter && tbl->formatter->process_option) {
409 const char *err = NULL;
410 if (tbl->formatter->process_option(tbl, key, value, &err)) {
415 // Unrecognized option
416 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
419 const char *table_set_option(struct table *tbl, const char *opt)
421 char *key = stk_strdup(opt);
422 char *value = strchr(key, ':');
426 return table_set_option_value(tbl, key, value);
429 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
431 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
432 const char *rv = table_set_option(tbl, gary_table_opts[i]);
440 /*** Default formatter for human-readable output ***/
442 static void table_row_human_readable(struct table *tbl)
444 for(uint i = 0; i < tbl->cols_to_output; i++) {
445 int col_idx = tbl->column_order[i].idx;
447 bputs(tbl->out, tbl->col_delimiter);
449 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
450 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
451 bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
453 bputc(tbl->out, '\n');
456 static void table_write_header(struct table *tbl)
458 for(uint i = 0; i < tbl->cols_to_output; i++) {
459 int col_idx = tbl->column_order[i].idx;
461 bputs(tbl->out, tbl->col_delimiter);
463 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
464 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
465 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
467 bputc(tbl->out, '\n');
470 static void table_start_human_readable(struct table *tbl)
472 if(tbl->col_delimiter == NULL) {
473 tbl->col_delimiter = " ";
476 if(tbl->append_delimiter == NULL) {
477 tbl->append_delimiter = ",";
480 if(tbl->print_header != 0) {
481 table_write_header(tbl);
485 struct table_formatter table_fmt_human_readable = {
486 .row_output = table_row_human_readable,
487 .table_start = table_start_human_readable,
490 /*** Default formatter for machine-readable output ***/
492 static void table_row_machine_readable(struct table *tbl)
494 for(uint i = 0; i < tbl->cols_to_output; i++) {
495 int col_idx = tbl->column_order[i].idx;
497 bputs(tbl->out, tbl->col_delimiter);
499 bputs(tbl->out, tbl->col_str_ptrs[col_idx]);
501 bputc(tbl->out, '\n');
504 static void table_start_machine_readable(struct table *tbl)
506 if(tbl->col_delimiter == NULL) {
507 tbl->col_delimiter = ";";
510 if(tbl->append_delimiter == NULL) {
511 tbl->append_delimiter = ",";
514 if(tbl->print_header != 0) {
515 uint col_idx = tbl->column_order[0].idx;
516 bputs(tbl->out, tbl->columns[col_idx].name);
517 for(uint i = 1; i < tbl->cols_to_output; i++) {
518 col_idx = tbl->column_order[i].idx;
519 bputs(tbl->out, tbl->col_delimiter);
520 bputs(tbl->out, tbl->columns[col_idx].name);
522 bputc(tbl->out, '\n');
526 struct table_formatter table_fmt_machine_readable = {
527 .row_output = table_row_machine_readable,
528 .table_start = table_start_machine_readable,
532 /*** Blockline formatter ***/
534 static void table_row_blockline_output(struct table *tbl)
536 for(uint i = 0; i < tbl->cols_to_output; i++) {
537 int col_idx = tbl->column_order[i].idx;
538 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->col_str_ptrs[col_idx]);
540 bputc(tbl->out, '\n');
543 static void table_start_blockline(struct table *tbl)
545 if(tbl->col_delimiter == NULL) {
546 tbl->col_delimiter = " ";
549 if(tbl->append_delimiter == NULL) {
550 tbl->append_delimiter = ",";
554 struct table_formatter table_fmt_blockline = {
555 .row_output = table_row_blockline_output,
556 .table_start = table_start_blockline
567 enum test_table_cols {
568 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
571 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) };
573 static struct table test_tbl = {
575 [test_col0_str] = TBL_COL_STR("col0_str", 20),
576 [test_col1_int] = TBL_COL_INT("col1_int", 8),
577 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
578 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
579 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
582 TBL_COL_ORDER(test_column_order),
583 TBL_OUTPUT_HUMAN_READABLE,
584 TBL_COL_DELIMITER("\t"),
585 TBL_APPEND_DELIMITER(",")
589 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
591 static void do_print1(struct table *test_tbl)
593 table_col_str(test_tbl, test_col0_str, "sdsdf");
594 table_append_str(test_tbl, "aaaaa");
595 table_col_int(test_tbl, test_col1_int, -10);
596 table_col_int(test_tbl, test_col1_int, 10000);
597 table_col_uint(test_tbl, test_col2_uint, 10);
598 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
599 table_col_bool(test_tbl, test_col3_bool, 1);
600 table_col_double(test_tbl, test_col4_double, 1.5);
601 table_col_printf(test_tbl, test_col4_double, "AAA");
602 table_end_row(test_tbl);
604 table_col_str(test_tbl, test_col0_str, "test");
605 table_append_str(test_tbl, "bbbbb");
606 table_col_int(test_tbl, test_col1_int, -100);
607 table_col_uint(test_tbl, test_col2_uint, 100);
608 table_col_bool(test_tbl, test_col3_bool, 0);
609 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
610 table_end_row(test_tbl);
613 static void test_simple1(struct fastbuf *out)
615 table_init(&test_tbl);
617 // print table with header
618 table_set_col_order_by_name(&test_tbl, "col3_bool");
619 table_start(&test_tbl, out);
620 do_print1(&test_tbl);
621 table_end(&test_tbl);
623 // print the same table as in the previous case without header
624 table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
625 table_start(&test_tbl, out);
626 do_print1(&test_tbl);
627 table_end(&test_tbl);
629 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
630 test_tbl.print_header = 0;
631 table_start(&test_tbl, out);
632 do_print1(&test_tbl);
633 table_end(&test_tbl);
634 test_tbl.print_header = 1;
636 table_set_col_order_by_name(&test_tbl, "col3_bool");
637 table_start(&test_tbl, out);
638 do_print1(&test_tbl);
639 table_end(&test_tbl);
641 table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
642 table_start(&test_tbl, out);
643 do_print1(&test_tbl);
644 table_end(&test_tbl);
646 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
647 table_start(&test_tbl, out);
648 do_print1(&test_tbl);
649 table_end(&test_tbl);
651 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");
652 table_start(&test_tbl, out);
653 do_print1(&test_tbl);
654 table_end(&test_tbl);
656 table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
657 table_start(&test_tbl, out);
658 do_print1(&test_tbl);
659 table_end(&test_tbl);
661 table_cleanup(&test_tbl);
664 enum test_any_table_cols {
665 test_any_col0_int, test_any_col1_any
668 static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
670 static struct table test_any_tbl = {
672 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
673 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
676 TBL_COL_ORDER(test_any_column_order),
677 TBL_OUTPUT_HUMAN_READABLE,
678 TBL_COL_DELIMITER("\t"),
679 TBL_APPEND_DELIMITER(",")
682 static void test_any_type(struct fastbuf *out)
684 table_init(&test_any_tbl);
686 table_start(&test_any_tbl, out);
688 table_col_int(&test_any_tbl, test_any_col0_int, -10);
689 table_col_int(&test_any_tbl, test_any_col1_any, 10000);
690 table_end_row(&test_any_tbl);
692 table_col_int(&test_any_tbl, test_any_col0_int, -10);
693 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
694 table_end_row(&test_any_tbl);
696 table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
697 table_append_printf(&test_any_tbl, "%d", 20);
698 table_append_printf(&test_any_tbl, "%d", 30);
699 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
700 table_append_printf(&test_any_tbl, "%.2lf", 1.5);
701 table_append_printf(&test_any_tbl, "%.2lf", 1.6);
702 table_end_row(&test_any_tbl);
704 table_end(&test_any_tbl);
705 table_cleanup(&test_any_tbl);
708 int main(int argc UNUSED, char **argv UNUSED)
711 out = bfdopen_shared(1, 4096);