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 = col_order[i];
135 //tbl->column_order[i].idx = col_idx;
136 //tbl->column_order[i].cell_content = NULL;
137 int col_idx = tbl->column_order[i].idx;//col_order[i];
138 int last = tbl->columns[col_idx].last_column;
140 tbl->columns[col_idx].last_column = i;
141 tbl->column_order[last].next_column = i;
143 tbl->columns[col_idx].last_column = i;
144 tbl->columns[col_idx].first_column = i;
146 tbl->column_order[i].next_column = -1;
150 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
152 for(int i = 0; i < cols_to_output; i++) {
153 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);
156 tbl->cols_to_output = cols_to_output;
157 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * cols_to_output);
158 for(int i = 0; i < cols_to_output; i++) {
159 int col_idx = col_order[i];
160 tbl->column_order[i].idx = col_idx;
161 tbl->column_order[i].cell_content = NULL;
163 table_update_ll(tbl);
166 bool table_col_is_printed(struct table *tbl, uint col_idx)
168 if(tbl->columns[col_idx].first_column == -1) return 0;
174 * TODO: This function deliberately leaks memory. When it is called multiple times,
175 * previous column orders still remain allocated in the table's memory pool.
177 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
179 if(col_order_str[0] == '*') {
180 int *col_order_int = alloca(sizeof(int) * tbl->column_count);
181 for(int i = 0; i < tbl->column_count; i++) {
182 col_order_int[i] = i;
184 table_set_col_order(tbl, col_order_int, tbl->column_count);
189 if(!col_order_str[0]) {
190 tbl->column_order = mp_alloc(tbl->pool, 0);
191 tbl->cols_to_output = 0;
195 char *tmp_col_order = stk_strdup(col_order_str);
198 for(int i = 0; col_order_str[i] != 0; i++) {
199 if(col_order_str[i] == ',') {
204 int *col_order_int = alloca(sizeof(int) * col_count);
205 int curr_col_order_int = 0;
206 const char *name_start = tmp_col_order;
208 char *next = strchr(name_start, ',');
213 int idx = table_get_col_idx(tbl, name_start);
215 return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
217 col_order_int[curr_col_order_int++] = idx;
222 table_set_col_order(tbl, col_order_int, curr_col_order_int);
226 /*** Table cells ***/
228 static void table_set_all_cols_content(struct table *tbl, int col, char *col_content, int override)
230 int curr_col = tbl->columns[col].first_column;
231 while(curr_col != -1) {
232 //fprintf(stdout, "curr_col: %d\n", curr_col);
234 if(override == 0 && tbl->column_order[curr_col].output_type != 0) {
235 die("Error while setting content of all cells of a single type column, cell format should not be overriden.");
237 tbl->column_order[curr_col].cell_content = col_content;
238 curr_col = tbl->column_order[curr_col].next_column;
242 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
244 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
245 tbl->last_printed_col = col;
246 tbl->row_printing_started = 1;
249 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
250 table_set_all_cols_content(tbl, col, cell_content, 1);
254 static const char *table_col_default_fmts[] = {
255 [COL_TYPE_STR] = "%s",
256 [COL_TYPE_INT] = "%d",
257 [COL_TYPE_S64] = "%lld",
258 [COL_TYPE_INTMAX] = "%jd",
259 [COL_TYPE_UINT] = "%u",
260 [COL_TYPE_U64] = "%llu",
261 [COL_TYPE_UINTMAX] = "%ju",
262 [COL_TYPE_BOOL] = "%d",
263 [COL_TYPE_DOUBLE] = "%.2lf",
264 [COL_TYPE_ANY] = NULL,
265 [COL_TYPE_LAST] = NULL
268 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
270 const char *fmt = tbl->columns[col].fmt;\
271 if(tbl->columns[col].type == COL_TYPE_ANY) {\
272 fmt = table_col_default_fmts[_typeconst_];\
274 table_col_##_name_##_fmt(tbl, col, fmt, val);\
277 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
279 int col = table_get_col_idx(tbl, col_name);\
280 table_col_##_name_(tbl, col, val);\
283 #define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val UNUSED)\
285 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
286 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
287 ASSERT(fmt != NULL);\
288 tbl->last_printed_col = col;\
289 tbl->row_printing_started = 1;\
290 char *cell_content = mp_printf(tbl->pool, fmt, val);\
291 table_set_all_cols_content(tbl, col, cell_content, 0);\
294 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_);\
295 TABLE_COL_STR(_name_, _type_, _typeconst_);\
296 TABLE_COL_FMT(_name_, _type_, _typeconst_);
298 TABLE_COL_BODIES(int, int, COL_TYPE_INT)
299 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
300 TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
301 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
302 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
303 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
304 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
305 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
309 #undef TABLE_COL_BODIES
311 void table_col_bool(struct table *tbl, int col, uint val)
313 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
316 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
318 int col = table_get_col_idx(tbl, col_name);
319 table_col_bool(tbl, col, val);
322 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
324 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
325 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
327 tbl->last_printed_col = col;
328 tbl->row_printing_started = 1;
330 int curr_col = tbl->columns[col].first_column;
331 while(curr_col != -1) {
332 switch(tbl->column_order[curr_col].output_type) {
333 case CELL_OUT_HUMAN_READABLE:
334 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "true" : "false");
336 case CELL_OUT_MACHINE_READABLE:
337 // FIXME: this is just an example of printing in different formats
338 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "1" : "0");
341 die("Unsupported output type.");
343 curr_col = tbl->column_order[curr_col].next_column;
346 // FIXME: add to printing of all columns
349 void table_reset_row(struct table *tbl)
351 for(uint i = 0; i < tbl->cols_to_output; i++) {
352 tbl->column_order[i].cell_content = NULL;
354 mp_restore(tbl->pool, &tbl->pool_state);
355 tbl->last_printed_col = -1;
356 tbl->row_printing_started = 0;
359 void table_end_row(struct table *tbl)
361 ASSERT(tbl->formatter->row_output);
362 if(tbl->row_printing_started == 0) return;
363 tbl->formatter->row_output(tbl);
364 table_reset_row(tbl);
367 /* Construction of a cell using a fastbuf */
369 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
371 fbpool_init(&tbl->fb_col_out);
372 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
374 return &tbl->fb_col_out.fb;
377 void table_col_fbend(struct table *tbl)
379 char *cell_content = fbpool_end(&tbl->fb_col_out);
380 table_set_all_cols_content(tbl, tbl->col_out, cell_content, 1);
384 /*** Option parsing ***/
386 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
388 // Options with no value
389 if(value == NULL || (value != NULL && strlen(value) == 0)) {
390 if(strcmp(key, "noheader") == 0) {
391 tbl->print_header = 0;
396 // Options with a value
398 if(strcmp(key, "header") == 0) {
400 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
401 uint tmp = value[0] - '0';
403 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
404 tbl->print_header = tmp;
406 } else if(strcmp(key, "cols") == 0) {
407 const char *err = table_set_col_order_by_name(tbl, value);
409 return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
412 } else if(strcmp(key, "fmt") == 0) {
413 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
414 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
415 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
417 return "Tableprinter: invalid argument to output-type option.";
420 } else if(strcmp(key, "col-delim") == 0) {
421 char * d = mp_printf(tbl->pool, "%s", value);
422 tbl->col_delimiter = d;
428 if(tbl->formatter && tbl->formatter->process_option) {
429 const char *err = NULL;
430 if (tbl->formatter->process_option(tbl, key, value, &err)) {
435 // Unrecognized option
436 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
439 const char *table_set_option(struct table *tbl, const char *opt)
441 char *key = stk_strdup(opt);
442 char *value = strchr(key, ':');
446 return table_set_option_value(tbl, key, value);
449 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
451 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
452 const char *rv = table_set_option(tbl, gary_table_opts[i]);
460 /*** Default formatter for human-readable output ***/
462 static void table_row_human_readable(struct table *tbl)
464 for(uint i = 0; i < tbl->cols_to_output; i++) {
465 int col_idx = tbl->column_order[i].idx;
467 bputs(tbl->out, tbl->col_delimiter);
469 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
470 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
471 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
473 bputc(tbl->out, '\n');
476 static void table_write_header(struct table *tbl)
478 for(uint i = 0; i < tbl->cols_to_output; i++) {
479 int col_idx = tbl->column_order[i].idx;
481 bputs(tbl->out, tbl->col_delimiter);
483 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
484 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
485 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
487 bputc(tbl->out, '\n');
490 static void table_start_human_readable(struct table *tbl)
492 if(tbl->col_delimiter == NULL) {
493 tbl->col_delimiter = " ";
496 if(tbl->print_header != 0) {
497 table_write_header(tbl);
501 struct table_formatter table_fmt_human_readable = {
502 .row_output = table_row_human_readable,
503 .table_start = table_start_human_readable,
506 /*** Default formatter for machine-readable output ***/
508 static void table_row_machine_readable(struct table *tbl)
510 for(uint i = 0; i < tbl->cols_to_output; i++) {
511 //int col_idx = tbl->column_order[i].idx;
513 bputs(tbl->out, tbl->col_delimiter);
515 bputs(tbl->out, tbl->column_order[i].cell_content);
517 bputc(tbl->out, '\n');
520 static void table_start_machine_readable(struct table *tbl)
522 if(tbl->col_delimiter == NULL) {
523 tbl->col_delimiter = "\t";
526 if(tbl->print_header != 0) {
527 uint col_idx = tbl->column_order[0].idx;
528 bputs(tbl->out, tbl->columns[col_idx].name);
529 for(uint i = 1; i < tbl->cols_to_output; i++) {
530 col_idx = tbl->column_order[i].idx;
531 bputs(tbl->out, tbl->col_delimiter);
532 bputs(tbl->out, tbl->columns[col_idx].name);
534 bputc(tbl->out, '\n');
538 struct table_formatter table_fmt_machine_readable = {
539 .row_output = table_row_machine_readable,
540 .table_start = table_start_machine_readable,
544 /*** Blockline formatter ***/
546 static void table_row_blockline_output(struct table *tbl)
548 for(uint i = 0; i < tbl->cols_to_output; i++) {
549 int col_idx = tbl->column_order[i].idx;
550 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->column_order[i].cell_content);
552 bputc(tbl->out, '\n');
555 static void table_start_blockline(struct table *tbl)
557 if(tbl->col_delimiter == NULL) {
558 tbl->col_delimiter = "\n";
562 struct table_formatter table_fmt_blockline = {
563 .row_output = table_row_blockline_output,
564 .table_start = table_start_blockline
575 enum test_table_cols {
576 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
579 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) };
581 static struct table test_tbl = {
583 [test_col0_str] = TBL_COL_STR("col0_str", 20),
584 [test_col1_int] = TBL_COL_INT("col1_int", 8),
585 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
586 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
587 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
590 TBL_COL_ORDER(test_column_order),
591 TBL_OUTPUT_HUMAN_READABLE,
592 TBL_COL_DELIMITER("\t"),
596 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
598 static void do_print1(struct table *test_tbl)
600 table_col_str(test_tbl, test_col0_str, "sdsdf");
601 table_col_int(test_tbl, test_col1_int, -10);
602 table_col_int(test_tbl, test_col1_int, 10000);
603 table_col_uint(test_tbl, test_col2_uint, 10);
604 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
605 table_col_bool(test_tbl, test_col3_bool, 1);
606 table_col_double(test_tbl, test_col4_double, 1.5);
607 table_col_printf(test_tbl, test_col4_double, "AAA");
608 table_end_row(test_tbl);
610 table_col_str(test_tbl, test_col0_str, "test");
611 table_col_int(test_tbl, test_col1_int, -100);
612 table_col_uint(test_tbl, test_col2_uint, 100);
613 table_col_bool(test_tbl, test_col3_bool, 0);
614 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
615 table_end_row(test_tbl);
618 static void test_simple1(struct fastbuf *out)
620 table_init(&test_tbl);
622 // print table with header
623 table_set_col_order_by_name(&test_tbl, "col3_bool");
624 table_start(&test_tbl, out);
625 do_print1(&test_tbl);
626 table_end(&test_tbl);
628 // print the same table as in the previous case without header
629 table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
630 table_start(&test_tbl, out);
631 do_print1(&test_tbl);
632 table_end(&test_tbl);
634 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
635 test_tbl.print_header = 0;
636 table_start(&test_tbl, out);
637 do_print1(&test_tbl);
638 table_end(&test_tbl);
639 test_tbl.print_header = 1;
641 table_set_col_order_by_name(&test_tbl, "col3_bool");
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, "col3_bool,col0_str");
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");
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,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
657 table_start(&test_tbl, out);
658 do_print1(&test_tbl);
659 table_end(&test_tbl);
661 table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
662 table_start(&test_tbl, out);
663 do_print1(&test_tbl);
664 table_end(&test_tbl);
666 table_cleanup(&test_tbl);
669 enum test_any_table_cols {
670 test_any_col0_int, test_any_col1_any
673 static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
675 static struct table test_any_tbl = {
677 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
678 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
681 TBL_COL_ORDER(test_any_column_order),
682 TBL_OUTPUT_HUMAN_READABLE,
683 TBL_COL_DELIMITER("\t"),
686 static void test_any_type(struct fastbuf *out)
688 table_init(&test_any_tbl);
690 table_start(&test_any_tbl, out);
692 table_col_int(&test_any_tbl, test_any_col0_int, -10);
693 table_col_int(&test_any_tbl, test_any_col1_any, 10000);
694 table_end_row(&test_any_tbl);
696 table_col_int(&test_any_tbl, test_any_col0_int, -10);
697 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
698 table_end_row(&test_any_tbl);
700 table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
701 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
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);