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;
67 if(!tbl->col_str_ptrs) {
68 tbl->col_str_ptrs = mp_alloc_zero(tbl->pool, sizeof(char *) * tbl->column_count);
71 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
73 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
75 mp_save(tbl->pool, &tbl->pool_state);
77 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
78 ASSERT_MSG(tbl->append_delimiter, "Append delimiter not specified.");
81 void table_end(struct table *tbl)
83 tbl->last_printed_col = -1;
84 tbl->row_printing_started = 0;
86 mp_restore(tbl->pool, &tbl->pool_state);
88 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
91 /*** Configuration ***/
93 void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
98 int table_get_col_idx(struct table *tbl, const char *col_name)
100 for(int i = 0; i < tbl->column_count; i++) {
101 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
106 const char * table_get_col_list(struct table *tbl)
108 if(tbl->column_count == 0) return "";
110 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
112 for(int i = 1; i < tbl->column_count; i++) {
113 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
119 // FIXME: Shouldn't this be table_SET_col_order() ?
120 void table_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_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, 0);
138 tbl->cols_to_output = 0;
142 char *tmp_col_order = stk_strdup(col_order_str);
145 for(int i = 0; col_order_str[i] != 0; i++) {
146 if(col_order_str[i] == ',') {
151 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * col_count);
152 int curr_col_order_int = 0;
153 const char *name_start = tmp_col_order;
155 char *next = strchr(name_start, ',');
160 int idx = table_get_col_idx(tbl, name_start);
162 return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
164 col_order_int[curr_col_order_int++] = idx;
169 tbl->column_order = col_order_int;
170 tbl->cols_to_output = curr_col_order_int;
174 /*** Table cells ***/
176 void table_set_printf(struct table *tbl, int col, const char *fmt, ...)
178 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
179 tbl->last_printed_col = col;
180 tbl->row_printing_started = 1;
183 tbl->col_str_ptrs[col] = mp_vprintf(tbl->pool, fmt, args);
187 static const char *table_set_col_default_fmts[] = {
188 [COL_TYPE_STR] = "%s",
189 [COL_TYPE_INT] = "%d",
190 [COL_TYPE_INTMAX] = "%jd",
191 [COL_TYPE_UINT] = "%u",
192 [COL_TYPE_UINTMAX] = "%ju",
193 [COL_TYPE_BOOL] = "%d",
194 [COL_TYPE_DOUBLE] = "%.2lf",
195 [COL_TYPE_ANY] = NULL,
196 [COL_TYPE_LAST] = NULL
199 #define TABLE_SET_COL(_name_, _type_, _typeconst_) void table_set_##_name_(struct table *tbl, int col, _type_ val) \
201 const char *fmt = tbl->columns[col].fmt;\
202 if(tbl->columns[col].type == COL_TYPE_ANY) {\
203 fmt = table_set_col_default_fmts[_typeconst_];\
205 table_set_##_name_##_fmt(tbl, col, fmt, val);\
208 #define TABLE_SET_COL_STR(_name_, _type_, _typeconst_) void table_set_##_name_##_name(struct table *tbl, const char *col_name, _type_ val) \
210 int col = table_get_col_idx(tbl, col_name);\
211 table_set_##_name_(tbl, col, val);\
214 #define TABLE_SET_COL_FMT(_name_, _type_, _typeconst_) void table_set_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
216 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
217 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
218 ASSERT(fmt != NULL);\
219 tbl->last_printed_col = col;\
220 tbl->row_printing_started = 1;\
221 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val);\
224 #define TABLE_SET(_name_, _type_, _typeconst_) TABLE_SET_COL(_name_, _type_, _typeconst_);\
225 TABLE_SET_COL_STR(_name_, _type_, _typeconst_);\
226 TABLE_SET_COL_FMT(_name_, _type_, _typeconst_);
228 TABLE_SET(int, int, COL_TYPE_INT)
229 TABLE_SET(uint, uint, COL_TYPE_UINT)
230 TABLE_SET(double, double, COL_TYPE_DOUBLE)
231 TABLE_SET(str, const char *, COL_TYPE_STR)
232 TABLE_SET(intmax, intmax_t, COL_TYPE_INTMAX)
233 TABLE_SET(uintmax, uintmax_t, COL_TYPE_UINTMAX)
234 #undef TABLE_SET_COL_FMT
235 #undef TABLE_SET_COL_STR
239 void table_set_bool(struct table *tbl, int col, uint val)
241 table_set_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
244 void table_set_bool_name(struct table *tbl, const char *col_name, uint val)
246 int col = table_get_col_idx(tbl, col_name);
247 table_set_bool(tbl, col, val);
250 void table_set_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
252 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
253 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
255 tbl->last_printed_col = col;
256 tbl->row_printing_started = 1;
257 tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val ? "true" : "false");
260 #define TABLE_APPEND(_name_, _type_, _typeconst_) void table_append_##_name_(struct table *tbl, _type_ val) \
262 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);\
263 ASSERT(_typeconst_ == tbl->columns[tbl->last_printed_col].type);\
264 int col = tbl->last_printed_col;\
265 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);\
266 tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val);\
269 TABLE_APPEND(int, int, COL_TYPE_INT)
270 TABLE_APPEND(uint, uint, COL_TYPE_UINT)
271 TABLE_APPEND(double, double, COL_TYPE_DOUBLE)
272 TABLE_APPEND(str, const char *, COL_TYPE_STR)
273 TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
274 TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
277 void table_append_bool(struct table *tbl, int val)
279 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
280 ASSERT(COL_TYPE_BOOL == tbl->columns[tbl->last_printed_col].type);
282 int col = tbl->last_printed_col;
284 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 ? "true" : "false");
289 void table_append_printf(struct table *tbl, const char *fmt, ...)
291 ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
292 int col = tbl->last_printed_col;
297 mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
298 tbl->col_str_ptrs[col] = mp_vprintf_append(tbl->pool, tbl->col_str_ptrs[col], fmt, args);
303 void table_end_row(struct table *tbl)
305 ASSERT(tbl->formatter->row_output);
306 tbl->formatter->row_output(tbl);
307 memset(tbl->col_str_ptrs, 0, sizeof(char *) * tbl->column_count);
308 mp_restore(tbl->pool, &tbl->pool_state);
309 tbl->last_printed_col = -1;
310 tbl->row_printing_started = 0;
313 /* Construction of a cell using a fastbuf */
315 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
317 fbpool_init(&tbl->fb_col_out);
318 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
320 return &tbl->fb_col_out.fb;
323 void table_col_fbend(struct table *tbl)
325 tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
329 /*** Option parsing ***/
331 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
333 // Options with no value
334 if(value == NULL || (value != NULL && strlen(value) == 0)) {
335 if(strcmp(key, "noheader") == 0) {
336 tbl->print_header = 0;
341 // Options with a value
343 if(strcmp(key, "header") == 0) {
344 // FIXME: Check syntax of value.
345 //tbl->print_header = strtol(value, NULL, 10); //atoi(value);
346 //if(errno != 0) tbl->print_header
348 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
349 uint tmp = value[0] - '0';
351 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
352 tbl->print_header = tmp;
354 } else if(strcmp(key, "cols") == 0) {
355 const char *err = table_col_order_by_name(tbl, value);
357 return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
360 } else if(strcmp(key, "fmt") == 0) {
361 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
362 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
364 return "Tableprinter: invalid argument to output-type option.";
367 } else if(strcmp(key, "col-delim") == 0) {
368 char * d = mp_printf(tbl->pool, "%s", value);
369 tbl->col_delimiter = d;
375 if(tbl->formatter && tbl->formatter->process_option) {
376 const char *err = NULL;
377 if (tbl->formatter->process_option(tbl, key, value, &err)) {
382 // Unrecognized option
383 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
386 const char *table_set_option(struct table *tbl, const char *opt)
388 char *key = stk_strdup(opt);
389 char *value = strchr(key, ':');
393 return table_set_option_value(tbl, key, value);
396 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
398 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
399 const char *rv = table_set_option(tbl, gary_table_opts[i]);
407 /*** Default formatter for human-readable output ***/
409 static void table_row_human_readable(struct table *tbl)
411 for(uint i = 0; i < tbl->cols_to_output; i++) {
412 int col_idx = tbl->column_order[i];
413 int col_width = tbl->columns[col_idx].width;
415 bputs(tbl->out, tbl->col_delimiter);
417 bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
419 bputc(tbl->out, '\n');
422 static void table_write_header(struct table *tbl)
424 for(uint i = 0; i < tbl->cols_to_output; i++) {
425 int col_idx = tbl->column_order[i];
427 bputs(tbl->out, tbl->col_delimiter);
429 bprintf(tbl->out, "%*s", tbl->columns[col_idx].width, tbl->columns[col_idx].name);
431 bputc(tbl->out, '\n');
434 static void table_start_human_readable(struct table *tbl)
436 if(tbl->col_delimiter == NULL) {
437 tbl->col_delimiter = " ";
440 if(tbl->append_delimiter == NULL) {
441 tbl->append_delimiter = ",";
444 if(tbl->print_header != 0) {
445 table_write_header(tbl);
449 struct table_formatter table_fmt_human_readable = {
450 .row_output = table_row_human_readable,
451 .table_start = table_start_human_readable,
454 /*** Default formatter for machine-readable output ***/
456 static void table_row_machine_readable(struct table *tbl)
458 for(uint i = 0; i < tbl->cols_to_output; i++) {
459 int col_idx = tbl->column_order[i];
461 bputs(tbl->out, tbl->col_delimiter);
463 bputs(tbl->out, tbl->col_str_ptrs[col_idx]);
465 bputc(tbl->out, '\n');
468 static void table_start_machine_readable(struct table *tbl)
470 if(tbl->col_delimiter == NULL) {
471 tbl->col_delimiter = ";";
474 if(tbl->append_delimiter == NULL) {
475 tbl->append_delimiter = ",";
478 if(tbl->print_header != 0) {
479 uint col_idx = tbl->column_order[0];
480 bputs(tbl->out, tbl->columns[col_idx].name);
481 for(uint i = 1; i < tbl->cols_to_output; i++) {
482 col_idx = tbl->column_order[i];
483 bputs(tbl->out, tbl->col_delimiter);
484 bputs(tbl->out, tbl->columns[col_idx].name);
486 bputc(tbl->out, '\n');
490 struct table_formatter table_fmt_machine_readable = {
491 .row_output = table_row_machine_readable,
492 .table_start = table_start_machine_readable,
501 enum test_table_cols {
502 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
505 static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str};
507 static struct table test_tbl = {
509 TBL_COL_STR(test, col0_str, 20),
510 TBL_COL_INT(test, col1_int, 8),
511 TBL_COL_UINT(test, col2_uint, 9),
512 TBL_COL_BOOL(test, col3_bool, 9),
513 TBL_COL_DOUBLE(test, col4_double, 11, 2),
516 TBL_COL_ORDER(test_column_order),
517 TBL_OUTPUT_HUMAN_READABLE,
518 TBL_COL_DELIMITER("\t"),
519 TBL_APPEND_DELIMITER(",")
523 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
525 static void do_print1(struct table *test_tbl)
527 table_set_str(test_tbl, test_col0_str, "sdsdf");
528 table_append_str(test_tbl, "aaaaa");
529 table_set_int(test_tbl, test_col1_int, -10);
530 table_set_int(test_tbl, test_col1_int, 10000);
531 table_set_uint(test_tbl, test_col2_uint, 10);
532 table_set_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
533 table_set_bool(test_tbl, test_col3_bool, 1);
534 table_set_double(test_tbl, test_col4_double, 1.5);
535 table_set_printf(test_tbl, test_col4_double, "AAA");
536 table_end_row(test_tbl);
538 table_set_str(test_tbl, test_col0_str, "test");
539 table_append_str(test_tbl, "bbbbb");
540 table_set_int(test_tbl, test_col1_int, -100);
541 table_set_uint(test_tbl, test_col2_uint, 100);
542 table_set_bool(test_tbl, test_col3_bool, 0);
543 table_set_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
544 table_end_row(test_tbl);
547 static void test_simple1(struct fastbuf *out)
549 table_init(&test_tbl, out);
550 // print table with header
551 table_col_order_by_name(&test_tbl, "col3_bool");
552 table_start(&test_tbl);
553 do_print1(&test_tbl);
554 table_end(&test_tbl);
556 // print the same table as in the previous case without header
557 table_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
558 table_start(&test_tbl);
559 do_print1(&test_tbl);
560 table_end(&test_tbl);
562 // this also tests whether there is need to call table_col_order_by_name after table_end was called
563 test_tbl.print_header = 0;
564 table_start(&test_tbl);
565 do_print1(&test_tbl);
566 table_end(&test_tbl);
567 test_tbl.print_header = 1;
569 table_col_order_by_name(&test_tbl, "col3_bool");
570 table_start(&test_tbl);
571 do_print1(&test_tbl);
572 table_end(&test_tbl);
574 table_col_order_by_name(&test_tbl, "col3_bool,col0_str");
575 table_start(&test_tbl);
576 do_print1(&test_tbl);
577 table_end(&test_tbl);
579 table_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
580 table_start(&test_tbl);
581 do_print1(&test_tbl);
582 table_end(&test_tbl);
584 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");
585 table_start(&test_tbl);
586 do_print1(&test_tbl);
587 table_end(&test_tbl);
589 table_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
590 table_start(&test_tbl);
591 do_print1(&test_tbl);
592 table_end(&test_tbl);
594 table_cleanup(&test_tbl);
597 enum test_any_table_cols {
598 test_any_col0_int, test_any_col1_any
601 static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
603 static struct table test_any_tbl = {
605 TBL_COL_INT(test_any, col0_int, 8),
606 TBL_COL_ANY(test_any, col1_any, 9),
609 TBL_COL_ORDER(test_any_column_order),
610 TBL_OUTPUT_HUMAN_READABLE,
611 TBL_COL_DELIMITER("\t"),
612 TBL_APPEND_DELIMITER(",")
615 static void test_any_type(struct fastbuf *out)
617 table_init(&test_any_tbl, out);
618 table_start(&test_any_tbl);
620 table_set_int(&test_any_tbl, test_any_col0_int, -10);
621 table_set_int(&test_any_tbl, test_any_col1_any, 10000);
622 table_end_row(&test_any_tbl);
624 table_set_int(&test_any_tbl, test_any_col0_int, -10);
625 table_set_double(&test_any_tbl, test_any_col1_any, 1.4);
626 table_end_row(&test_any_tbl);
628 table_set_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
629 table_append_printf(&test_any_tbl, "%d", 20);
630 table_append_printf(&test_any_tbl, "%d", 30);
631 table_set_double(&test_any_tbl, test_any_col1_any, 1.4);
632 table_append_printf(&test_any_tbl, "%.2lf", 1.5);
633 table_append_printf(&test_any_tbl, "%.2lf", 1.6);
634 table_end_row(&test_any_tbl);
636 table_end(&test_any_tbl);
637 table_cleanup(&test_any_tbl);
640 int main(int argc UNUSED, char **argv UNUSED)
643 out = bfdopen_shared(1, 4096);