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 void table_clean_row(struct table *tbl)
326 memset(tbl->col_str_ptrs, 0, sizeof(char *) * tbl->column_count);
327 mp_restore(tbl->pool, &tbl->pool_state);
328 tbl->last_printed_col = -1;
329 tbl->row_printing_started = 0;
332 /* Construction of a cell using a fastbuf */
334 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
336 fbpool_init(&tbl->fb_col_out);
337 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
339 return &tbl->fb_col_out.fb;
342 void table_col_fbend(struct table *tbl)
344 tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
348 /*** Option parsing ***/
350 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
352 // Options with no value
353 if(value == NULL || (value != NULL && strlen(value) == 0)) {
354 if(strcmp(key, "noheader") == 0) {
355 tbl->print_header = 0;
360 // Options with a value
362 if(strcmp(key, "header") == 0) {
364 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
365 uint tmp = value[0] - '0';
367 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
368 tbl->print_header = tmp;
370 } else if(strcmp(key, "cols") == 0) {
371 const char *err = table_set_col_order_by_name(tbl, value);
373 return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
376 } else if(strcmp(key, "fmt") == 0) {
377 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
378 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
379 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
381 return "Tableprinter: invalid argument to output-type option.";
384 } else if(strcmp(key, "col-delim") == 0) {
385 char * d = mp_printf(tbl->pool, "%s", value);
386 tbl->col_delimiter = d;
392 if(tbl->formatter && tbl->formatter->process_option) {
393 const char *err = NULL;
394 if (tbl->formatter->process_option(tbl, key, value, &err)) {
399 // Unrecognized option
400 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
403 const char *table_set_option(struct table *tbl, const char *opt)
405 char *key = stk_strdup(opt);
406 char *value = strchr(key, ':');
410 return table_set_option_value(tbl, key, value);
413 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
415 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
416 const char *rv = table_set_option(tbl, gary_table_opts[i]);
424 /*** Default formatter for human-readable output ***/
426 static void table_row_human_readable(struct table *tbl)
428 for(uint i = 0; i < tbl->cols_to_output; i++) {
429 int col_idx = tbl->column_order[i];
431 bputs(tbl->out, tbl->col_delimiter);
433 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
434 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
435 bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
437 bputc(tbl->out, '\n');
440 static void table_write_header(struct table *tbl)
442 for(uint i = 0; i < tbl->cols_to_output; i++) {
443 int col_idx = tbl->column_order[i];
445 bputs(tbl->out, tbl->col_delimiter);
447 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
448 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
449 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
451 bputc(tbl->out, '\n');
454 static void table_start_human_readable(struct table *tbl)
456 if(tbl->col_delimiter == NULL) {
457 tbl->col_delimiter = " ";
460 if(tbl->append_delimiter == NULL) {
461 tbl->append_delimiter = ",";
464 if(tbl->print_header != 0) {
465 table_write_header(tbl);
469 struct table_formatter table_fmt_human_readable = {
470 .row_output = table_row_human_readable,
471 .table_start = table_start_human_readable,
474 /*** Default formatter for machine-readable output ***/
476 static void table_row_machine_readable(struct table *tbl)
478 for(uint i = 0; i < tbl->cols_to_output; i++) {
479 int col_idx = tbl->column_order[i];
481 bputs(tbl->out, tbl->col_delimiter);
483 bputs(tbl->out, tbl->col_str_ptrs[col_idx]);
485 bputc(tbl->out, '\n');
488 static void table_start_machine_readable(struct table *tbl)
490 if(tbl->col_delimiter == NULL) {
491 tbl->col_delimiter = ";";
494 if(tbl->append_delimiter == NULL) {
495 tbl->append_delimiter = ",";
498 if(tbl->print_header != 0) {
499 uint col_idx = tbl->column_order[0];
500 bputs(tbl->out, tbl->columns[col_idx].name);
501 for(uint i = 1; i < tbl->cols_to_output; i++) {
502 col_idx = tbl->column_order[i];
503 bputs(tbl->out, tbl->col_delimiter);
504 bputs(tbl->out, tbl->columns[col_idx].name);
506 bputc(tbl->out, '\n');
510 struct table_formatter table_fmt_machine_readable = {
511 .row_output = table_row_machine_readable,
512 .table_start = table_start_machine_readable,
516 /*** Blockline formatter ***/
518 static void table_row_blockline_output(struct table *tbl)
520 for(uint i = 0; i < tbl->cols_to_output; i++) {
521 int col_idx = tbl->column_order[i];
522 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->col_str_ptrs[col_idx]);
524 bputc(tbl->out, '\n');
527 static void table_start_blockline(struct table *tbl)
529 if(tbl->col_delimiter == NULL) {
530 tbl->col_delimiter = " ";
533 if(tbl->append_delimiter == NULL) {
534 tbl->append_delimiter = ",";
538 struct table_formatter table_fmt_blockline = {
539 .row_output = table_row_blockline_output,
540 .table_start = table_start_blockline
551 enum test_table_cols {
552 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
555 static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str};
557 static struct table test_tbl = {
559 [test_col0_str] = TBL_COL_STR("col0_str", 20),
560 [test_col1_int] = TBL_COL_INT("col1_int", 8),
561 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
562 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
563 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
566 TBL_COL_ORDER(test_column_order),
567 TBL_OUTPUT_HUMAN_READABLE,
568 TBL_COL_DELIMITER("\t"),
569 TBL_APPEND_DELIMITER(",")
573 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
575 static void do_print1(struct table *test_tbl)
577 table_col_str(test_tbl, test_col0_str, "sdsdf");
578 table_append_str(test_tbl, "aaaaa");
579 table_col_int(test_tbl, test_col1_int, -10);
580 table_col_int(test_tbl, test_col1_int, 10000);
581 table_col_uint(test_tbl, test_col2_uint, 10);
582 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
583 table_col_bool(test_tbl, test_col3_bool, 1);
584 table_col_double(test_tbl, test_col4_double, 1.5);
585 table_col_printf(test_tbl, test_col4_double, "AAA");
586 table_end_row(test_tbl);
588 table_col_str(test_tbl, test_col0_str, "test");
589 table_append_str(test_tbl, "bbbbb");
590 table_col_int(test_tbl, test_col1_int, -100);
591 table_col_uint(test_tbl, test_col2_uint, 100);
592 table_col_bool(test_tbl, test_col3_bool, 0);
593 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
594 table_end_row(test_tbl);
597 static void test_simple1(struct fastbuf *out)
599 table_init(&test_tbl);
601 // print table with header
602 table_set_col_order_by_name(&test_tbl, "col3_bool");
603 table_start(&test_tbl, out);
604 do_print1(&test_tbl);
605 table_end(&test_tbl);
607 // print the same table as in the previous case without header
608 table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
609 table_start(&test_tbl, out);
610 do_print1(&test_tbl);
611 table_end(&test_tbl);
613 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
614 test_tbl.print_header = 0;
615 table_start(&test_tbl, out);
616 do_print1(&test_tbl);
617 table_end(&test_tbl);
618 test_tbl.print_header = 1;
620 table_set_col_order_by_name(&test_tbl, "col3_bool");
621 table_start(&test_tbl, out);
622 do_print1(&test_tbl);
623 table_end(&test_tbl);
625 table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
626 table_start(&test_tbl, out);
627 do_print1(&test_tbl);
628 table_end(&test_tbl);
630 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
631 table_start(&test_tbl, out);
632 do_print1(&test_tbl);
633 table_end(&test_tbl);
635 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");
636 table_start(&test_tbl, out);
637 do_print1(&test_tbl);
638 table_end(&test_tbl);
640 table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
641 table_start(&test_tbl, out);
642 do_print1(&test_tbl);
643 table_end(&test_tbl);
645 table_cleanup(&test_tbl);
648 enum test_any_table_cols {
649 test_any_col0_int, test_any_col1_any
652 static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
654 static struct table test_any_tbl = {
656 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
657 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
660 TBL_COL_ORDER(test_any_column_order),
661 TBL_OUTPUT_HUMAN_READABLE,
662 TBL_COL_DELIMITER("\t"),
663 TBL_APPEND_DELIMITER(",")
666 static void test_any_type(struct fastbuf *out)
668 table_init(&test_any_tbl);
670 table_start(&test_any_tbl, out);
672 table_col_int(&test_any_tbl, test_any_col0_int, -10);
673 table_col_int(&test_any_tbl, test_any_col1_any, 10000);
674 table_end_row(&test_any_tbl);
676 table_col_int(&test_any_tbl, test_any_col0_int, -10);
677 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
678 table_end_row(&test_any_tbl);
680 table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
681 table_append_printf(&test_any_tbl, "%d", 20);
682 table_append_printf(&test_any_tbl, "%d", 30);
683 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
684 table_append_printf(&test_any_tbl, "%.2lf", 1.5);
685 table_append_printf(&test_any_tbl, "%.2lf", 1.6);
686 table_end_row(&test_any_tbl);
688 table_end(&test_any_tbl);
689 table_cleanup(&test_any_tbl);
692 int main(int argc UNUSED, char **argv UNUSED)
695 out = bfdopen_shared(1, 4096);