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_make_instance(const struct table_template *tbl_template)
25 struct table *new_inst = xmalloc_zero(sizeof(struct table)); // FIXME: update allocation to the weird schema made by pchar and mj?
27 new_inst->pool = mp_new(4096);
29 // initialize column definitions
30 int col_count = 0; // count the number of columns in the struct table
32 if(tbl_template->columns[col_count].name == NULL &&
33 tbl_template->columns[col_count].fmt == NULL &&
34 tbl_template->columns[col_count].width == 0 &&
35 tbl_template->columns[col_count].type == COL_TYPE_LAST)
37 ASSERT(tbl_template->columns[col_count].name != NULL);
38 ASSERT(tbl_template->columns[col_count].type == COL_TYPE_ANY || tbl_template->columns[col_count].fmt != NULL);
39 ASSERT(tbl_template->columns[col_count].width != 0);
43 new_inst->column_count = col_count;
45 new_inst->columns = mp_alloc_zero(new_inst->pool, sizeof(struct table_column) * new_inst->column_count);
46 memcpy(new_inst->columns, tbl_template->columns, sizeof(struct table_column) * new_inst->column_count);
48 // initialize column_order
49 if(tbl_template->column_order) {
50 new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
51 memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
52 for(uint i = 0; i < new_inst->cols_to_output; i++) {
53 new_inst->column_order[i].cell_content = NULL;
54 //new_inst->column_order[i].col_def = NULL; // FIXME: col_def should not be touched, probably ...
55 int col_idx = new_inst->column_order[i].idx;//col_order[i];
56 new_inst->column_order[i].col_def = new_inst->columns + col_idx;
57 new_inst->column_order[i].output_type = tbl_template->column_order[i].output_type;
60 new_inst->cols_to_output = tbl_template->cols_to_output;
63 new_inst->col_delimiter = tbl_template->col_delimiter;
64 new_inst->print_header = 1;
66 new_inst->last_printed_col = -1;
67 new_inst->row_printing_started = 0;
68 new_inst->col_out = -1;
69 new_inst->formatter = tbl_template->formatter;
70 new_inst->data = NULL;
74 struct table *table_init(const struct table_template *tbl_template)
76 struct table *tbl = table_make_instance(tbl_template);
79 tbl->formatter = &table_fmt_human_readable;
82 tbl->print_header = 1; // by default, print header
86 void table_cleanup(struct table *tbl)
89 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;
169 for(int i = 0; i < cols_to_output; i++) {
170 int idx = tbl->column_order[i].idx;
171 tbl->column_order[i].col_def = tbl->columns + idx;
174 for(int i = 0; i < cols_to_output; i++) {
175 int first = tbl->column_order[i].col_def->first_column;
176 tbl->column_order[i].col_def->first_column = i;
179 tbl->column_order[i].next_column = first;
181 tbl->column_order[i].next_column = -1;
186 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
188 for(int i = 0; i < cols_to_output; i++) {
189 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);
192 tbl->cols_to_output = cols_to_output;
193 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * cols_to_output);
194 for(int i = 0; i < cols_to_output; i++) {
195 int col_idx = col_order[i];
196 tbl->column_order[i].idx = col_idx;
197 tbl->column_order[i].col_def = tbl->columns + col_idx;
198 tbl->column_order[i].cell_content = NULL;
199 tbl->column_order[i].output_type = CELL_OUT_UNINITIALIZED;
201 table_update_ll(tbl);
204 bool table_col_is_printed(struct table *tbl, uint col_idx)
206 if(tbl->columns[col_idx].first_column == -1) return 0;
211 static char * table_parse_col_arg(char *col_def)
213 // FIXME: should be switched to str_sepsplit
214 char * left_br = strchr(col_def, '[');
215 if(left_br == NULL) return NULL;
218 char *right_br = strchr(left_br, ']');
224 * Setting options for basic table types (as defined in table.h)
226 bool table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char **err)
228 struct table_column *col_def = tbl->column_order[col_idx].col_def;
230 if(col_def->type == COL_TYPE_DOUBLE) {
232 const char *tmp_err = str_to_uint(&precision, col_arg, NULL, 0);
234 *err = mp_printf(tbl->pool, "An error occured while parsing precision: %s", tmp_err);
237 tbl->column_order[col_idx].output_type = precision;
241 *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_idx);
246 * TODO: This function deliberately leaks memory. When it is called multiple times,
247 * previous column orders still remain allocated in the table's memory pool.
249 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
251 if(col_order_str[0] == '*') {
252 int *col_order_int = alloca(sizeof(int) * tbl->column_count);
253 for(int i = 0; i < tbl->column_count; i++) {
254 col_order_int[i] = i;
256 table_set_col_order(tbl, col_order_int, tbl->column_count);
261 if(!col_order_str[0]) {
262 tbl->column_order = mp_alloc(tbl->pool, 0);
263 tbl->cols_to_output = 0;
267 char *tmp_col_order = stk_strdup(col_order_str);
270 for(int i = 0; col_order_str[i] != 0; i++) {
271 if(col_order_str[i] == ',') {
276 tbl->cols_to_output = col_count;
277 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * col_count);
279 int curr_col_idx = 0;
280 char *name_start = tmp_col_order;
282 char *next = strchr(name_start, ',');
287 char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
288 int col_idx = table_get_col_idx(tbl, name_start);
291 return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
293 tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
294 tbl->column_order[curr_col_idx].idx = col_idx;
295 tbl->column_order[curr_col_idx].cell_content = NULL;
296 tbl->column_order[curr_col_idx].output_type = CELL_OUT_UNINITIALIZED;
297 if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].type_def->set_col_instance_option) {
299 tbl->columns[col_idx].type_def->set_col_instance_option(tbl, curr_col_idx, arg, &err);
300 if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
307 table_update_ll(tbl);
312 /*** Table cells ***/
314 static void table_set_all_inst_content(struct table *tbl, int col_templ, char *col_content, int override)
316 TBL_COL_ITER_START(tbl, col_templ, curr_col_ptr, curr_col) {
317 if(override == 0 && curr_col_ptr->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 curr_col_ptr->cell_content = col_content;
324 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
326 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
327 tbl->last_printed_col = col;
328 tbl->row_printing_started = 1;
331 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
332 table_set_all_inst_content(tbl, col, cell_content, 1);
336 static const char *table_col_default_fmts[] = {
337 [COL_TYPE_STR] = "%s",
338 [COL_TYPE_INT] = "%d",
339 [COL_TYPE_S64] = "%lld",
340 [COL_TYPE_INTMAX] = "%jd",
341 [COL_TYPE_UINT] = "%u",
342 [COL_TYPE_U64] = "%llu",
343 [COL_TYPE_UINTMAX] = "%ju",
344 [COL_TYPE_BOOL] = "%d",
345 [COL_TYPE_DOUBLE] = "%.2lf",
346 [COL_TYPE_ANY] = NULL,
347 [COL_TYPE_LAST] = NULL
350 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
352 const char *fmt = tbl->columns[col].fmt;\
353 if(tbl->columns[col].type == COL_TYPE_ANY) {\
354 fmt = table_col_default_fmts[_typeconst_];\
356 table_col_##_name_##_fmt(tbl, col, fmt, val);\
359 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
361 int col = table_get_col_idx(tbl, col_name);\
362 table_col_##_name_(tbl, col, val);\
365 #define TABLE_COL_FMT(_name_, _type_, _typeconst_, _override) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val) \
367 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
368 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
369 ASSERT(fmt != NULL);\
370 tbl->last_printed_col = col;\
371 tbl->row_printing_started = 1;\
372 char *cell_content = mp_printf(tbl->pool, fmt, val);\
373 table_set_all_inst_content(tbl, col, cell_content, _override);\
376 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_, _override) TABLE_COL(_name_, _type_, _typeconst_); \
377 TABLE_COL_STR(_name_, _type_, _typeconst_);\
378 TABLE_COL_FMT(_name_, _type_, _typeconst_, _override);
380 TABLE_COL_BODIES(int, int, COL_TYPE_INT, 0)
381 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT, 0)
382 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR, 1)
383 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX, 0)
384 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX, 0)
385 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64, 0)
386 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64, 0)
388 // column type double is a special case
389 TABLE_COL(double, double, COL_TYPE_DOUBLE);
390 TABLE_COL_STR(double, double, COL_TYPE_DOUBLE);
392 TABLE_COL(bool, bool, COL_TYPE_BOOL);
393 TABLE_COL_STR(bool, bool, COL_TYPE_BOOL);
398 #undef TABLE_COL_BODIES
400 void table_col_double_fmt(struct table *tbl, int col, const char *fmt, double val)
402 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
403 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_DOUBLE == tbl->columns[col].type);
405 tbl->last_printed_col = col;
406 tbl->row_printing_started = 1;
407 char *cell_content = mp_printf(tbl->pool, fmt, val);
408 int curr_col = tbl->columns[col].first_column;
409 while(curr_col != -1) {
410 char *cell_content_tmp = NULL;
411 switch(tbl->column_order[curr_col].output_type) {
412 case CELL_OUT_UNINITIALIZED:
413 cell_content_tmp = cell_content;
415 case CELL_OUT_MACHINE_READABLE:
416 cell_content_tmp = mp_printf(tbl->pool, "%4lf", val);
419 cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val);
422 tbl->column_order[curr_col].cell_content = cell_content_tmp;
423 curr_col = tbl->column_order[curr_col].next_column;
427 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, bool 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_inst_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, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
505 uint tmp = value[0] - '0';
507 return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
508 tbl->print_header = tmp;
510 } else if(strcmp(key, "cols") == 0) {
511 return table_set_col_order_by_name(tbl, value);
512 } else if(strcmp(key, "fmt") == 0) {
513 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
514 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
515 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
517 return "Invalid argument to output-type option.";
520 } else if(strcmp(key, "col-delim") == 0) {
521 char * d = mp_printf(tbl->pool, "%s", value);
522 tbl->col_delimiter = d;
528 if(tbl->formatter && tbl->formatter->process_option) {
529 const char *err = NULL;
530 if(tbl->formatter->process_option(tbl, key, value, &err)) {
535 // Unrecognized option
536 return mp_printf(tbl->pool, "Invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
539 const char *table_set_option(struct table *tbl, const char *opt)
541 char *key = stk_strdup(opt);
542 char *value = strchr(key, ':');
546 return table_set_option_value(tbl, key, value);
549 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
551 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
552 const char *rv = table_set_option(tbl, gary_table_opts[i]);
560 /*** Default formatter for human-readable output ***/
562 static void table_row_human_readable(struct table *tbl)
564 for(uint i = 0; i < tbl->cols_to_output; i++) {
565 struct table_column *col_def = tbl->column_order[i].col_def;
567 bputs(tbl->out, tbl->col_delimiter);
569 int col_width = col_def->width & CELL_WIDTH_MASK;
570 if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
571 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
573 bputc(tbl->out, '\n');
576 static void table_write_header(struct table *tbl)
578 for(uint i = 0; i < tbl->cols_to_output; i++) {
579 struct table_column *col_def = tbl->column_order[i].col_def;
581 bputs(tbl->out, tbl->col_delimiter);
583 int col_width = col_def->width & CELL_WIDTH_MASK;
584 if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
585 bprintf(tbl->out, "%*s", col_width, col_def->name);
587 bputc(tbl->out, '\n');
590 static void table_start_human_readable(struct table *tbl)
592 if(tbl->col_delimiter == NULL) {
593 tbl->col_delimiter = " ";
596 if(tbl->print_header != 0) {
597 table_write_header(tbl);
601 struct table_formatter table_fmt_human_readable = {
602 .row_output = table_row_human_readable,
603 .table_start = table_start_human_readable,
606 /*** Default formatter for machine-readable output ***/
608 static void table_row_machine_readable(struct table *tbl)
610 for(uint i = 0; i < tbl->cols_to_output; i++) {
612 bputs(tbl->out, tbl->col_delimiter);
614 bputs(tbl->out, tbl->column_order[i].cell_content);
616 bputc(tbl->out, '\n');
619 static void table_start_machine_readable(struct table *tbl)
621 if(tbl->col_delimiter == NULL) {
622 tbl->col_delimiter = "\t";
625 if(tbl->print_header != 0 && tbl->cols_to_output > 0) {
626 bputs(tbl->out, tbl->column_order[0].col_def->name);
627 for(uint i = 1; i < tbl->cols_to_output; i++) {
628 bputs(tbl->out, tbl->col_delimiter);
629 bputs(tbl->out, tbl->column_order[i].col_def->name);
631 bputc(tbl->out, '\n');
635 struct table_formatter table_fmt_machine_readable = {
636 .row_output = table_row_machine_readable,
637 .table_start = table_start_machine_readable,
641 /*** Blockline formatter ***/
643 static void table_row_blockline_output(struct table *tbl)
645 for(uint i = 0; i < tbl->cols_to_output; i++) {
646 struct table_column *col_def = tbl->column_order[i].col_def;
647 bprintf(tbl->out, "%s: %s\n", col_def->name, tbl->column_order[i].cell_content);
649 bputc(tbl->out, '\n');
652 static void table_start_blockline(struct table *tbl)
654 if(tbl->col_delimiter == NULL) {
655 tbl->col_delimiter = "\n";
659 struct table_formatter table_fmt_blockline = {
660 .row_output = table_row_blockline_output,
661 .table_start = table_start_blockline
670 enum test_table_cols {
671 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
674 static struct table_col_instance 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) };
676 static struct table_template test_tbl = {
678 [test_col0_str] = TBL_COL_STR("col0_str", 20),
679 [test_col1_int] = TBL_COL_INT("col1_int", 8),
680 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
681 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
682 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
685 TBL_COL_ORDER(test_column_order),
686 TBL_OUTPUT_HUMAN_READABLE,
687 TBL_COL_DELIMITER("\t"),
691 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
693 static void do_print1(struct table *test_tbl)
695 table_col_str(test_tbl, test_col0_str, "sdsdf");
696 table_col_int(test_tbl, test_col1_int, -10);
697 table_col_int(test_tbl, test_col1_int, 10000);
698 table_col_uint(test_tbl, test_col2_uint, 10);
699 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
700 table_col_bool(test_tbl, test_col3_bool, 1);
701 table_col_double(test_tbl, test_col4_double, 1.5);
702 table_col_printf(test_tbl, test_col4_double, "AAA");
703 table_end_row(test_tbl);
705 table_col_str(test_tbl, test_col0_str, "test");
706 table_col_int(test_tbl, test_col1_int, -100);
707 table_col_uint(test_tbl, test_col2_uint, 100);
708 table_col_bool(test_tbl, test_col3_bool, 0);
709 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
710 table_end_row(test_tbl);
713 static void test_simple1(struct fastbuf *out)
715 struct table *tbl = table_init(&test_tbl);
717 // print table with header
718 table_set_col_order_by_name(tbl, "col3_bool");
719 table_start(tbl, out);
723 // print the same table as in the previous case without header
724 table_set_col_order_by_name(tbl, "col0_str,col2_uint,col1_int,col3_bool");
725 table_start(tbl, out);
729 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
730 tbl->print_header = 0;
731 table_start(tbl, out);
734 tbl->print_header = 1;
736 table_set_col_order_by_name(tbl, "col3_bool");
737 table_start(tbl, out);
741 table_set_col_order_by_name(tbl, "col3_bool,col0_str");
742 table_start(tbl, out);
746 table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint");
747 table_start(tbl, out);
751 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");
752 table_start(tbl, out);
756 table_set_col_order_by_name(tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
757 table_start(tbl, out);
764 enum test_any_table_cols {
765 test_any_col0_int, test_any_col1_any
768 static struct table_col_instance test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
770 static struct table_template test_any_tbl = {
772 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
773 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
776 TBL_COL_ORDER(test_any_column_order),
777 TBL_OUTPUT_HUMAN_READABLE,
778 TBL_COL_DELIMITER("\t"),
781 static void test_any_type(struct fastbuf *out)
783 struct table *tbl = table_init(&test_any_tbl);
785 table_start(tbl, out);
787 table_col_int(tbl, test_any_col0_int, -10);
788 table_col_int(tbl, test_any_col1_any, 10000);
791 table_col_int(tbl, test_any_col0_int, -10);
792 table_col_double(tbl, test_any_col1_any, 1.4);
795 table_col_printf(tbl, test_any_col0_int, "%d", 10);
796 table_col_double(tbl, test_any_col1_any, 1.4);
803 int main(int argc UNUSED, char **argv UNUSED)
806 out = bfdopen_shared(1, 4096);