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>
12 #include <ucw/strtonum.h>
17 /* Forward declarations */
19 static void table_update_ll(struct table *tbl);
21 /*** Management of tables ***/
23 static struct table *table_template_copy(struct table_template *tbl_template)
25 struct table *copy = mp_alloc_zero(tbl_template->pool, sizeof(struct table));
27 copy->column_count = tbl_template->column_count;
28 copy->pool = mp_new(4096);
29 if(tbl_template->column_order) {
30 copy->column_order = mp_alloc_zero(copy->pool, sizeof(struct table_col_info) * tbl_template->cols_to_output);
31 memcpy(copy->column_order, tbl_template->column_order, sizeof(struct table_col_info) * tbl_template->cols_to_output);
32 for(uint i = 0; i < copy->cols_to_output; i++) {
33 copy->column_order[i].cell_content = NULL;
34 copy->column_order[i].col_def = NULL;
35 copy->column_order[i].output_type = tbl_template->column_order[i].output_type;
38 copy->cols_to_output = tbl_template->cols_to_output;
41 copy->columns = tbl_template->columns;
43 copy->col_delimiter = tbl_template->col_delimiter;
44 copy->print_header = 1;
46 copy->last_printed_col = -1;
47 copy->row_printing_started = 0;
49 copy->formatter = tbl_template->formatter;
54 struct table *table_init(struct table_template *tbl_template)
56 int col_count = 0; // count the number of columns in the struct table
58 if(!tbl_template->pool) {
59 tbl_template->pool = mp_new(4096);
62 struct table *tbl = table_template_copy(tbl_template);
65 if(tbl->columns[col_count].name == NULL &&
66 tbl->columns[col_count].fmt == NULL &&
67 tbl->columns[col_count].width == 0 &&
68 tbl->columns[col_count].type == COL_TYPE_LAST)
70 ASSERT(tbl->columns[col_count].name != NULL);
71 ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl_template->columns[col_count].fmt != NULL);
72 ASSERT(tbl->columns[col_count].width != 0);
77 tbl->column_count = col_count;
80 tbl->formatter = &table_fmt_human_readable;
83 tbl->print_header = 1; // by default, print header
87 void table_cleanup(struct table *tbl)
90 memset(tbl, 0, sizeof(struct table));
93 // TODO: test default column order
94 static void table_make_default_column_order(struct table *tbl)
96 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count); // FIXME: use stack instead of memory pool
97 for(int i = 0; i < tbl->column_count; i++) {
100 table_set_col_order(tbl, col_order_int, tbl->column_count);
103 void table_start(struct table *tbl, struct fastbuf *out)
105 tbl->last_printed_col = -1;
106 tbl->row_printing_started = 0;
109 ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
111 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
113 // update linked lists
114 table_update_ll(tbl);
116 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
118 mp_save(tbl->pool, &tbl->pool_state);
120 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
123 void table_end(struct table *tbl)
125 tbl->last_printed_col = -1;
126 tbl->row_printing_started = 0;
128 mp_restore(tbl->pool, &tbl->pool_state);
130 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
133 /*** Configuration ***/
135 void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
137 tbl->formatter = fmt;
140 int table_get_col_idx(struct table *tbl, const char *col_name)
142 for(int i = 0; i < tbl->column_count; i++) {
143 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
148 const char * table_get_col_list(struct table *tbl)
150 if(tbl->column_count == 0) return "";
152 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
154 for(int i = 1; i < tbl->column_count; i++) {
155 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
161 static void table_update_ll(struct table *tbl)
163 int cols_to_output = tbl->cols_to_output;
165 for(int i = 0; i < tbl->column_count; i++) {
166 tbl->columns[i].first_column = -1;
167 tbl->columns[i].last_column = -1;
170 for(int i = 0; i < cols_to_output; i++) {
171 int idx = tbl->column_order[i].idx;
172 tbl->column_order[i].col_def = tbl->columns + idx;
175 for(int i = 0; i < cols_to_output; i++) {
176 int last = tbl->column_order[i].col_def->last_column;
178 tbl->column_order[i].col_def->last_column = i;
179 tbl->column_order[last].next_column = i;
181 tbl->column_order[i].col_def->last_column = i;
182 tbl->column_order[i].col_def->first_column = i;
184 tbl->column_order[i].next_column = -1;
188 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
190 for(int i = 0; i < cols_to_output; i++) {
191 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);
194 tbl->cols_to_output = cols_to_output;
195 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * cols_to_output);
196 for(int i = 0; i < cols_to_output; i++) {
197 int col_idx = col_order[i];
198 tbl->column_order[i].idx = col_idx;
199 tbl->column_order[i].col_def = tbl->columns + col_idx;
200 tbl->column_order[i].cell_content = NULL;
201 tbl->column_order[i].output_type = CELL_OUT_UNINITIALIZED;
203 table_update_ll(tbl);
206 bool table_col_is_printed(struct table *tbl, uint col_idx)
208 if(tbl->columns[col_idx].first_column == -1) return 0;
213 static char * table_parse_col_arg(char *col_def)
215 // FIXME: should be switched to str_sepsplit
216 char * left_br = strchr(col_def, '[');
217 if(left_br == NULL) return NULL;
220 char *right_br = strchr(left_br, ']');
226 * Setting options for basic table types (as defined in table.h)
228 bool table_set_col_opt_default(struct table *tbl, int col_copy_idx, const char *col_arg, char **err)
230 struct table_column *tmp_col = tbl->column_order[col_copy_idx].col_def;
231 int col_type_idx = tbl->column_order[col_copy_idx].idx;
233 if(tmp_col->type == COL_TYPE_DOUBLE) {
235 str_to_uint(&precision, col_arg, NULL, 0);
236 tbl->column_order[col_type_idx].output_type = precision;
240 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
245 * TODO: This function deliberately leaks memory. When it is called multiple times,
246 * previous column orders still remain allocated in the table's memory pool.
248 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
250 if(col_order_str[0] == '*') {
251 int *col_order_int = alloca(sizeof(int) * tbl->column_count);
252 for(int i = 0; i < tbl->column_count; i++) {
253 col_order_int[i] = i;
255 table_set_col_order(tbl, col_order_int, tbl->column_count);
260 if(!col_order_str[0]) {
261 tbl->column_order = mp_alloc(tbl->pool, 0);
262 tbl->cols_to_output = 0;
266 char *tmp_col_order = stk_strdup(col_order_str);
269 for(int i = 0; col_order_str[i] != 0; i++) {
270 if(col_order_str[i] == ',') {
275 tbl->cols_to_output = col_count;
276 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * col_count);
278 int curr_col_idx = 0;
279 char *name_start = tmp_col_order;
281 char *next = strchr(name_start, ',');
286 char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
287 int col_idx = table_get_col_idx(tbl, name_start);
290 return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
292 tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
293 tbl->column_order[curr_col_idx].idx = col_idx;
294 tbl->column_order[curr_col_idx].cell_content = NULL;
295 tbl->column_order[curr_col_idx].output_type = CELL_OUT_UNINITIALIZED;
296 if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].type_def->set_col_instance_option) {
298 tbl->columns[col_idx].type_def->set_col_instance_option(tbl, curr_col_idx, arg, &err);
299 if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
306 table_update_ll(tbl);
311 /*** Table cells ***/
313 static void table_set_all_cols_content(struct table *tbl, int col, char *col_content, int override)
315 int curr_col = tbl->columns[col].first_column;
316 while(curr_col != -1) {
317 if(override == 0 && tbl->column_order[curr_col].output_type != CELL_OUT_UNINITIALIZED ) {
318 die("Error while setting content of all cells of a single type column, cell format should not be overriden.");
320 tbl->column_order[curr_col].cell_content = col_content;
321 curr_col = tbl->column_order[curr_col].next_column;
325 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
327 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
328 tbl->last_printed_col = col;
329 tbl->row_printing_started = 1;
332 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
333 table_set_all_cols_content(tbl, col, cell_content, 1);
337 static const char *table_col_default_fmts[] = {
338 [COL_TYPE_STR] = "%s",
339 [COL_TYPE_INT] = "%d",
340 [COL_TYPE_S64] = "%lld",
341 [COL_TYPE_INTMAX] = "%jd",
342 [COL_TYPE_UINT] = "%u",
343 [COL_TYPE_U64] = "%llu",
344 [COL_TYPE_UINTMAX] = "%ju",
345 [COL_TYPE_BOOL] = "%d",
346 [COL_TYPE_DOUBLE] = "%.2lf",
347 [COL_TYPE_ANY] = NULL,
348 [COL_TYPE_LAST] = NULL
351 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
353 const char *fmt = tbl->columns[col].fmt;\
354 if(tbl->columns[col].type == COL_TYPE_ANY) {\
355 fmt = table_col_default_fmts[_typeconst_];\
357 table_col_##_name_##_fmt(tbl, col, fmt, val);\
360 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
362 int col = table_get_col_idx(tbl, col_name);\
363 table_col_##_name_(tbl, col, val);\
366 #define TABLE_COL_FMT(_name_, _type_, _typeconst_, _override) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val) \
368 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
369 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
370 ASSERT(fmt != NULL);\
371 tbl->last_printed_col = col;\
372 tbl->row_printing_started = 1;\
373 char *cell_content = mp_printf(tbl->pool, fmt, val);\
374 table_set_all_cols_content(tbl, col, cell_content, _override);\
377 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_, _override) TABLE_COL(_name_, _type_, _typeconst_); \
378 TABLE_COL_STR(_name_, _type_, _typeconst_);\
379 TABLE_COL_FMT(_name_, _type_, _typeconst_, _override);
381 TABLE_COL_BODIES(int, int, COL_TYPE_INT, 0)
382 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT, 0)
383 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR, 1)
384 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX, 0)
385 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX, 0)
386 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64, 0)
387 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64, 0)
389 // column type double is a special case
390 TABLE_COL(double, double, COL_TYPE_DOUBLE);
391 TABLE_COL_STR(double, double, COL_TYPE_DOUBLE);
395 #undef TABLE_COL_BODIES
397 void table_col_double_fmt(struct table *tbl, int col, const char *fmt, double val)
399 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
400 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_DOUBLE == tbl->columns[col].type);
402 tbl->last_printed_col = col;
403 tbl->row_printing_started = 1;
404 char *cell_content = mp_printf(tbl->pool, fmt, val);
405 int curr_col = tbl->columns[col].first_column;
406 while(curr_col != -1) {
407 if(tbl->column_order[curr_col].output_type < 0) tbl->column_order[curr_col].cell_content = cell_content;
409 char *cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val);
410 tbl->column_order[curr_col].cell_content = cell_content_tmp;
412 curr_col = tbl->column_order[curr_col].next_column;
416 void table_col_bool(struct table *tbl, int col, uint val)
418 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
421 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
423 int col = table_get_col_idx(tbl, col_name);
424 table_col_bool(tbl, col, val);
427 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
429 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
430 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
432 tbl->last_printed_col = col;
433 tbl->row_printing_started = 1;
435 int curr_col = tbl->columns[col].first_column;
436 while(curr_col != -1) {
437 switch(tbl->column_order[curr_col].output_type) {
438 case CELL_OUT_HUMAN_READABLE:
439 case CELL_OUT_UNINITIALIZED:
440 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "true" : "false");
442 case CELL_OUT_MACHINE_READABLE:
443 // FIXME: this is just an example of printing in different formats
444 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "1" : "0");
447 die("Unsupported output type.");
449 curr_col = tbl->column_order[curr_col].next_column;
453 void table_reset_row(struct table *tbl)
455 for(uint i = 0; i < tbl->cols_to_output; i++) {
456 tbl->column_order[i].cell_content = NULL;
458 mp_restore(tbl->pool, &tbl->pool_state);
459 tbl->last_printed_col = -1;
460 tbl->row_printing_started = 0;
463 void table_end_row(struct table *tbl)
465 ASSERT(tbl->formatter->row_output);
466 if(tbl->row_printing_started == 0) return;
467 tbl->formatter->row_output(tbl);
468 table_reset_row(tbl);
471 /* Construction of a cell using a fastbuf */
473 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
475 fbpool_init(&tbl->fb_col_out);
476 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
478 return &tbl->fb_col_out.fb;
481 void table_col_fbend(struct table *tbl)
483 char *cell_content = fbpool_end(&tbl->fb_col_out);
484 table_set_all_cols_content(tbl, tbl->col_out, cell_content, 1);
488 /*** Option parsing ***/
490 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
492 // Options with no value
493 if(value == NULL || (value != NULL && strlen(value) == 0)) {
494 if(strcmp(key, "noheader") == 0) {
495 tbl->print_header = 0;
500 // Options with a value
502 if(strcmp(key, "header") == 0) {
504 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
505 uint tmp = value[0] - '0';
507 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
508 tbl->print_header = tmp;
510 } else if(strcmp(key, "cols") == 0) {
511 const char *err = table_set_col_order_by_name(tbl, value);
513 } else if(strcmp(key, "fmt") == 0) {
514 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
515 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
516 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
518 return "Tableprinter: invalid argument to output-type option.";
521 } else if(strcmp(key, "col-delim") == 0) {
522 char * d = mp_printf(tbl->pool, "%s", value);
523 tbl->col_delimiter = d;
529 if(tbl->formatter && tbl->formatter->process_option) {
530 const char *err = NULL;
531 if(tbl->formatter->process_option(tbl, key, value, &err)) {
536 // Unrecognized option
537 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
540 const char *table_set_option(struct table *tbl, const char *opt)
542 char *key = stk_strdup(opt);
543 char *value = strchr(key, ':');
547 return table_set_option_value(tbl, key, value);
550 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
552 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
553 const char *rv = table_set_option(tbl, gary_table_opts[i]);
561 /*** Default formatter for human-readable output ***/
563 static void table_row_human_readable(struct table *tbl)
565 for(uint i = 0; i < tbl->cols_to_output; i++) {
566 int col_idx = tbl->column_order[i].idx;
568 bputs(tbl->out, tbl->col_delimiter);
570 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
571 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
572 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
574 bputc(tbl->out, '\n');
577 static void table_write_header(struct table *tbl)
579 for(uint i = 0; i < tbl->cols_to_output; i++) {
580 int col_idx = tbl->column_order[i].col_def - tbl->columns;
582 bputs(tbl->out, tbl->col_delimiter);
584 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
585 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
586 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
588 bputc(tbl->out, '\n');
591 static void table_start_human_readable(struct table *tbl)
593 if(tbl->col_delimiter == NULL) {
594 tbl->col_delimiter = " ";
597 if(tbl->print_header != 0) {
598 table_write_header(tbl);
602 struct table_formatter table_fmt_human_readable = {
603 .row_output = table_row_human_readable,
604 .table_start = table_start_human_readable,
607 /*** Default formatter for machine-readable output ***/
609 static void table_row_machine_readable(struct table *tbl)
611 for(uint i = 0; i < tbl->cols_to_output; i++) {
613 bputs(tbl->out, tbl->col_delimiter);
615 bputs(tbl->out, tbl->column_order[i].cell_content);
617 bputc(tbl->out, '\n');
620 static void table_start_machine_readable(struct table *tbl)
622 if(tbl->col_delimiter == NULL) {
623 tbl->col_delimiter = "\t";
626 if(tbl->print_header != 0) {
627 uint col_idx = tbl->column_order[0].col_def - tbl->columns;
628 bputs(tbl->out, tbl->columns[col_idx].name);
629 for(uint i = 1; i < tbl->cols_to_output; i++) {
630 col_idx = tbl->column_order[i].col_def - tbl->columns;
631 bputs(tbl->out, tbl->col_delimiter);
632 bputs(tbl->out, tbl->columns[col_idx].name);
634 bputc(tbl->out, '\n');
638 struct table_formatter table_fmt_machine_readable = {
639 .row_output = table_row_machine_readable,
640 .table_start = table_start_machine_readable,
644 /*** Blockline formatter ***/
646 static void table_row_blockline_output(struct table *tbl)
648 for(uint i = 0; i < tbl->cols_to_output; i++) {
649 int col_idx = tbl->column_order[i].idx;
650 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->column_order[i].cell_content);
652 bputc(tbl->out, '\n');
655 static void table_start_blockline(struct table *tbl)
657 if(tbl->col_delimiter == NULL) {
658 tbl->col_delimiter = "\n";
662 struct table_formatter table_fmt_blockline = {
663 .row_output = table_row_blockline_output,
664 .table_start = table_start_blockline
675 enum test_table_cols {
676 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
679 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) };
681 static struct table_template test_tbl = {
683 [test_col0_str] = TBL_COL_STR("col0_str", 20),
684 [test_col1_int] = TBL_COL_INT("col1_int", 8),
685 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
686 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
687 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
690 TBL_COL_ORDER(test_column_order),
691 TBL_OUTPUT_HUMAN_READABLE,
692 TBL_COL_DELIMITER("\t"),
696 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
698 static void do_print1(struct table *test_tbl)
700 table_col_str(test_tbl, test_col0_str, "sdsdf");
701 table_col_int(test_tbl, test_col1_int, -10);
702 table_col_int(test_tbl, test_col1_int, 10000);
703 table_col_uint(test_tbl, test_col2_uint, 10);
704 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
705 table_col_bool(test_tbl, test_col3_bool, 1);
706 table_col_double(test_tbl, test_col4_double, 1.5);
707 table_col_printf(test_tbl, test_col4_double, "AAA");
708 table_end_row(test_tbl);
710 table_col_str(test_tbl, test_col0_str, "test");
711 table_col_int(test_tbl, test_col1_int, -100);
712 table_col_uint(test_tbl, test_col2_uint, 100);
713 table_col_bool(test_tbl, test_col3_bool, 0);
714 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
715 table_end_row(test_tbl);
718 static void test_simple1(struct fastbuf *out)
720 struct table *tbl = table_init(&test_tbl);
722 // print table with header
723 table_set_col_order_by_name(tbl, "col3_bool");
724 table_start(tbl, out);
728 // print the same table as in the previous case without header
729 table_set_col_order_by_name(tbl, "col0_str,col2_uint,col1_int,col3_bool");
730 table_start(tbl, out);
734 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
735 tbl->print_header = 0;
736 table_start(tbl, out);
739 tbl->print_header = 1;
741 table_set_col_order_by_name(tbl, "col3_bool");
742 table_start(tbl, out);
746 table_set_col_order_by_name(tbl, "col3_bool,col0_str");
747 table_start(tbl, out);
751 table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint");
752 table_start(tbl, out);
756 table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
757 table_start(tbl, out);
761 table_set_col_order_by_name(tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
762 table_start(tbl, out);
769 enum test_any_table_cols {
770 test_any_col0_int, test_any_col1_any
773 static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
775 static struct table_template test_any_tbl = {
777 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
778 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
781 TBL_COL_ORDER(test_any_column_order),
782 TBL_OUTPUT_HUMAN_READABLE,
783 TBL_COL_DELIMITER("\t"),
786 static void test_any_type(struct fastbuf *out)
788 struct table *tbl = table_init(&test_any_tbl);
790 table_start(tbl, out);
792 table_col_int(tbl, test_any_col0_int, -10);
793 table_col_int(tbl, test_any_col1_any, 10000);
796 table_col_int(tbl, test_any_col0_int, -10);
797 table_col_double(tbl, test_any_col1_any, 1.4);
800 table_col_printf(tbl, test_any_col0_int, "%d", 10);
801 table_col_double(tbl, test_any_col1_any, 1.4);
808 int main(int argc UNUSED, char **argv UNUSED)
811 out = bfdopen_shared(1, 4096);