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 int table_col_is_printed(struct table *tbl, uint col_idx)
133 for(uint i = 0; i < tbl->cols_to_output; i++) {
134 if(tbl->column_order[i] == col_idx) return 1;
141 * TODO: This function deliberately leaks memory. When it is called multiple times,
142 * previous column orders still remain allocated in the table's memory pool.
144 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
146 if(col_order_str[0] == '*') {
147 tbl->column_order = mp_alloc(tbl->pool, sizeof(int) * tbl->column_count);
148 tbl->cols_to_output = tbl->column_count;
149 for(uint i = 0; i < tbl->cols_to_output; i++) tbl->column_order[i] = i;
154 if(!col_order_str[0]) {
155 tbl->column_order = mp_alloc(tbl->pool, 0);
156 tbl->cols_to_output = 0;
160 char *tmp_col_order = stk_strdup(col_order_str);
163 for(int i = 0; col_order_str[i] != 0; i++) {
164 if(col_order_str[i] == ',') {
169 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * col_count);
170 int curr_col_order_int = 0;
171 const char *name_start = tmp_col_order;
173 char *next = strchr(name_start, ',');
178 int idx = table_get_col_idx(tbl, name_start);
180 return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
182 col_order_int[curr_col_order_int++] = idx;
187 tbl->column_order = col_order_int;
188 tbl->cols_to_output = curr_col_order_int;
192 /*** Table cells ***/
194 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
196 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
197 tbl->last_printed_col = col;
198 tbl->row_printing_started = 1;
201 tbl->col_str_ptrs[col] = mp_vprintf(tbl->pool, fmt, args);
205 static const char *table_col_default_fmts[] = {
206 [COL_TYPE_STR] = "%s",
207 [COL_TYPE_INT] = "%d",
208 [COL_TYPE_INTMAX] = "%jd",
209 [COL_TYPE_UINT] = "%u",
210 [COL_TYPE_UINTMAX] = "%ju",
211 [COL_TYPE_BOOL] = "%d",
212 [COL_TYPE_DOUBLE] = "%.2lf",
213 [COL_TYPE_ANY] = NULL,
214 [COL_TYPE_LAST] = NULL
217 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val) \
219 const char *fmt = tbl->columns[col].fmt;\
220 if(tbl->columns[col].type == COL_TYPE_ANY) {\
221 fmt = table_col_default_fmts[_typeconst_];\
223 table_col_##_name_##_fmt(tbl, col, fmt, val);\
226 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val) \
228 int col = table_get_col_idx(tbl, col_name);\
229 table_col_##_name_(tbl, col, val);\
232 #define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
234 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
235 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
236 ASSERT(fmt != NULL);\
237 tbl->last_printed_col = col;\
238 tbl->row_printing_started = 1;\
239 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val);\
242 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_);\
243 TABLE_COL_STR(_name_, _type_, _typeconst_);\
244 TABLE_COL_FMT(_name_, _type_, _typeconst_);
246 TABLE_COL_BODIES(int, int, COL_TYPE_INT)
247 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
248 TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
249 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
250 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
251 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
252 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
253 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
257 #undef TABLE_COL_BODIES
259 void table_col_bool(struct table *tbl, int col, uint val)
261 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
264 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
266 int col = table_get_col_idx(tbl, col_name);
267 table_col_bool(tbl, col, val);
270 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
272 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
273 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
275 tbl->last_printed_col = col;
276 tbl->row_printing_started = 1;
277 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val ? "true" : "false");
280 #define TABLE_APPEND(_name_, _type_, _typeconst_) void table_append_##_name_(struct table *tbl, _type_ val) \
282 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);\
283 ASSERT(_typeconst_ == tbl->columns[tbl->last_printed_col].type);\
284 int col = tbl->last_printed_col;\
285 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);\
286 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val);\
289 TABLE_APPEND(int, int, COL_TYPE_INT)
290 TABLE_APPEND(uint, uint, COL_TYPE_UINT)
291 TABLE_APPEND(double, double, COL_TYPE_DOUBLE)
292 TABLE_APPEND(str, const char *, COL_TYPE_STR)
293 TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
294 TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
295 TABLE_APPEND(u64, u64, COL_TYPE_U64)
298 void table_append_bool(struct table *tbl, int val)
300 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
301 ASSERT(COL_TYPE_BOOL == tbl->columns[tbl->last_printed_col].type);
303 int col = tbl->last_printed_col;
305 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
307 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val ? "true" : "false");
310 void table_append_printf(struct table *tbl, const char *fmt, ...)
312 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
313 int col = tbl->last_printed_col;
318 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
319 tbl->col_str_ptrs[col] = mp_vprintf_append(tbl->pool, tbl->col_str_ptrs[col], fmt, args);
324 void table_reset_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 void table_end_row(struct table *tbl)
334 ASSERT(tbl->formatter->row_output);
335 if(tbl->row_printing_started == 0) return;
336 tbl->formatter->row_output(tbl);
337 table_reset_row(tbl);
340 /* Construction of a cell using a fastbuf */
342 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
344 fbpool_init(&tbl->fb_col_out);
345 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
347 return &tbl->fb_col_out.fb;
350 void table_col_fbend(struct table *tbl)
352 tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
356 /*** Option parsing ***/
358 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
360 // Options with no value
361 if(value == NULL || (value != NULL && strlen(value) == 0)) {
362 if(strcmp(key, "noheader") == 0) {
363 tbl->print_header = 0;
368 // Options with a value
370 if(strcmp(key, "header") == 0) {
372 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
373 uint tmp = value[0] - '0';
375 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
376 tbl->print_header = tmp;
378 } else if(strcmp(key, "cols") == 0) {
379 const char *err = table_set_col_order_by_name(tbl, value);
381 return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
384 } else if(strcmp(key, "fmt") == 0) {
385 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
386 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
387 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
389 return "Tableprinter: invalid argument to output-type option.";
392 } else if(strcmp(key, "col-delim") == 0) {
393 char * d = mp_printf(tbl->pool, "%s", value);
394 tbl->col_delimiter = d;
400 if(tbl->formatter && tbl->formatter->process_option) {
401 const char *err = NULL;
402 if (tbl->formatter->process_option(tbl, key, value, &err)) {
407 // Unrecognized option
408 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
411 const char *table_set_option(struct table *tbl, const char *opt)
413 char *key = stk_strdup(opt);
414 char *value = strchr(key, ':');
418 return table_set_option_value(tbl, key, value);
421 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
423 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
424 const char *rv = table_set_option(tbl, gary_table_opts[i]);
432 /*** Default formatter for human-readable output ***/
434 static void table_row_human_readable(struct table *tbl)
436 for(uint i = 0; i < tbl->cols_to_output; i++) {
437 int col_idx = tbl->column_order[i];
439 bputs(tbl->out, tbl->col_delimiter);
441 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
442 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
443 bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
445 bputc(tbl->out, '\n');
448 static void table_write_header(struct table *tbl)
450 for(uint i = 0; i < tbl->cols_to_output; i++) {
451 int col_idx = tbl->column_order[i];
453 bputs(tbl->out, tbl->col_delimiter);
455 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
456 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
457 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
459 bputc(tbl->out, '\n');
462 static void table_start_human_readable(struct table *tbl)
464 if(tbl->col_delimiter == NULL) {
465 tbl->col_delimiter = " ";
468 if(tbl->append_delimiter == NULL) {
469 tbl->append_delimiter = ",";
472 if(tbl->print_header != 0) {
473 table_write_header(tbl);
477 struct table_formatter table_fmt_human_readable = {
478 .row_output = table_row_human_readable,
479 .table_start = table_start_human_readable,
482 /*** Default formatter for machine-readable output ***/
484 static void table_row_machine_readable(struct table *tbl)
486 for(uint i = 0; i < tbl->cols_to_output; i++) {
487 int col_idx = tbl->column_order[i];
489 bputs(tbl->out, tbl->col_delimiter);
491 bputs(tbl->out, tbl->col_str_ptrs[col_idx]);
493 bputc(tbl->out, '\n');
496 static void table_start_machine_readable(struct table *tbl)
498 if(tbl->col_delimiter == NULL) {
499 tbl->col_delimiter = ";";
502 if(tbl->append_delimiter == NULL) {
503 tbl->append_delimiter = ",";
506 if(tbl->print_header != 0) {
507 uint col_idx = tbl->column_order[0];
508 bputs(tbl->out, tbl->columns[col_idx].name);
509 for(uint i = 1; i < tbl->cols_to_output; i++) {
510 col_idx = tbl->column_order[i];
511 bputs(tbl->out, tbl->col_delimiter);
512 bputs(tbl->out, tbl->columns[col_idx].name);
514 bputc(tbl->out, '\n');
518 struct table_formatter table_fmt_machine_readable = {
519 .row_output = table_row_machine_readable,
520 .table_start = table_start_machine_readable,
524 /*** Blockline formatter ***/
526 static void table_row_blockline_output(struct table *tbl)
528 for(uint i = 0; i < tbl->cols_to_output; i++) {
529 int col_idx = tbl->column_order[i];
530 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->col_str_ptrs[col_idx]);
532 bputc(tbl->out, '\n');
535 static void table_start_blockline(struct table *tbl)
537 if(tbl->col_delimiter == NULL) {
538 tbl->col_delimiter = " ";
541 if(tbl->append_delimiter == NULL) {
542 tbl->append_delimiter = ",";
546 struct table_formatter table_fmt_blockline = {
547 .row_output = table_row_blockline_output,
548 .table_start = table_start_blockline
559 enum test_table_cols {
560 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
563 static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str};
565 static struct table test_tbl = {
567 [test_col0_str] = TBL_COL_STR("col0_str", 20),
568 [test_col1_int] = TBL_COL_INT("col1_int", 8),
569 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
570 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
571 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
574 TBL_COL_ORDER(test_column_order),
575 TBL_OUTPUT_HUMAN_READABLE,
576 TBL_COL_DELIMITER("\t"),
577 TBL_APPEND_DELIMITER(",")
581 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
583 static void do_print1(struct table *test_tbl)
585 table_col_str(test_tbl, test_col0_str, "sdsdf");
586 table_append_str(test_tbl, "aaaaa");
587 table_col_int(test_tbl, test_col1_int, -10);
588 table_col_int(test_tbl, test_col1_int, 10000);
589 table_col_uint(test_tbl, test_col2_uint, 10);
590 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
591 table_col_bool(test_tbl, test_col3_bool, 1);
592 table_col_double(test_tbl, test_col4_double, 1.5);
593 table_col_printf(test_tbl, test_col4_double, "AAA");
594 table_end_row(test_tbl);
596 table_col_str(test_tbl, test_col0_str, "test");
597 table_append_str(test_tbl, "bbbbb");
598 table_col_int(test_tbl, test_col1_int, -100);
599 table_col_uint(test_tbl, test_col2_uint, 100);
600 table_col_bool(test_tbl, test_col3_bool, 0);
601 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
602 table_end_row(test_tbl);
605 static void test_simple1(struct fastbuf *out)
607 table_init(&test_tbl);
609 // print table with header
610 table_set_col_order_by_name(&test_tbl, "col3_bool");
611 table_start(&test_tbl, out);
612 do_print1(&test_tbl);
613 table_end(&test_tbl);
615 // print the same table as in the previous case without header
616 table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
617 table_start(&test_tbl, out);
618 do_print1(&test_tbl);
619 table_end(&test_tbl);
621 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
622 test_tbl.print_header = 0;
623 table_start(&test_tbl, out);
624 do_print1(&test_tbl);
625 table_end(&test_tbl);
626 test_tbl.print_header = 1;
628 table_set_col_order_by_name(&test_tbl, "col3_bool");
629 table_start(&test_tbl, out);
630 do_print1(&test_tbl);
631 table_end(&test_tbl);
633 table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
634 table_start(&test_tbl, out);
635 do_print1(&test_tbl);
636 table_end(&test_tbl);
638 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
639 table_start(&test_tbl, out);
640 do_print1(&test_tbl);
641 table_end(&test_tbl);
643 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");
644 table_start(&test_tbl, out);
645 do_print1(&test_tbl);
646 table_end(&test_tbl);
648 table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
649 table_start(&test_tbl, out);
650 do_print1(&test_tbl);
651 table_end(&test_tbl);
653 table_cleanup(&test_tbl);
656 enum test_any_table_cols {
657 test_any_col0_int, test_any_col1_any
660 static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
662 static struct table test_any_tbl = {
664 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
665 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
668 TBL_COL_ORDER(test_any_column_order),
669 TBL_OUTPUT_HUMAN_READABLE,
670 TBL_COL_DELIMITER("\t"),
671 TBL_APPEND_DELIMITER(",")
674 static void test_any_type(struct fastbuf *out)
676 table_init(&test_any_tbl);
678 table_start(&test_any_tbl, out);
680 table_col_int(&test_any_tbl, test_any_col0_int, -10);
681 table_col_int(&test_any_tbl, test_any_col1_any, 10000);
682 table_end_row(&test_any_tbl);
684 table_col_int(&test_any_tbl, test_any_col0_int, -10);
685 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
686 table_end_row(&test_any_tbl);
688 table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
689 table_append_printf(&test_any_tbl, "%d", 20);
690 table_append_printf(&test_any_tbl, "%d", 30);
691 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
692 table_append_printf(&test_any_tbl, "%.2lf", 1.5);
693 table_append_printf(&test_any_tbl, "%.2lf", 1.6);
694 table_end_row(&test_any_tbl);
696 table_end(&test_any_tbl);
697 table_cleanup(&test_any_tbl);
700 int main(int argc UNUSED, char **argv UNUSED)
703 out = bfdopen_shared(1, 4096);