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 void table_template_init(struct table *tbl_template)
25 if(!tbl_template->pool) {
26 tbl_template->pool = mp_new(4096);
30 static struct table *table_template_copy(struct table *tbl_template)
32 struct table *copy = mp_alloc_zero(tbl_template->pool, sizeof(struct table));
34 copy->column_count = tbl_template->column_count;
35 copy->pool = mp_new(4096);
36 if(tbl_template->column_order) {
37 copy->column_order = mp_alloc_zero(copy->pool, sizeof(struct table_col_info) * tbl_template->cols_to_output); //tbl_template->; // FIXME: more complicated copying
38 memcpy(copy->column_order, tbl_template->column_order, sizeof(struct table_col_info) * tbl_template->cols_to_output);
39 for(uint i = 0; i < copy->cols_to_output; i++) {
40 copy->column_order[i].cell_content = NULL;
41 copy->column_order[i].col_def = NULL;
42 copy->column_order[i].output_type = tbl_template->column_order[i].output_type;
45 copy->cols_to_output = tbl_template->cols_to_output;
48 copy->columns = tbl_template->columns;
50 copy->col_delimiter = tbl_template->col_delimiter;
51 copy->print_header = tbl_template->print_header;
53 copy->last_printed_col = -1;
54 copy->row_printing_started = 0;
56 copy->formatter = tbl_template->formatter;
61 struct table *table_init(struct table *tbl_template)
63 int col_count = 0; // count the number of columns in the struct table
65 table_template_init(tbl_template);
67 struct table *tbl = table_template_copy(tbl_template);
70 if(tbl->columns[col_count].name == NULL &&
71 tbl->columns[col_count].fmt == NULL &&
72 tbl->columns[col_count].width == 0 &&
73 tbl->columns[col_count].type == COL_TYPE_LAST)
75 ASSERT(tbl->columns[col_count].name != NULL);
76 ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl_template->columns[col_count].fmt != NULL);
77 ASSERT(tbl->columns[col_count].width != 0);
82 tbl->column_count = col_count;
85 tbl->formatter = &table_fmt_human_readable;
88 tbl->print_header = 1; // by default, print header
92 void table_cleanup(struct table *tbl)
95 memset(tbl, 0, sizeof(struct table));
98 // TODO: test default column order
99 static void table_make_default_column_order(struct table *tbl)
101 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count); // FIXME: use stack instead of memory pool
102 for(int i = 0; i < tbl->column_count; i++) {
103 col_order_int[i] = i;
105 table_set_col_order(tbl, col_order_int, tbl->column_count);
108 void table_start(struct table *tbl, struct fastbuf *out)
110 tbl->last_printed_col = -1;
111 tbl->row_printing_started = 0;
114 ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
116 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
118 // update linked lists
119 table_update_ll(tbl);
121 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
123 mp_save(tbl->pool, &tbl->pool_state);
125 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
128 void table_end(struct table *tbl)
130 tbl->last_printed_col = -1;
131 tbl->row_printing_started = 0;
133 mp_restore(tbl->pool, &tbl->pool_state);
135 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
138 /*** Configuration ***/
140 void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
142 tbl->formatter = fmt;
145 int table_get_col_idx(struct table *tbl, const char *col_name)
147 for(int i = 0; i < tbl->column_count; i++) {
148 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
153 const char * table_get_col_list(struct table *tbl)
155 if(tbl->column_count == 0) return "";
157 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
159 for(int i = 1; i < tbl->column_count; i++) {
160 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
166 static void table_update_ll(struct table *tbl)
168 int cols_to_output = tbl->cols_to_output;
170 for(int i = 0; i < tbl->column_count; i++) {
171 tbl->columns[i].first_column = -1;
172 tbl->columns[i].last_column = -1;
175 for(int i = 0; i < cols_to_output; i++) {
176 int idx = tbl->column_order[i].idx;
177 tbl->column_order[i].col_def = tbl->columns + idx;
180 for(int i = 0; i < cols_to_output; i++) {
181 int last = tbl->column_order[i].col_def->last_column;
183 tbl->column_order[i].col_def->last_column = i;
184 tbl->column_order[last].next_column = i;
186 tbl->column_order[i].col_def->last_column = i;
187 tbl->column_order[i].col_def->first_column = i;
189 tbl->column_order[i].next_column = -1;
193 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
195 for(int i = 0; i < cols_to_output; i++) {
196 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);
199 tbl->cols_to_output = cols_to_output;
200 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * cols_to_output);
201 for(int i = 0; i < cols_to_output; i++) {
202 int col_idx = col_order[i];
203 tbl->column_order[i].idx = col_idx;
204 tbl->column_order[i].col_def = tbl->columns + col_idx;
205 tbl->column_order[i].cell_content = NULL;
206 tbl->column_order[i].output_type = CELL_OUT_UNINITIALIZED;
208 table_update_ll(tbl);
211 bool table_col_is_printed(struct table *tbl, uint col_idx)
213 if(tbl->columns[col_idx].first_column == -1) return 0;
218 static char * table_parse_col_arg(char *col_def)
220 // FIXME: should be switched to str_sepsplit
221 char * left_br = strchr(col_def, '[');
222 if(left_br == NULL) return NULL;
225 char *right_br = strchr(left_br, ']');
231 * Setting options for basic table types (as defined in table.h)
233 bool table_set_col_opt_default(struct table *tbl, int col_copy_idx, const char *col_arg, char **err)
235 struct table_column *tmp_col = tbl->column_order[col_copy_idx].col_def;
236 int col_type_idx = tbl->column_order[col_copy_idx].idx;
238 if(tmp_col->type == COL_TYPE_DOUBLE) {
240 str_to_uint(&precision, col_arg, NULL, 0);
241 tbl->column_order[col_type_idx].output_type = precision;
245 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
250 * TODO: This function deliberately leaks memory. When it is called multiple times,
251 * previous column orders still remain allocated in the table's memory pool.
253 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
255 if(col_order_str[0] == '*') {
256 int *col_order_int = alloca(sizeof(int) * tbl->column_count);
257 for(int i = 0; i < tbl->column_count; i++) {
258 col_order_int[i] = i;
260 table_set_col_order(tbl, col_order_int, tbl->column_count);
265 if(!col_order_str[0]) {
266 tbl->column_order = mp_alloc(tbl->pool, 0);
267 tbl->cols_to_output = 0;
271 char *tmp_col_order = stk_strdup(col_order_str);
274 for(int i = 0; col_order_str[i] != 0; i++) {
275 if(col_order_str[i] == ',') {
280 tbl->cols_to_output = col_count;
281 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * col_count);
283 int curr_col_idx = 0;
284 char *name_start = tmp_col_order;
286 char *next = strchr(name_start, ',');
291 char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
292 int col_idx = table_get_col_idx(tbl, name_start);
295 return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
297 tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
298 tbl->column_order[curr_col_idx].idx = col_idx;
299 tbl->column_order[curr_col_idx].cell_content = NULL;
300 tbl->column_order[curr_col_idx].output_type = CELL_OUT_UNINITIALIZED;
301 if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].type_def->set_col_instance_option) {
303 tbl->columns[col_idx].type_def->set_col_instance_option(tbl, curr_col_idx, arg, &err);
304 if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
311 table_update_ll(tbl);
316 /*** Table cells ***/
318 static void table_set_all_cols_content(struct table *tbl, int col, char *col_content, int override)
320 int curr_col = tbl->columns[col].first_column;
321 while(curr_col != -1) {
322 if(override == 0 && tbl->column_order[curr_col].output_type != CELL_OUT_UNINITIALIZED ) {
323 die("Error while setting content of all cells of a single type column, cell format should not be overriden.");
325 tbl->column_order[curr_col].cell_content = col_content;
326 curr_col = tbl->column_order[curr_col].next_column;
330 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
332 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
333 tbl->last_printed_col = col;
334 tbl->row_printing_started = 1;
337 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
338 table_set_all_cols_content(tbl, col, cell_content, 1);
342 static const char *table_col_default_fmts[] = {
343 [COL_TYPE_STR] = "%s",
344 [COL_TYPE_INT] = "%d",
345 [COL_TYPE_S64] = "%lld",
346 [COL_TYPE_INTMAX] = "%jd",
347 [COL_TYPE_UINT] = "%u",
348 [COL_TYPE_U64] = "%llu",
349 [COL_TYPE_UINTMAX] = "%ju",
350 [COL_TYPE_BOOL] = "%d",
351 [COL_TYPE_DOUBLE] = "%.2lf",
352 [COL_TYPE_ANY] = NULL,
353 [COL_TYPE_LAST] = NULL
356 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
358 const char *fmt = tbl->columns[col].fmt;\
359 if(tbl->columns[col].type == COL_TYPE_ANY) {\
360 fmt = table_col_default_fmts[_typeconst_];\
362 table_col_##_name_##_fmt(tbl, col, fmt, val);\
365 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
367 int col = table_get_col_idx(tbl, col_name);\
368 table_col_##_name_(tbl, col, val);\
371 #define TABLE_COL_FMT(_name_, _type_, _typeconst_, _override) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val) \
373 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
374 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
375 ASSERT(fmt != NULL);\
376 tbl->last_printed_col = col;\
377 tbl->row_printing_started = 1;\
378 char *cell_content = mp_printf(tbl->pool, fmt, val);\
379 table_set_all_cols_content(tbl, col, cell_content, _override);\
382 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_, _override) TABLE_COL(_name_, _type_, _typeconst_); \
383 TABLE_COL_STR(_name_, _type_, _typeconst_);\
384 TABLE_COL_FMT(_name_, _type_, _typeconst_, _override);
386 TABLE_COL_BODIES(int, int, COL_TYPE_INT, 0)
387 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT, 0)
388 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR, 1)
389 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX, 0)
390 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX, 0)
391 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64, 0)
392 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64, 0)
394 // column type double is a special case
395 TABLE_COL(double, double, COL_TYPE_DOUBLE);
396 TABLE_COL_STR(double, double, COL_TYPE_DOUBLE);
400 #undef TABLE_COL_BODIES
402 void table_col_double_fmt(struct table *tbl, int col, const char *fmt, double val)
404 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
405 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_DOUBLE == tbl->columns[col].type);
407 tbl->last_printed_col = col;
408 tbl->row_printing_started = 1;
409 char *cell_content = mp_printf(tbl->pool, fmt, val);
410 int curr_col = tbl->columns[col].first_column;
411 while(curr_col != -1) {
412 if(tbl->column_order[curr_col].output_type < 0) tbl->column_order[curr_col].cell_content = cell_content;
414 char *cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val);
415 tbl->column_order[curr_col].cell_content = cell_content_tmp;
417 curr_col = tbl->column_order[curr_col].next_column;
421 void table_col_bool(struct table *tbl, int col, uint val)
423 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
426 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
428 int col = table_get_col_idx(tbl, col_name);
429 table_col_bool(tbl, col, val);
432 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
434 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
435 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
437 tbl->last_printed_col = col;
438 tbl->row_printing_started = 1;
440 int curr_col = tbl->columns[col].first_column;
441 while(curr_col != -1) {
442 switch(tbl->column_order[curr_col].output_type) {
443 case CELL_OUT_HUMAN_READABLE:
444 case CELL_OUT_UNINITIALIZED:
445 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "true" : "false");
447 case CELL_OUT_MACHINE_READABLE:
448 // FIXME: this is just an example of printing in different formats
449 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "1" : "0");
452 die("Unsupported output type.");
454 curr_col = tbl->column_order[curr_col].next_column;
458 void table_reset_row(struct table *tbl)
460 for(uint i = 0; i < tbl->cols_to_output; i++) {
461 tbl->column_order[i].cell_content = NULL;
463 mp_restore(tbl->pool, &tbl->pool_state);
464 tbl->last_printed_col = -1;
465 tbl->row_printing_started = 0;
468 void table_end_row(struct table *tbl)
470 ASSERT(tbl->formatter->row_output);
471 if(tbl->row_printing_started == 0) return;
472 tbl->formatter->row_output(tbl);
473 table_reset_row(tbl);
476 /* Construction of a cell using a fastbuf */
478 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
480 fbpool_init(&tbl->fb_col_out);
481 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
483 return &tbl->fb_col_out.fb;
486 void table_col_fbend(struct table *tbl)
488 char *cell_content = fbpool_end(&tbl->fb_col_out);
489 table_set_all_cols_content(tbl, tbl->col_out, cell_content, 1);
493 /*** Option parsing ***/
495 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
497 // Options with no value
498 if(value == NULL || (value != NULL && strlen(value) == 0)) {
499 if(strcmp(key, "noheader") == 0) {
500 tbl->print_header = 0;
505 // Options with a value
507 if(strcmp(key, "header") == 0) {
509 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
510 uint tmp = value[0] - '0';
512 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
513 tbl->print_header = tmp;
515 } else if(strcmp(key, "cols") == 0) {
516 const char *err = table_set_col_order_by_name(tbl, value);
518 } else if(strcmp(key, "fmt") == 0) {
519 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
520 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
521 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
523 return "Tableprinter: invalid argument to output-type option.";
526 } else if(strcmp(key, "col-delim") == 0) {
527 char * d = mp_printf(tbl->pool, "%s", value);
528 tbl->col_delimiter = d;
534 if(tbl->formatter && tbl->formatter->process_option) {
535 const char *err = NULL;
536 if(tbl->formatter->process_option(tbl, key, value, &err)) {
541 // Unrecognized option
542 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
545 const char *table_set_option(struct table *tbl, const char *opt)
547 char *key = stk_strdup(opt);
548 char *value = strchr(key, ':');
552 return table_set_option_value(tbl, key, value);
555 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
557 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
558 const char *rv = table_set_option(tbl, gary_table_opts[i]);
566 /*** Default formatter for human-readable output ***/
568 static void table_row_human_readable(struct table *tbl)
570 for(uint i = 0; i < tbl->cols_to_output; i++) {
571 int col_idx = tbl->column_order[i].idx;
573 bputs(tbl->out, tbl->col_delimiter);
575 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
576 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
577 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
579 bputc(tbl->out, '\n');
582 static void table_write_header(struct table *tbl)
584 for(uint i = 0; i < tbl->cols_to_output; i++) {
585 int col_idx = tbl->column_order[i].col_def - tbl->columns;
587 bputs(tbl->out, tbl->col_delimiter);
589 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
590 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
591 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
593 bputc(tbl->out, '\n');
596 static void table_start_human_readable(struct table *tbl)
598 if(tbl->col_delimiter == NULL) {
599 tbl->col_delimiter = " ";
602 if(tbl->print_header != 0) {
603 table_write_header(tbl);
607 struct table_formatter table_fmt_human_readable = {
608 .row_output = table_row_human_readable,
609 .table_start = table_start_human_readable,
612 /*** Default formatter for machine-readable output ***/
614 static void table_row_machine_readable(struct table *tbl)
616 for(uint i = 0; i < tbl->cols_to_output; i++) {
618 bputs(tbl->out, tbl->col_delimiter);
620 bputs(tbl->out, tbl->column_order[i].cell_content);
622 bputc(tbl->out, '\n');
625 static void table_start_machine_readable(struct table *tbl)
627 if(tbl->col_delimiter == NULL) {
628 tbl->col_delimiter = "\t";
631 if(tbl->print_header != 0) {
632 uint col_idx = tbl->column_order[0].col_def - tbl->columns;
633 bputs(tbl->out, tbl->columns[col_idx].name);
634 for(uint i = 1; i < tbl->cols_to_output; i++) {
635 col_idx = tbl->column_order[i].col_def - tbl->columns;
636 bputs(tbl->out, tbl->col_delimiter);
637 bputs(tbl->out, tbl->columns[col_idx].name);
639 bputc(tbl->out, '\n');
643 struct table_formatter table_fmt_machine_readable = {
644 .row_output = table_row_machine_readable,
645 .table_start = table_start_machine_readable,
649 /*** Blockline formatter ***/
651 static void table_row_blockline_output(struct table *tbl)
653 for(uint i = 0; i < tbl->cols_to_output; i++) {
654 int col_idx = tbl->column_order[i].idx;
655 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->column_order[i].cell_content);
657 bputc(tbl->out, '\n');
660 static void table_start_blockline(struct table *tbl)
662 if(tbl->col_delimiter == NULL) {
663 tbl->col_delimiter = "\n";
667 struct table_formatter table_fmt_blockline = {
668 .row_output = table_row_blockline_output,
669 .table_start = table_start_blockline
680 enum test_table_cols {
681 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
684 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) };
686 static struct table test_tbl = {
688 [test_col0_str] = TBL_COL_STR("col0_str", 20),
689 [test_col1_int] = TBL_COL_INT("col1_int", 8),
690 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
691 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
692 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
695 TBL_COL_ORDER(test_column_order),
696 TBL_OUTPUT_HUMAN_READABLE,
697 TBL_COL_DELIMITER("\t"),
701 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
703 static void do_print1(struct table *test_tbl)
705 table_col_str(test_tbl, test_col0_str, "sdsdf");
706 table_col_int(test_tbl, test_col1_int, -10);
707 table_col_int(test_tbl, test_col1_int, 10000);
708 table_col_uint(test_tbl, test_col2_uint, 10);
709 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
710 table_col_bool(test_tbl, test_col3_bool, 1);
711 table_col_double(test_tbl, test_col4_double, 1.5);
712 table_col_printf(test_tbl, test_col4_double, "AAA");
713 table_end_row(test_tbl);
715 table_col_str(test_tbl, test_col0_str, "test");
716 table_col_int(test_tbl, test_col1_int, -100);
717 table_col_uint(test_tbl, test_col2_uint, 100);
718 table_col_bool(test_tbl, test_col3_bool, 0);
719 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
720 table_end_row(test_tbl);
723 static void test_simple1(struct fastbuf *out)
725 struct table *tbl = table_init(&test_tbl);
727 // print table with header
728 table_set_col_order_by_name(tbl, "col3_bool");
729 table_start(tbl, out);
733 // print the same table as in the previous case without header
734 table_set_col_order_by_name(tbl, "col0_str,col2_uint,col1_int,col3_bool");
735 table_start(tbl, out);
739 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
740 tbl->print_header = 0;
741 table_start(tbl, out);
744 tbl->print_header = 1;
746 table_set_col_order_by_name(tbl, "col3_bool");
747 table_start(tbl, out);
751 table_set_col_order_by_name(tbl, "col3_bool,col0_str");
752 table_start(tbl, out);
756 table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint");
757 table_start(tbl, out);
761 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");
762 table_start(tbl, out);
766 table_set_col_order_by_name(tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
767 table_start(tbl, out);
774 enum test_any_table_cols {
775 test_any_col0_int, test_any_col1_any
778 static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
780 static struct table test_any_tbl = {
782 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
783 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
786 TBL_COL_ORDER(test_any_column_order),
787 TBL_OUTPUT_HUMAN_READABLE,
788 TBL_COL_DELIMITER("\t"),
791 static void test_any_type(struct fastbuf *out)
793 struct table *tbl = table_init(&test_any_tbl);
795 table_start(tbl, out);
797 table_col_int(tbl, test_any_col0_int, -10);
798 table_col_int(tbl, test_any_col1_any, 10000);
801 table_col_int(tbl, test_any_col0_int, -10);
802 table_col_double(tbl, test_any_col1_any, 1.4);
805 table_col_printf(tbl, test_any_col0_int, "%d", 10);
806 table_col_double(tbl, test_any_col1_any, 1.4);
813 int main(int argc UNUSED, char **argv UNUSED)
816 out = bfdopen_shared(1, 4096);