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, struct fastbuf *out)
21 int col_count = 0; // count the number of columns in the struct table
24 if(tbl->columns[col_count].name == NULL &&
25 tbl->columns[col_count].fmt == NULL &&
26 tbl->columns[col_count].width == 0 &&
27 tbl->columns[col_count].type == COL_TYPE_LAST)
29 ASSERT(tbl->columns[col_count].name != NULL);
30 ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl->columns[col_count].fmt != NULL);
31 ASSERT(tbl->columns[col_count].width != 0);
32 ASSERT(tbl->columns[col_count].type < COL_TYPE_LAST);
35 tbl->pool = mp_new(4096);
37 tbl->column_count = col_count;
40 tbl->formatter = &table_fmt_human_readable;
43 tbl->print_header = 1; // by default, print header
46 void table_cleanup(struct table *tbl)
49 memset(tbl, 0, sizeof(struct table));
52 // TODO: test default column order
53 static void table_make_default_column_order(struct table *tbl)
55 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count);
56 for(int i = 0; i < tbl->column_count; i++) {
59 table_col_order(tbl, col_order_int, tbl->column_count);
62 void table_start(struct table *tbl)
64 tbl->last_printed_col = -1;
65 tbl->row_printing_started = 0;
68 tbl->col_str_ptrs = mp_alloc_zero(tbl->pool, sizeof(char *) * tbl->column_count);
70 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
72 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
73 if(tbl->cols_to_output == 0) {
75 die("Table should output at least one column.");
78 mp_save(tbl->pool, &tbl->pool_state);
80 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
81 ASSERT_MSG(tbl->append_delimiter, "Append delimiter not specified.");
84 void table_end(struct table *tbl)
86 tbl->last_printed_col = -1;
87 tbl->row_printing_started = 0;
88 tbl->print_header = 1;
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 NULL;
114 // FIXME: This does not work! The start of the string may be reallocated later.
115 char *tmp = mp_printf(tbl->pool, "%s", tbl->columns[0].name);
117 for(int i = 1; i < tbl->column_count; i++) {
118 mp_printf_append(tbl->pool, tmp, ",%s", tbl->columns[i].name);
124 // FIXME: Shouldn't this be table_SET_col_order() ?
125 void table_col_order(struct table *tbl, int *col_order, int cols_to_output)
127 for(int i = 0; i < cols_to_output; i++) {
128 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);
131 tbl->column_order = col_order;
132 tbl->cols_to_output = cols_to_output;
136 * TODO: ERROR! this function deliberately causes memory leak. the
137 * problem is that when table_col_order_by_name is called multiple-times,
138 * the mp_save adds all the resulting column orders on the memory pool.
139 * The memory leak is small, but it is present.
141 int table_col_order_by_name(struct table *tbl, const char *col_order_str)
143 int col_order_len = strlen(col_order_str);
145 char *tmp_col_order = stk_strdup(col_order_str);
148 for(int i = 0; i < col_order_len; i++) {
149 if(col_order_str[i] == ',') {
154 struct mempool_state mp_tmp_state;
155 mp_save(tbl->pool, &mp_tmp_state);
157 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * col_count);
158 int curr_col_order_int = 0;
159 const char *name_start = tmp_col_order;
161 char *next = strchr(name_start, ',');
166 int idx = table_get_col_idx(tbl, name_start);
167 col_order_int[curr_col_order_int] = idx;
169 //ASSERT_MSG(idx != -1, "Table column with name '%s' does not exist.", name_start);
170 mp_restore(tbl->pool, &mp_tmp_state);
173 curr_col_order_int++;
178 tbl->column_order = col_order_int;
179 tbl->cols_to_output = curr_col_order_int;
183 /*** Table cells ***/
185 void table_set_printf(struct table *tbl, int col, const char *fmt, ...)
187 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
188 tbl->last_printed_col = col;
189 tbl->row_printing_started = 1;
192 tbl->col_str_ptrs[col] = mp_vprintf(tbl->pool, fmt, args);
196 static const char *table_set_col_default_fmts[] = {
197 [COL_TYPE_STR] = "%s",
198 [COL_TYPE_INT] = "%d",
199 [COL_TYPE_INTMAX] = "%jd",
200 [COL_TYPE_UINT] = "%u",
201 [COL_TYPE_UINTMAX] = "%ju",
202 [COL_TYPE_BOOL] = "%d",
203 [COL_TYPE_DOUBLE] = "%.2lf",
204 [COL_TYPE_ANY] = NULL,
205 [COL_TYPE_LAST] = NULL
208 #define TABLE_SET_COL(_name_, _type_, _typeconst_) void table_set_##_name_(struct table *tbl, int col, _type_ val) \
210 const char *fmt = tbl->columns[col].fmt;\
211 if(tbl->columns[col].type == COL_TYPE_ANY) {\
212 fmt = table_set_col_default_fmts[_typeconst_];\
214 table_set_##_name_##_fmt(tbl, col, fmt, val);\
217 #define TABLE_SET_COL_STR(_name_, _type_, _typeconst_) void table_set_##_name_##_name(struct table *tbl, const char *col_name, _type_ val) \
219 int col = table_get_col_idx(tbl, col_name);\
220 table_set_##_name_(tbl, col, val);\
223 #define TABLE_SET_COL_FMT(_name_, _type_, _typeconst_) void table_set_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
225 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
226 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
227 ASSERT(fmt != NULL);\
228 tbl->last_printed_col = col;\
229 tbl->row_printing_started = 1;\
230 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val);\
233 #define TABLE_SET(_name_, _type_, _typeconst_) TABLE_SET_COL(_name_, _type_, _typeconst_);\
234 TABLE_SET_COL_STR(_name_, _type_, _typeconst_);\
235 TABLE_SET_COL_FMT(_name_, _type_, _typeconst_);
237 TABLE_SET(int, int, COL_TYPE_INT)
238 TABLE_SET(uint, uint, COL_TYPE_UINT)
239 TABLE_SET(double, double, COL_TYPE_DOUBLE)
240 TABLE_SET(str, const char *, COL_TYPE_STR)
241 TABLE_SET(intmax, intmax_t, COL_TYPE_INTMAX)
242 TABLE_SET(uintmax, uintmax_t, COL_TYPE_UINTMAX)
243 #undef TABLE_SET_COL_FMT
244 #undef TABLE_SET_COL_STR
248 void table_set_bool(struct table *tbl, int col, uint val)
250 table_set_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
253 void table_set_bool_name(struct table *tbl, const char *col_name, uint val)
255 int col = table_get_col_idx(tbl, col_name);
256 table_set_bool(tbl, col, val);
259 void table_set_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
261 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
262 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
264 tbl->last_printed_col = col;
265 tbl->row_printing_started = 1;
266 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val ? "true" : "false");
269 #define TABLE_APPEND(_name_, _type_, _typeconst_) void table_append_##_name_(struct table *tbl, _type_ val) \
271 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);\
272 ASSERT(_typeconst_ == tbl->columns[tbl->last_printed_col].type);\
273 int col = tbl->last_printed_col;\
274 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);\
275 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val);\
278 TABLE_APPEND(int, int, COL_TYPE_INT)
279 TABLE_APPEND(uint, uint, COL_TYPE_UINT)
280 TABLE_APPEND(double, double, COL_TYPE_DOUBLE)
281 TABLE_APPEND(str, const char *, COL_TYPE_STR)
282 TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
283 TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
286 void table_append_bool(struct table *tbl, int val)
288 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
289 ASSERT(COL_TYPE_BOOL == tbl->columns[tbl->last_printed_col].type);
291 int col = tbl->last_printed_col;
293 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
295 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val ? "true" : "false");
298 void table_append_printf(struct table *tbl, const char *fmt, ...)
300 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
301 int col = tbl->last_printed_col;
306 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
307 tbl->col_str_ptrs[col] = mp_vprintf_append(tbl->pool, tbl->col_str_ptrs[col], fmt, args);
312 void table_end_row(struct table *tbl)
314 ASSERT(tbl->formatter->row_output);
315 tbl->formatter->row_output(tbl);
316 memset(tbl->col_str_ptrs, 0, sizeof(char *) * tbl->column_count);
317 mp_restore(tbl->pool, &tbl->pool_state);
318 tbl->last_printed_col = -1;
319 tbl->row_printing_started = 0;
322 /* Construction of a cell using a fastbuf */
324 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
326 fbpool_init(&tbl->fb_col_out);
327 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
329 return &tbl->fb_col_out.fb;
332 void table_col_fbend(struct table *tbl)
334 tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
338 /*** Option parsing ***/
340 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
342 // Options with no value
343 if(value == NULL || (value != NULL && strlen(value) == 0)) {
344 if(strcmp(key, "noheader") == 0) {
345 tbl->print_header = 0;
350 // Options with a value
352 if(strcmp(key, "header") == 0) {
353 // FIXME: Check syntax of value.
354 //tbl->print_header = strtol(value, NULL, 10); //atoi(value);
355 //if(errno != 0) tbl->print_header
357 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
358 uint tmp = value[0] - '0';
360 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
361 tbl->print_header = tmp;
363 } else if(strcmp(key, "cols") == 0) {
364 // FIXME: We should not exit/abort on errors caused from command line.
365 if(table_col_order_by_name(tbl, value) != 0) {
366 const char *tmp = table_get_col_list(tbl);
367 return mp_printf(tbl->pool, "Invalid tableprinter column list: possible column names are %s.", tmp);
370 } else if(strcmp(key, "fmt") == 0) {
371 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
372 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
374 return "Tableprinter: invalid argument to output-type option.";
377 } else if(strcmp(key, "col-delim") == 0) {
378 char * d = mp_printf(tbl->pool, "%s", value);
379 tbl->col_delimiter = d;
385 if(tbl->formatter && tbl->formatter->process_option) {
386 const char *err = NULL;
387 if (tbl->formatter->process_option(tbl, key, value, &err)) {
392 // Unrecognized option
393 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
396 const char *table_set_option(struct table *tbl, const char *opt)
398 char *key = stk_strdup(opt);
399 char *value = strchr(key, ':');
403 return table_set_option_value(tbl, key, value);
406 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
408 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
409 const char *rv = table_set_option(tbl, gary_table_opts[i]);
417 /*** Default formatter for human-readable output ***/
419 static void table_row_human_readable(struct table *tbl)
421 uint col = tbl->column_order[0];
422 int col_width = tbl->columns[col].width;
423 bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col]);
424 for(uint i = 1; i < tbl->cols_to_output; i++) {
425 col = tbl->column_order[i];
426 col_width = tbl->columns[col].width;
427 bputs(tbl->out, tbl->col_delimiter);
428 bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col]);
431 bputc(tbl->out, '\n');
434 static void table_write_header(struct table *tbl)
436 uint col_idx = tbl->column_order[0];
437 bprintf(tbl->out, "%*s", tbl->columns[col_idx].width, tbl->columns[col_idx].name);
439 for(uint i = 1; i < tbl->cols_to_output; i++) {
440 col_idx = tbl->column_order[i];
441 bputs(tbl->out, tbl->col_delimiter);
442 bprintf(tbl->out, "%*s", tbl->columns[col_idx].width, tbl->columns[col_idx].name);
445 bputc(tbl->out, '\n');
448 static void table_start_human_readable(struct table *tbl)
450 if(tbl->col_delimiter == NULL) {
451 tbl->col_delimiter = " ";
454 if(tbl->append_delimiter == NULL) {
455 tbl->append_delimiter = ",";
458 if(tbl->print_header != 0) {
459 tbl->print_header = 0;
460 table_write_header(tbl);
464 struct table_formatter table_fmt_human_readable = {
465 .row_output = table_row_human_readable,
466 .table_start = table_start_human_readable,
469 /*** Default formatter for machine-readable output ***/
471 static void table_row_machine_readable(struct table *tbl)
473 uint col = tbl->column_order[0];
474 bputs(tbl->out, tbl->col_str_ptrs[col]);
475 for(uint i = 1; i < tbl->cols_to_output; i++) {
476 col = tbl->column_order[i];
477 bputs(tbl->out, tbl->col_delimiter);
478 bputs(tbl->out, tbl->col_str_ptrs[col]);
481 bputc(tbl->out, '\n');
484 static void table_start_machine_readable(struct table *tbl)
486 if(tbl->col_delimiter == NULL) {
487 tbl->col_delimiter = ";";
490 if(tbl->append_delimiter == NULL) {
491 tbl->append_delimiter = ",";
494 if(tbl->print_header != 0) {
495 // FIXME: This magic is not needed, the value of print_header should be kept
496 // to the value set during table initialization (e.g., by command-line options)
497 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,
521 enum test_table_cols {
522 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
525 static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str};
527 static struct table test_tbl = {
529 TBL_COL_STR(test, col0_str, 20),
530 TBL_COL_INT(test, col1_int, 8),
531 TBL_COL_UINT(test, col2_uint, 9),
532 TBL_COL_BOOL(test, col3_bool, 9),
533 TBL_COL_DOUBLE(test, col4_double, 11, 2),
536 TBL_COL_ORDER(test_column_order),
537 TBL_OUTPUT_HUMAN_READABLE,
538 TBL_COL_DELIMITER("\t"),
539 TBL_APPEND_DELIMITER(",")
543 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
545 static void do_print1(struct table *test_tbl)
547 table_set_str(test_tbl, test_col0_str, "sdsdf");
548 table_append_str(test_tbl, "aaaaa");
549 table_set_int(test_tbl, test_col1_int, -10);
550 table_set_int(test_tbl, test_col1_int, 10000);
551 table_set_uint(test_tbl, test_col2_uint, 10);
552 table_set_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
553 table_set_bool(test_tbl, test_col3_bool, 1);
554 table_set_double(test_tbl, test_col4_double, 1.5);
555 table_set_printf(test_tbl, test_col4_double, "AAA");
556 table_end_row(test_tbl);
558 table_set_str(test_tbl, test_col0_str, "test");
559 table_append_str(test_tbl, "bbbbb");
560 table_set_int(test_tbl, test_col1_int, -100);
561 table_set_uint(test_tbl, test_col2_uint, 100);
562 table_set_bool(test_tbl, test_col3_bool, 0);
563 table_set_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
564 table_end_row(test_tbl);
567 static void test_simple1(struct fastbuf *out)
569 table_init(&test_tbl, out);
570 // print table with header
571 table_col_order_by_name(&test_tbl, "col3_bool");
572 table_start(&test_tbl);
573 do_print1(&test_tbl);
574 table_end(&test_tbl);
576 // print the same table as in the previous case without header
577 table_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
578 table_start(&test_tbl);
579 do_print1(&test_tbl);
580 table_end(&test_tbl);
582 // this also tests whether there is need to call table_col_order_by_name after table_end was called
583 test_tbl.print_header = 0;
584 table_start(&test_tbl);
585 do_print1(&test_tbl);
586 table_end(&test_tbl);
588 table_col_order_by_name(&test_tbl, "col3_bool");
589 table_start(&test_tbl);
590 do_print1(&test_tbl);
591 table_end(&test_tbl);
593 table_col_order_by_name(&test_tbl, "col3_bool,col0_str");
594 table_start(&test_tbl);
595 do_print1(&test_tbl);
596 table_end(&test_tbl);
598 table_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
599 table_start(&test_tbl);
600 do_print1(&test_tbl);
601 table_end(&test_tbl);
603 table_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
604 table_start(&test_tbl);
605 do_print1(&test_tbl);
606 table_end(&test_tbl);
608 table_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
609 table_start(&test_tbl);
610 do_print1(&test_tbl);
611 table_end(&test_tbl);
613 table_cleanup(&test_tbl);
616 enum test_any_table_cols {
617 test_any_col0_int, test_any_col1_any
620 static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
622 static struct table test_any_tbl = {
624 TBL_COL_INT(test_any, col0_int, 8),
625 TBL_COL_ANY(test_any, col1_any, 9),
628 TBL_COL_ORDER(test_any_column_order),
629 TBL_OUTPUT_HUMAN_READABLE,
630 TBL_COL_DELIMITER("\t"),
631 TBL_APPEND_DELIMITER(",")
634 static void test_any_type(struct fastbuf *out)
636 table_init(&test_any_tbl, out);
637 table_start(&test_any_tbl);
639 table_set_int(&test_any_tbl, test_any_col0_int, -10);
640 table_set_int(&test_any_tbl, test_any_col1_any, 10000);
641 table_end_row(&test_any_tbl);
643 table_set_int(&test_any_tbl, test_any_col0_int, -10);
644 table_set_double(&test_any_tbl, test_any_col1_any, 1.4);
645 table_end_row(&test_any_tbl);
647 table_set_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
648 table_append_printf(&test_any_tbl, "%d", 20);
649 table_append_printf(&test_any_tbl, "%d", 30);
650 table_set_double(&test_any_tbl, test_any_col1_any, 1.4);
651 table_append_printf(&test_any_tbl, "%.2lf", 1.5);
652 table_append_printf(&test_any_tbl, "%.2lf", 1.6);
653 table_end_row(&test_any_tbl);
655 table_end(&test_any_tbl);
656 table_cleanup(&test_any_tbl);
659 int main(int argc UNUSED, char **argv UNUSED)
662 out = bfdopen_shared(1, 4096);