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;
160 table_update_ll(tbl);
163 bool table_col_is_printed(struct table *tbl, uint col_idx)
165 if(tbl->columns[col_idx].first_column == -1) return 0;
171 * TODO: This function deliberately leaks memory. When it is called multiple times,
172 * previous column orders still remain allocated in the table's memory pool.
174 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
176 if(col_order_str[0] == '*') {
177 int *col_order_int = alloca(sizeof(int) * tbl->column_count);
178 for(int i = 0; i < tbl->column_count; i++) {
179 col_order_int[i] = i;
181 table_set_col_order(tbl, col_order_int, tbl->column_count);
186 if(!col_order_str[0]) {
187 tbl->column_order = mp_alloc(tbl->pool, 0);
188 tbl->cols_to_output = 0;
192 char *tmp_col_order = stk_strdup(col_order_str);
195 for(int i = 0; col_order_str[i] != 0; i++) {
196 if(col_order_str[i] == ',') {
201 int *col_order_int = alloca(sizeof(int) * col_count);
202 int curr_col_order_int = 0;
203 const char *name_start = tmp_col_order;
205 char *next = strchr(name_start, ',');
210 int idx = table_get_col_idx(tbl, name_start);
212 return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
214 col_order_int[curr_col_order_int++] = idx;
219 table_set_col_order(tbl, col_order_int, curr_col_order_int);
223 /*** Table cells ***/
225 static void table_set_all_cols_content(struct table *tbl, int col, char *col_content, int override)
227 int curr_col = tbl->columns[col].first_column;
228 while(curr_col != -1) {
229 if(override == 0 && tbl->column_order[curr_col].output_type != 0) {
230 die("Error while setting content of all cells of a single type column, cell format should not be overriden.");
232 tbl->column_order[curr_col].cell_content = col_content;
233 curr_col = tbl->column_order[curr_col].next_column;
237 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
239 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
240 tbl->last_printed_col = col;
241 tbl->row_printing_started = 1;
244 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
245 table_set_all_cols_content(tbl, col, cell_content, 1);
249 static const char *table_col_default_fmts[] = {
250 [COL_TYPE_STR] = "%s",
251 [COL_TYPE_INT] = "%d",
252 [COL_TYPE_S64] = "%lld",
253 [COL_TYPE_INTMAX] = "%jd",
254 [COL_TYPE_UINT] = "%u",
255 [COL_TYPE_U64] = "%llu",
256 [COL_TYPE_UINTMAX] = "%ju",
257 [COL_TYPE_BOOL] = "%d",
258 [COL_TYPE_DOUBLE] = "%.2lf",
259 [COL_TYPE_ANY] = NULL,
260 [COL_TYPE_LAST] = NULL
263 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
265 const char *fmt = tbl->columns[col].fmt;\
266 if(tbl->columns[col].type == COL_TYPE_ANY) {\
267 fmt = table_col_default_fmts[_typeconst_];\
269 table_col_##_name_##_fmt(tbl, col, fmt, val);\
272 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
274 int col = table_get_col_idx(tbl, col_name);\
275 table_col_##_name_(tbl, col, val);\
278 #define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val UNUSED)\
280 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
281 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
282 ASSERT(fmt != NULL);\
283 tbl->last_printed_col = col;\
284 tbl->row_printing_started = 1;\
285 char *cell_content = mp_printf(tbl->pool, fmt, val);\
286 table_set_all_cols_content(tbl, col, cell_content, 0);\
289 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_);\
290 TABLE_COL_STR(_name_, _type_, _typeconst_);\
291 TABLE_COL_FMT(_name_, _type_, _typeconst_);
293 TABLE_COL_BODIES(int, int, COL_TYPE_INT)
294 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
295 TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
296 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
297 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
298 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
299 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
300 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
304 #undef TABLE_COL_BODIES
306 void table_col_bool(struct table *tbl, int col, uint val)
308 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
311 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
313 int col = table_get_col_idx(tbl, col_name);
314 table_col_bool(tbl, col, val);
317 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
319 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
320 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
322 tbl->last_printed_col = col;
323 tbl->row_printing_started = 1;
325 int curr_col = tbl->columns[col].first_column;
326 while(curr_col != -1) {
327 switch(tbl->column_order[curr_col].output_type) {
328 case CELL_OUT_HUMAN_READABLE:
329 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "true" : "false");
331 case CELL_OUT_MACHINE_READABLE:
332 // FIXME: this is just an example of printing in different formats
333 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "1" : "0");
336 die("Unsupported output type.");
338 curr_col = tbl->column_order[curr_col].next_column;
341 // FIXME: add to printing of all columns
344 void table_reset_row(struct table *tbl)
346 for(uint i = 0; i < tbl->cols_to_output; i++) {
347 tbl->column_order[i].cell_content = NULL;
349 mp_restore(tbl->pool, &tbl->pool_state);
350 tbl->last_printed_col = -1;
351 tbl->row_printing_started = 0;
354 void table_end_row(struct table *tbl)
356 ASSERT(tbl->formatter->row_output);
357 if(tbl->row_printing_started == 0) return;
358 tbl->formatter->row_output(tbl);
359 table_reset_row(tbl);
362 /* Construction of a cell using a fastbuf */
364 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
366 fbpool_init(&tbl->fb_col_out);
367 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
369 return &tbl->fb_col_out.fb;
372 void table_col_fbend(struct table *tbl)
374 char *cell_content = fbpool_end(&tbl->fb_col_out);
375 table_set_all_cols_content(tbl, tbl->col_out, cell_content, 1);
379 /*** Option parsing ***/
381 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
383 // Options with no value
384 if(value == NULL || (value != NULL && strlen(value) == 0)) {
385 if(strcmp(key, "noheader") == 0) {
386 tbl->print_header = 0;
391 // Options with a value
393 if(strcmp(key, "header") == 0) {
395 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
396 uint tmp = value[0] - '0';
398 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
399 tbl->print_header = tmp;
401 } else if(strcmp(key, "cols") == 0) {
402 const char *err = table_set_col_order_by_name(tbl, value);
404 return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
407 } else if(strcmp(key, "fmt") == 0) {
408 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
409 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
410 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
412 return "Tableprinter: invalid argument to output-type option.";
415 } else if(strcmp(key, "col-delim") == 0) {
416 char * d = mp_printf(tbl->pool, "%s", value);
417 tbl->col_delimiter = d;
423 if(tbl->formatter && tbl->formatter->process_option) {
424 const char *err = NULL;
425 if (tbl->formatter->process_option(tbl, key, value, &err)) {
430 // Unrecognized option
431 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
434 const char *table_set_option(struct table *tbl, const char *opt)
436 char *key = stk_strdup(opt);
437 char *value = strchr(key, ':');
441 return table_set_option_value(tbl, key, value);
444 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
446 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
447 const char *rv = table_set_option(tbl, gary_table_opts[i]);
455 /*** Default formatter for human-readable output ***/
457 static void table_row_human_readable(struct table *tbl)
459 for(uint i = 0; i < tbl->cols_to_output; i++) {
460 int col_idx = tbl->column_order[i].idx;
462 bputs(tbl->out, tbl->col_delimiter);
464 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
465 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
466 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
468 bputc(tbl->out, '\n');
471 static void table_write_header(struct table *tbl)
473 for(uint i = 0; i < tbl->cols_to_output; i++) {
474 int col_idx = tbl->column_order[i].idx;
476 bputs(tbl->out, tbl->col_delimiter);
478 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
479 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
480 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
482 bputc(tbl->out, '\n');
485 static void table_start_human_readable(struct table *tbl)
487 if(tbl->col_delimiter == NULL) {
488 tbl->col_delimiter = " ";
491 if(tbl->print_header != 0) {
492 table_write_header(tbl);
496 struct table_formatter table_fmt_human_readable = {
497 .row_output = table_row_human_readable,
498 .table_start = table_start_human_readable,
501 /*** Default formatter for machine-readable output ***/
503 static void table_row_machine_readable(struct table *tbl)
505 for(uint i = 0; i < tbl->cols_to_output; i++) {
507 bputs(tbl->out, tbl->col_delimiter);
509 bputs(tbl->out, tbl->column_order[i].cell_content);
511 bputc(tbl->out, '\n');
514 static void table_start_machine_readable(struct table *tbl)
516 if(tbl->col_delimiter == NULL) {
517 tbl->col_delimiter = "\t";
520 if(tbl->print_header != 0) {
521 uint col_idx = tbl->column_order[0].idx;
522 bputs(tbl->out, tbl->columns[col_idx].name);
523 for(uint i = 1; i < tbl->cols_to_output; i++) {
524 col_idx = tbl->column_order[i].idx;
525 bputs(tbl->out, tbl->col_delimiter);
526 bputs(tbl->out, tbl->columns[col_idx].name);
528 bputc(tbl->out, '\n');
532 struct table_formatter table_fmt_machine_readable = {
533 .row_output = table_row_machine_readable,
534 .table_start = table_start_machine_readable,
538 /*** Blockline formatter ***/
540 static void table_row_blockline_output(struct table *tbl)
542 for(uint i = 0; i < tbl->cols_to_output; i++) {
543 int col_idx = tbl->column_order[i].idx;
544 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->column_order[i].cell_content);
546 bputc(tbl->out, '\n');
549 static void table_start_blockline(struct table *tbl)
551 if(tbl->col_delimiter == NULL) {
552 tbl->col_delimiter = "\n";
556 struct table_formatter table_fmt_blockline = {
557 .row_output = table_row_blockline_output,
558 .table_start = table_start_blockline
569 enum test_table_cols {
570 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
573 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) };
575 static struct table test_tbl = {
577 [test_col0_str] = TBL_COL_STR("col0_str", 20),
578 [test_col1_int] = TBL_COL_INT("col1_int", 8),
579 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
580 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
581 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
584 TBL_COL_ORDER(test_column_order),
585 TBL_OUTPUT_HUMAN_READABLE,
586 TBL_COL_DELIMITER("\t"),
590 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
592 static void do_print1(struct table *test_tbl)
594 table_col_str(test_tbl, test_col0_str, "sdsdf");
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_col_int(test_tbl, test_col1_int, -100);
606 table_col_uint(test_tbl, test_col2_uint, 100);
607 table_col_bool(test_tbl, test_col3_bool, 0);
608 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
609 table_end_row(test_tbl);
612 static void test_simple1(struct fastbuf *out)
614 table_init(&test_tbl);
616 // print table with header
617 table_set_col_order_by_name(&test_tbl, "col3_bool");
618 table_start(&test_tbl, out);
619 do_print1(&test_tbl);
620 table_end(&test_tbl);
622 // print the same table as in the previous case without header
623 table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
624 table_start(&test_tbl, out);
625 do_print1(&test_tbl);
626 table_end(&test_tbl);
628 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
629 test_tbl.print_header = 0;
630 table_start(&test_tbl, out);
631 do_print1(&test_tbl);
632 table_end(&test_tbl);
633 test_tbl.print_header = 1;
635 table_set_col_order_by_name(&test_tbl, "col3_bool");
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, "col3_bool,col0_str");
641 table_start(&test_tbl, out);
642 do_print1(&test_tbl);
643 table_end(&test_tbl);
645 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
646 table_start(&test_tbl, out);
647 do_print1(&test_tbl);
648 table_end(&test_tbl);
650 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");
651 table_start(&test_tbl, out);
652 do_print1(&test_tbl);
653 table_end(&test_tbl);
655 table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
656 table_start(&test_tbl, out);
657 do_print1(&test_tbl);
658 table_end(&test_tbl);
660 table_cleanup(&test_tbl);
663 enum test_any_table_cols {
664 test_any_col0_int, test_any_col1_any
667 static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
669 static struct table test_any_tbl = {
671 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
672 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
675 TBL_COL_ORDER(test_any_column_order),
676 TBL_OUTPUT_HUMAN_READABLE,
677 TBL_COL_DELIMITER("\t"),
680 static void test_any_type(struct fastbuf *out)
682 table_init(&test_any_tbl);
684 table_start(&test_any_tbl, out);
686 table_col_int(&test_any_tbl, test_any_col0_int, -10);
687 table_col_int(&test_any_tbl, test_any_col1_any, 10000);
688 table_end_row(&test_any_tbl);
690 table_col_int(&test_any_tbl, test_any_col0_int, -10);
691 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
692 table_end_row(&test_any_tbl);
694 table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
695 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
696 table_end_row(&test_any_tbl);
698 table_end(&test_any_tbl);
699 table_cleanup(&test_any_tbl);
702 int main(int argc UNUSED, char **argv UNUSED)
705 out = bfdopen_shared(1, 4096);