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);
30 ASSERT(tbl->columns[col_count].type < COL_TYPE_LAST);
33 tbl->pool = mp_new(4096);
35 tbl->column_count = col_count;
38 tbl->formatter = &table_fmt_human_readable;
41 tbl->print_header = 1; // by default, print header
44 void table_cleanup(struct table *tbl)
47 memset(tbl, 0, sizeof(struct table));
50 // TODO: test default column order
51 static void table_make_default_column_order(struct table *tbl)
53 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count);
54 for(int i = 0; i < tbl->column_count; i++) {
57 table_set_col_order(tbl, col_order_int, tbl->column_count);
60 void table_start(struct table *tbl, struct fastbuf *out)
62 tbl->last_printed_col = -1;
63 tbl->row_printing_started = 0;
66 ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
68 if(!tbl->col_str_ptrs) {
69 tbl->col_str_ptrs = mp_alloc_zero(tbl->pool, sizeof(char *) * tbl->column_count);
72 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
74 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
76 mp_save(tbl->pool, &tbl->pool_state);
78 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
79 ASSERT_MSG(tbl->append_delimiter, "Append delimiter not specified.");
82 void table_end(struct table *tbl)
84 tbl->last_printed_col = -1;
85 tbl->row_printing_started = 0;
87 mp_restore(tbl->pool, &tbl->pool_state);
89 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
92 /*** Configuration ***/
94 void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
99 int table_get_col_idx(struct table *tbl, const char *col_name)
101 for(int i = 0; i < tbl->column_count; i++) {
102 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
107 const char * table_get_col_list(struct table *tbl)
109 if(tbl->column_count == 0) return "";
111 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
113 for(int i = 1; i < tbl->column_count; i++) {
114 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
120 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
122 for(int i = 0; i < cols_to_output; i++) {
123 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);
126 tbl->column_order = col_order;
127 tbl->cols_to_output = cols_to_output;
131 * TODO: This function deliberately leaks memory. When it is called multiple times,
132 * previous column orders still remain allocated in the table's memory pool.
134 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
136 if(col_order_str[0] == '*') {
137 tbl->column_order = mp_alloc(tbl->pool, sizeof(int) * tbl->column_count);
138 tbl->cols_to_output = tbl->column_count;
139 for(uint i = 0; i < tbl->cols_to_output; i++) tbl->column_order[i] = i;
144 if(!col_order_str[0]) {
145 tbl->column_order = mp_alloc(tbl->pool, 0);
146 tbl->cols_to_output = 0;
150 char *tmp_col_order = stk_strdup(col_order_str);
153 for(int i = 0; col_order_str[i] != 0; i++) {
154 if(col_order_str[i] == ',') {
159 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * col_count);
160 int curr_col_order_int = 0;
161 const char *name_start = tmp_col_order;
163 char *next = strchr(name_start, ',');
168 int idx = table_get_col_idx(tbl, name_start);
170 return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
172 col_order_int[curr_col_order_int++] = idx;
177 tbl->column_order = col_order_int;
178 tbl->cols_to_output = curr_col_order_int;
182 /*** Table cells ***/
184 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
186 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
187 tbl->last_printed_col = col;
188 tbl->row_printing_started = 1;
191 tbl->col_str_ptrs[col] = mp_vprintf(tbl->pool, fmt, args);
195 static const char *table_col_default_fmts[] = {
196 [COL_TYPE_STR] = "%s",
197 [COL_TYPE_INT] = "%d",
198 [COL_TYPE_INTMAX] = "%jd",
199 [COL_TYPE_UINT] = "%u",
200 [COL_TYPE_UINTMAX] = "%ju",
201 [COL_TYPE_BOOL] = "%d",
202 [COL_TYPE_DOUBLE] = "%.2lf",
203 [COL_TYPE_ANY] = NULL,
204 [COL_TYPE_LAST] = NULL
207 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val) \
209 const char *fmt = tbl->columns[col].fmt;\
210 if(tbl->columns[col].type == COL_TYPE_ANY) {\
211 fmt = table_col_default_fmts[_typeconst_];\
213 table_col_##_name_##_fmt(tbl, col, fmt, val);\
216 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val) \
218 int col = table_get_col_idx(tbl, col_name);\
219 table_col_##_name_(tbl, col, val);\
222 #define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
224 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
225 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
226 ASSERT(fmt != NULL);\
227 tbl->last_printed_col = col;\
228 tbl->row_printing_started = 1;\
229 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val);\
232 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_);\
233 TABLE_COL_STR(_name_, _type_, _typeconst_);\
234 TABLE_COL_FMT(_name_, _type_, _typeconst_);
236 TABLE_COL_BODIES(int, int, COL_TYPE_INT)
237 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
238 TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
239 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
240 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
241 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
242 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
243 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
247 #undef TABLE_COL_BODIES
249 void table_col_bool(struct table *tbl, int col, uint val)
251 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
254 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
256 int col = table_get_col_idx(tbl, col_name);
257 table_col_bool(tbl, col, val);
260 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
262 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
263 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
265 tbl->last_printed_col = col;
266 tbl->row_printing_started = 1;
267 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val ? "true" : "false");
270 #define TABLE_APPEND(_name_, _type_, _typeconst_) void table_append_##_name_(struct table *tbl, _type_ val) \
272 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);\
273 ASSERT(_typeconst_ == tbl->columns[tbl->last_printed_col].type);\
274 int col = tbl->last_printed_col;\
275 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);\
276 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val);\
279 TABLE_APPEND(int, int, COL_TYPE_INT)
280 TABLE_APPEND(uint, uint, COL_TYPE_UINT)
281 TABLE_APPEND(double, double, COL_TYPE_DOUBLE)
282 TABLE_APPEND(str, const char *, COL_TYPE_STR)
283 TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
284 TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
285 TABLE_APPEND(u64, u64, COL_TYPE_U64)
288 void table_append_bool(struct table *tbl, int val)
290 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
291 ASSERT(COL_TYPE_BOOL == tbl->columns[tbl->last_printed_col].type);
293 int col = tbl->last_printed_col;
295 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
297 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val ? "true" : "false");
300 void table_append_printf(struct table *tbl, const char *fmt, ...)
302 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
303 int col = tbl->last_printed_col;
308 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
309 tbl->col_str_ptrs[col] = mp_vprintf_append(tbl->pool, tbl->col_str_ptrs[col], fmt, args);
314 void table_end_row(struct table *tbl)
316 ASSERT(tbl->formatter->row_output);
317 tbl->formatter->row_output(tbl);
318 memset(tbl->col_str_ptrs, 0, sizeof(char *) * tbl->column_count);
319 mp_restore(tbl->pool, &tbl->pool_state);
320 tbl->last_printed_col = -1;
321 tbl->row_printing_started = 0;
324 /* Construction of a cell using a fastbuf */
326 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
328 fbpool_init(&tbl->fb_col_out);
329 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
331 return &tbl->fb_col_out.fb;
334 void table_col_fbend(struct table *tbl)
336 tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
340 /*** Option parsing ***/
342 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
344 // Options with no value
345 if(value == NULL || (value != NULL && strlen(value) == 0)) {
346 if(strcmp(key, "noheader") == 0) {
347 tbl->print_header = 0;
352 // Options with a value
354 if(strcmp(key, "header") == 0) {
356 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
357 uint tmp = value[0] - '0';
359 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
360 tbl->print_header = tmp;
362 } else if(strcmp(key, "cols") == 0) {
363 const char *err = table_set_col_order_by_name(tbl, value);
365 return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
368 } else if(strcmp(key, "fmt") == 0) {
369 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
370 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
371 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
373 return "Tableprinter: invalid argument to output-type option.";
376 } else if(strcmp(key, "col-delim") == 0) {
377 char * d = mp_printf(tbl->pool, "%s", value);
378 tbl->col_delimiter = d;
384 if(tbl->formatter && tbl->formatter->process_option) {
385 const char *err = NULL;
386 if (tbl->formatter->process_option(tbl, key, value, &err)) {
391 // Unrecognized option
392 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
395 const char *table_set_option(struct table *tbl, const char *opt)
397 char *key = stk_strdup(opt);
398 char *value = strchr(key, ':');
402 return table_set_option_value(tbl, key, value);
405 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
407 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
408 const char *rv = table_set_option(tbl, gary_table_opts[i]);
416 /*** Default formatter for human-readable output ***/
418 static void table_row_human_readable(struct table *tbl)
420 for(uint i = 0; i < tbl->cols_to_output; i++) {
421 int col_idx = tbl->column_order[i];
423 bputs(tbl->out, tbl->col_delimiter);
425 int col_width = tbl->columns[col_idx].width & CELL_ALIGN_MASK;
426 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
427 bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
429 bputc(tbl->out, '\n');
432 static void table_write_header(struct table *tbl)
434 for(uint i = 0; i < tbl->cols_to_output; i++) {
435 int col_idx = tbl->column_order[i];
437 bputs(tbl->out, tbl->col_delimiter);
439 int col_width = tbl->columns[col_idx].width & CELL_ALIGN_MASK;
440 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
441 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
443 bputc(tbl->out, '\n');
446 static void table_start_human_readable(struct table *tbl)
448 if(tbl->col_delimiter == NULL) {
449 tbl->col_delimiter = " ";
452 if(tbl->append_delimiter == NULL) {
453 tbl->append_delimiter = ",";
456 if(tbl->print_header != 0) {
457 table_write_header(tbl);
461 struct table_formatter table_fmt_human_readable = {
462 .row_output = table_row_human_readable,
463 .table_start = table_start_human_readable,
466 /*** Default formatter for machine-readable output ***/
468 static void table_row_machine_readable(struct table *tbl)
470 for(uint i = 0; i < tbl->cols_to_output; i++) {
471 int col_idx = tbl->column_order[i];
473 bputs(tbl->out, tbl->col_delimiter);
475 bputs(tbl->out, tbl->col_str_ptrs[col_idx]);
477 bputc(tbl->out, '\n');
480 static void table_start_machine_readable(struct table *tbl)
482 if(tbl->col_delimiter == NULL) {
483 tbl->col_delimiter = ";";
486 if(tbl->append_delimiter == NULL) {
487 tbl->append_delimiter = ",";
490 if(tbl->print_header != 0) {
491 uint col_idx = tbl->column_order[0];
492 bputs(tbl->out, tbl->columns[col_idx].name);
493 for(uint i = 1; i < tbl->cols_to_output; i++) {
494 col_idx = tbl->column_order[i];
495 bputs(tbl->out, tbl->col_delimiter);
496 bputs(tbl->out, tbl->columns[col_idx].name);
498 bputc(tbl->out, '\n');
502 struct table_formatter table_fmt_machine_readable = {
503 .row_output = table_row_machine_readable,
504 .table_start = table_start_machine_readable,
508 /*** Blockline formatter ***/
510 static void table_row_blockline_output(struct table *tbl)
512 for(uint i = 0; i < tbl->cols_to_output; i++) {
513 int col_idx = tbl->column_order[i];
514 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->col_str_ptrs[col_idx]);
516 bputc(tbl->out, '\n');
519 static void table_start_blockline(struct table *tbl)
521 if(tbl->col_delimiter == NULL) {
522 tbl->col_delimiter = " ";
525 if(tbl->append_delimiter == NULL) {
526 tbl->append_delimiter = ",";
530 struct table_formatter table_fmt_blockline = {
531 .row_output = table_row_blockline_output,
532 .table_start = table_start_blockline
543 enum test_table_cols {
544 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
547 static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str};
549 static struct table test_tbl = {
551 [test_col0_str] = TBL_COL_STR("col0_str", 20),
552 [test_col1_int] = TBL_COL_INT("col1_int", 8),
553 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
554 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
555 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
558 TBL_COL_ORDER(test_column_order),
559 TBL_OUTPUT_HUMAN_READABLE,
560 TBL_COL_DELIMITER("\t"),
561 TBL_APPEND_DELIMITER(",")
565 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
567 static void do_print1(struct table *test_tbl)
569 table_col_str(test_tbl, test_col0_str, "sdsdf");
570 table_append_str(test_tbl, "aaaaa");
571 table_col_int(test_tbl, test_col1_int, -10);
572 table_col_int(test_tbl, test_col1_int, 10000);
573 table_col_uint(test_tbl, test_col2_uint, 10);
574 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
575 table_col_bool(test_tbl, test_col3_bool, 1);
576 table_col_double(test_tbl, test_col4_double, 1.5);
577 table_col_printf(test_tbl, test_col4_double, "AAA");
578 table_end_row(test_tbl);
580 table_col_str(test_tbl, test_col0_str, "test");
581 table_append_str(test_tbl, "bbbbb");
582 table_col_int(test_tbl, test_col1_int, -100);
583 table_col_uint(test_tbl, test_col2_uint, 100);
584 table_col_bool(test_tbl, test_col3_bool, 0);
585 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
586 table_end_row(test_tbl);
589 static void test_simple1(struct fastbuf *out)
591 table_init(&test_tbl);
593 // print table with header
594 table_set_col_order_by_name(&test_tbl, "col3_bool");
595 table_start(&test_tbl, out);
596 do_print1(&test_tbl);
597 table_end(&test_tbl);
599 // print the same table as in the previous case without header
600 table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
601 table_start(&test_tbl, out);
602 do_print1(&test_tbl);
603 table_end(&test_tbl);
605 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
606 test_tbl.print_header = 0;
607 table_start(&test_tbl, out);
608 do_print1(&test_tbl);
609 table_end(&test_tbl);
610 test_tbl.print_header = 1;
612 table_set_col_order_by_name(&test_tbl, "col3_bool");
613 table_start(&test_tbl, out);
614 do_print1(&test_tbl);
615 table_end(&test_tbl);
617 table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
618 table_start(&test_tbl, out);
619 do_print1(&test_tbl);
620 table_end(&test_tbl);
622 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
623 table_start(&test_tbl, out);
624 do_print1(&test_tbl);
625 table_end(&test_tbl);
627 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");
628 table_start(&test_tbl, out);
629 do_print1(&test_tbl);
630 table_end(&test_tbl);
632 table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
633 table_start(&test_tbl, out);
634 do_print1(&test_tbl);
635 table_end(&test_tbl);
637 table_cleanup(&test_tbl);
640 enum test_any_table_cols {
641 test_any_col0_int, test_any_col1_any
644 static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
646 static struct table test_any_tbl = {
648 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
649 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
652 TBL_COL_ORDER(test_any_column_order),
653 TBL_OUTPUT_HUMAN_READABLE,
654 TBL_COL_DELIMITER("\t"),
655 TBL_APPEND_DELIMITER(",")
658 static void test_any_type(struct fastbuf *out)
660 table_init(&test_any_tbl);
662 table_start(&test_any_tbl, out);
664 table_col_int(&test_any_tbl, test_any_col0_int, -10);
665 table_col_int(&test_any_tbl, test_any_col1_any, 10000);
666 table_end_row(&test_any_tbl);
668 table_col_int(&test_any_tbl, test_any_col0_int, -10);
669 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
670 table_end_row(&test_any_tbl);
672 table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
673 table_append_printf(&test_any_tbl, "%d", 20);
674 table_append_printf(&test_any_tbl, "%d", 30);
675 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
676 table_append_printf(&test_any_tbl, "%.2lf", 1.5);
677 table_append_printf(&test_any_tbl, "%.2lf", 1.6);
678 table_end_row(&test_any_tbl);
680 table_end(&test_any_tbl);
681 table_cleanup(&test_any_tbl);
684 int main(int argc UNUSED, char **argv UNUSED)
687 out = bfdopen_shared(1, 4096);