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 struct table *table_init(const struct table_template *tbl_template)
25 struct mempool *pool = mp_new(4096);
26 struct table *new_inst = mp_alloc_zero(pool, sizeof(struct table));
28 new_inst->pool = pool;
30 // initialize column definitions
31 uint col_count = 0; // count the number of columns in the struct table
33 if(tbl_template->columns[col_count].name == NULL &&
34 tbl_template->columns[col_count].width == 0 &&
35 tbl_template->columns[col_count].type_def == COL_TYPE_ANY)
37 ASSERT(tbl_template->columns[col_count].name != NULL);
38 ASSERT(tbl_template->columns[col_count].width != 0);
42 new_inst->column_count = col_count;
44 new_inst->columns = tbl_template->columns;
45 new_inst->ll_headers = mp_alloc(new_inst->pool, sizeof(int) * col_count);
46 for(uint i = 0; i < col_count; i++) {
47 new_inst->ll_headers[i] = -1;
50 // initialize column_order
51 if(tbl_template->column_order) {
52 int cols_to_output = 0;
53 for(; ; cols_to_output++) {
54 if(tbl_template->column_order[cols_to_output].idx == (uint) ~0) break;
57 new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_instance) * cols_to_output);
58 memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_instance) * cols_to_output);
59 for(uint i = 0; i < new_inst->cols_to_output; i++) {
60 new_inst->column_order[i].cell_content = NULL;
61 int col_def_idx = new_inst->column_order[i].idx;
62 new_inst->column_order[i].col_def = new_inst->columns + col_def_idx;
63 new_inst->column_order[i].fmt = tbl_template->columns[col_def_idx].fmt;
66 new_inst->cols_to_output = cols_to_output;
69 new_inst->col_delimiter = tbl_template->col_delimiter;
70 new_inst->print_header = true;
72 new_inst->row_printing_started = false;
73 new_inst->col_out = -1;
74 new_inst->formatter = tbl_template->formatter;
75 if(!new_inst->formatter) {
76 new_inst->formatter = &table_fmt_human_readable;
78 new_inst->formatter_data = NULL;
82 void table_cleanup(struct table *tbl)
87 // TODO: test default column order
88 static void table_make_default_column_order(struct table *tbl)
90 struct table_col_instance *col_order = alloca(sizeof(struct table_col_instance) * (tbl->column_count + 1));
91 bzero(col_order, sizeof(struct table_col_instance) * tbl->column_count);
93 for(int i = 0; i < tbl->column_count; i++) {
94 col_order[i].idx = (uint) i;
95 // currently, XTYPE_FMT_DEFAULT is 0, so bzero actually sets it correctly. This makes it more explicit.
96 col_order[i].fmt = XTYPE_FMT_DEFAULT;
98 struct table_col_instance tbl_col_order_end = TBL_COL_ORDER_END;
99 col_order[tbl->column_count] = tbl_col_order_end;
101 table_set_col_order(tbl, col_order);
104 void table_start(struct table *tbl, struct fastbuf *out)
106 tbl->row_printing_started = false;
109 ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
111 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
112 // update linked lists
113 table_update_ll(tbl);
114 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
116 mp_save(tbl->pool, &tbl->pool_state);
118 ASSERT_MSG(tbl->col_delimiter, "Column delimiter not specified.");
121 void table_end(struct table *tbl)
123 tbl->row_printing_started = false;
125 mp_restore(tbl->pool, &tbl->pool_state);
127 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
130 /*** Configuration ***/
132 void table_set_formatter(struct table *tbl, const struct table_formatter *fmt)
134 tbl->formatter = fmt;
137 int table_get_col_idx(struct table *tbl, const char *col_name)
139 for(int i = 0; i < tbl->column_count; i++) {
140 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
145 const char * table_get_col_list(struct table *tbl)
147 if(tbl->column_count == 0) return "";
149 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
151 for(int i = 1; i < tbl->column_count; i++) {
152 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
158 static void table_update_ll(struct table *tbl)
160 int cols_to_output = tbl->cols_to_output;
162 for(int i = 0; i < tbl->column_count; i++) {
163 tbl->ll_headers[i] = -1;
166 for(int i = 0; i < cols_to_output; i++) {
167 int col_def_idx = tbl->column_order[i].idx;
168 tbl->column_order[i].col_def = tbl->columns + col_def_idx;
171 for(int i = 0; i < cols_to_output; i++) {
172 int col_def_idx = tbl->column_order[i].idx;
173 int first = tbl->ll_headers[col_def_idx];
174 tbl->ll_headers[col_def_idx] = i;
175 tbl->column_order[i].next_column = first;
179 void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order)
181 uint cols_to_output = 0;
182 for(; ; cols_to_output++) {
183 if(col_order[cols_to_output].idx == (uint) ~0) break;
184 ASSERT_MSG(col_order[cols_to_output].idx < (uint) tbl->column_count,
185 "Column %d does not exist; column number should be between 0 and %d(including).", col_order[cols_to_output].idx, tbl->column_count - 1);
188 tbl->cols_to_output = cols_to_output;
189 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * cols_to_output);
190 memcpy(tbl->column_order, col_order, sizeof(struct table_col_instance) * cols_to_output);
191 for(uint i = 0; i < cols_to_output; i++) {
192 int col_def_idx = tbl->column_order[i].idx; // this is given in col_order
193 tbl->column_order[i].col_def = tbl->columns + col_def_idx;
194 tbl->column_order[i].cell_content = NULL; // cell_content is copied from @col_order, so make sure that it is NULL
195 tbl->column_order[i].next_column = -1;
196 // tbl->column_order[i].fmt should be untouched (copied from col_order)
200 bool table_col_is_printed(struct table *tbl, uint col_def_idx)
202 if(tbl->ll_headers[col_def_idx] == -1) return false;
207 const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *col_opt)
209 const struct table_column *col_def = tbl->column_order[col_inst_idx].col_def;
211 // Make sure that we do not call table_set_col_opt, which would
212 // result in an infinite recursion.
213 if(col_def && col_def->set_col_opt) {
214 ASSERT_MSG(col_def->set_col_opt != table_set_col_opt,"table_set_col_opt should not be used as a struct table_column::set_col_opt hook");
215 return col_def->set_col_opt(tbl, col_inst_idx, col_opt);
218 if(col_def && col_def->type_def && col_def->type_def->parse_fmt) {
220 const char *tmp_err = col_def->type_def->parse_fmt(col_opt, &fmt, tbl->pool);
221 if(tmp_err) return tmp_err;
222 tbl->column_order[col_inst_idx].fmt = fmt;
226 return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_inst_idx);
229 static char **table_parse_col_arg2(char *col_def)
231 char * left_br = strchr(col_def, '[');
232 if(left_br == NULL) return NULL;
237 char *col_opt = left_br;
240 char **result = NULL;
241 GARY_INIT(result, 0);
243 next = strchr(col_opt, ',');
245 if(*next == 0) break;
248 *GARY_PUSH(result) = col_opt;
252 *GARY_PUSH(result) = col_opt;
258 * TODO: This function deliberately leaks memory. When it is called multiple times,
259 * previous column orders still remain allocated in the table's memory pool.
261 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
263 if(col_order_str[0] == '*') {
264 table_make_default_column_order(tbl);
268 if(!col_order_str[0]) {
269 tbl->column_order = mp_alloc(tbl->pool, 0);
270 tbl->cols_to_output = 0;
274 char *tmp_col_order = stk_strdup(col_order_str);
277 bool inside_brackets = false;
278 for(int i = 0; col_order_str[i] != 0; i++) {
279 if(col_order_str[i] == '[') inside_brackets = true;
280 if(col_order_str[i] == ']') inside_brackets = false;
281 if(!inside_brackets && col_order_str[i] == ',') {
286 tbl->cols_to_output = col_count;
287 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * col_count);
289 int curr_col_inst_idx = 0;
290 char *name_start = tmp_col_order;
292 char *next = strpbrk(name_start, "[,");
293 if(next && *next == '[') {
294 next = strchr(next, ']');
296 next = *next == 0 ? NULL : next + 1;
301 char **args = table_parse_col_arg2(name_start); // this sets 0 on the '['
302 int col_def_idx = table_get_col_idx(tbl, name_start);
304 if(col_def_idx == -1) {
305 return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
307 tbl->column_order[curr_col_inst_idx].col_def = tbl->columns + col_def_idx;
308 tbl->column_order[curr_col_inst_idx].idx = col_def_idx;
309 tbl->column_order[curr_col_inst_idx].fmt = tbl->columns[col_def_idx].fmt;
311 for(uint i = 0; i < GARY_SIZE(args); i++) {
312 const char *err = NULL;
313 err = table_set_col_opt(tbl, curr_col_inst_idx, args[i]);
314 if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
326 /*** Table cells ***/
329 * The TBL_COL_ITER_START macro are used for iterating over all instances of a particular column in
330 * table _tbl. _colidx is the column index in _tbl, _instptr is the pointer to the column instance
331 * (struct table_col_instance *), _idxval is the index of current column index. The variables are
332 * enclosed in a block, so they do not introduce variable name collisions.
334 * The TBL_COL_ITER_END macro must close the block started with TBL_COL_ITER_START.
336 * These macros are usually used to hide the implementation details of the column instances linked
337 * list. This is usefull for definition of new types.
339 #define TBL_COL_ITER_START(_tbl, _colidx, _instptr, _idxval) { struct table_col_instance *_instptr = NULL; int _idxval = _tbl->ll_headers[_colidx]; \
340 for(_idxval = _tbl->ll_headers[_colidx], _instptr = _tbl->column_order + _idxval; _idxval != -1; _idxval = _tbl->column_order[_idxval].next_column, _instptr = _tbl->column_order + _idxval)
342 #define TBL_COL_ITER_END }
344 static void table_col_raw(struct table *tbl, int col_templ, const char *col_content)
346 TBL_COL_ITER_START(tbl, col_templ, curr_col_ptr, curr_col) {
347 curr_col_ptr->cell_content = col_content;
351 void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type)
353 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
354 ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == tbl->columns[col].type_def);
355 tbl->row_printing_started = true;
356 TBL_COL_ITER_START(tbl, col, curr_col, curr_col_inst_idx) {
357 enum xtype_fmt fmt = curr_col->fmt;
358 curr_col->cell_content = expected_type->format(value, fmt, tbl->pool);
362 #undef TBL_COL_ITER_START
363 #undef TBL_COL_ITER_END
365 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
367 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
368 tbl->row_printing_started = true;
371 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
372 table_col_raw(tbl, col, cell_content);
376 TABLE_COL_BODY(int, int)
377 TABLE_COL_BODY(uint, uint)
378 TABLE_COL_BODY(double, double)
379 TABLE_COL_BODY(intmax, intmax_t)
380 TABLE_COL_BODY(uintmax, uintmax_t)
381 TABLE_COL_BODY(s64, s64)
382 TABLE_COL_BODY(u64, u64)
383 TABLE_COL_BODY(bool, bool)
384 TABLE_COL_BODY(str, const char *)
386 void table_reset_row(struct table *tbl)
388 for(uint i = 0; i < tbl->cols_to_output; i++) {
389 tbl->column_order[i].cell_content = NULL;
391 mp_restore(tbl->pool, &tbl->pool_state);
392 tbl->row_printing_started = false;
395 void table_end_row(struct table *tbl)
397 ASSERT(tbl->formatter->row_output);
398 if(tbl->row_printing_started == false) return;
399 tbl->formatter->row_output(tbl);
400 table_reset_row(tbl);
403 /* Construction of a cell using a fastbuf */
405 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
407 fbpool_init(&tbl->fb_col_out);
408 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
410 return &tbl->fb_col_out.fb;
413 void table_col_fbend(struct table *tbl)
415 char *cell_content = fbpool_end(&tbl->fb_col_out);
416 table_col_raw(tbl, tbl->col_out, cell_content);
420 /*** Option parsing ***/
422 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
424 // Options with no value
425 if(value == NULL || (value != NULL && strlen(value) == 0)) {
426 if(strcmp(key, "noheader") == 0) {
427 tbl->print_header = false;
432 // Options with a value
434 if(strcmp(key, "header") == 0) {
436 const char *err = xt_bool.parse(value, &tmp, tbl->pool);
438 return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
440 tbl->print_header = tmp;
443 } else if(strcmp(key, "cols") == 0) {
444 return table_set_col_order_by_name(tbl, value);
445 } else if(strcmp(key, "fmt") == 0) {
446 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
447 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
448 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
450 return "Invalid argument to output-type option.";
453 } else if(strcmp(key, "cells") == 0) {
455 const char *err = xtype_parse_fmt(NULL, value, &fmt, tbl->pool);
456 if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
457 for(uint i = 0; i < tbl->cols_to_output; i++) {
458 tbl->column_order[i].fmt = fmt;
461 } else if(strcmp(key, "raw") == 0 || strcmp(key, "pretty") == 0) {
463 const char *err = xtype_parse_fmt(NULL, key, &fmt, tbl->pool);
464 if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
465 for(uint i = 0; i < tbl->cols_to_output; i++) {
466 tbl->column_order[i].fmt = fmt;
469 } else if(strcmp(key, "col-delim") == 0) {
470 char * d = mp_printf(tbl->pool, "%s", value);
471 tbl->col_delimiter = d;
477 if(tbl->formatter && tbl->formatter->process_option) {
478 const char *err = NULL;
479 if(tbl->formatter->process_option(tbl, key, value, &err)) {
484 // Unrecognized option
485 return mp_printf(tbl->pool, "Invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
488 const char *table_set_option(struct table *tbl, const char *opt)
490 char *key = stk_strdup(opt);
491 char *value = strchr(key, ':');
495 return table_set_option_value(tbl, key, value);
498 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
500 for(uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
501 const char *rv = table_set_option(tbl, gary_table_opts[i]);
509 /*** Default formatter for human-readable output ***/
511 static void table_row_human_readable(struct table *tbl)
513 for(uint i = 0; i < tbl->cols_to_output; i++) {
514 const struct table_column *col_def = tbl->column_order[i].col_def;
516 bputs(tbl->out, tbl->col_delimiter);
518 int col_width = col_def->width & CELL_WIDTH_MASK;
519 if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
520 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
522 bputc(tbl->out, '\n');
525 static void table_write_header(struct table *tbl)
527 for(uint i = 0; i < tbl->cols_to_output; i++) {
528 const struct table_column *col_def = tbl->column_order[i].col_def;
530 bputs(tbl->out, tbl->col_delimiter);
532 int col_width = col_def->width & CELL_WIDTH_MASK;
533 if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
534 bprintf(tbl->out, "%*s", col_width, col_def->name);
536 bputc(tbl->out, '\n');
539 static void table_start_human_readable(struct table *tbl)
541 if(tbl->col_delimiter == NULL) {
542 tbl->col_delimiter = " ";
545 if(tbl->print_header != false) {
546 table_write_header(tbl);
550 const struct table_formatter table_fmt_human_readable = {
551 .row_output = table_row_human_readable,
552 .table_start = table_start_human_readable,
555 /*** Default formatter for machine-readable output ***/
557 static void table_row_machine_readable(struct table *tbl)
559 for(uint i = 0; i < tbl->cols_to_output; i++) {
561 bputs(tbl->out, tbl->col_delimiter);
563 bputs(tbl->out, tbl->column_order[i].cell_content);
565 bputc(tbl->out, '\n');
568 static void table_start_machine_readable(struct table *tbl)
570 if(tbl->col_delimiter == NULL) {
571 tbl->col_delimiter = "\t";
574 if(tbl->print_header != false && tbl->cols_to_output > 0) {
575 bputs(tbl->out, tbl->column_order[0].col_def->name);
576 for(uint i = 1; i < tbl->cols_to_output; i++) {
577 bputs(tbl->out, tbl->col_delimiter);
578 bputs(tbl->out, tbl->column_order[i].col_def->name);
580 bputc(tbl->out, '\n');
584 const struct table_formatter table_fmt_machine_readable = {
585 .row_output = table_row_machine_readable,
586 .table_start = table_start_machine_readable,
590 /*** Blockline formatter ***/
592 static void table_row_blockline_output(struct table *tbl)
594 for(uint i = 0; i < tbl->cols_to_output; i++) {
595 const struct table_column *col_def = tbl->column_order[i].col_def;
596 bprintf(tbl->out, "%s: %s\n", col_def->name, tbl->column_order[i].cell_content);
598 bputc(tbl->out, '\n');
601 static void table_start_blockline(struct table *tbl)
603 if(tbl->col_delimiter == NULL) {
604 tbl->col_delimiter = "\n";
608 const struct table_formatter table_fmt_blockline = {
609 .row_output = table_row_blockline_output,
610 .table_start = table_start_blockline
619 enum test_table_cols {
620 TEST_COL0_STR, TEST_COL1_INT, TEST_COL2_UINT, TEST_COL3_BOOL, TEST_COL4_DOUBLE
623 static struct table_col_instance test_column_order[] = { TBL_COL(TEST_COL3_BOOL), TBL_COL(TEST_COL4_DOUBLE),
624 TBL_COL(TEST_COL2_UINT), TBL_COL(TEST_COL1_INT), TBL_COL(TEST_COL0_STR), TBL_COL_ORDER_END };
626 static struct table_template test_tbl = {
628 [TEST_COL0_STR] = TBL_COL_STR("col0_str", 20),
629 [TEST_COL1_INT] = TBL_COL_INT("col1_int", 8),
630 [TEST_COL2_UINT] = TBL_COL_UINT("col2_uint", 9),
631 [TEST_COL3_BOOL] = TBL_COL_BOOL_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
632 [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE("col4_double", 11),
635 TBL_COL_ORDER(test_column_order),
636 TBL_FMT_HUMAN_READABLE,
637 TBL_COL_DELIMITER("\t"),
641 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
643 static void do_print1(struct table *test_tbl)
645 table_col_str(test_tbl, TEST_COL0_STR, "sdsdf");
646 table_col_int(test_tbl, TEST_COL1_INT, -10);
647 table_col_int(test_tbl, TEST_COL1_INT, 10000);
648 table_col_uint(test_tbl, TEST_COL2_UINT, 10);
649 table_col_printf(test_tbl, TEST_COL2_UINT, "XXX-%u", 22222);
650 table_col_bool(test_tbl, TEST_COL3_BOOL, true);
651 table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
652 table_col_printf(test_tbl, TEST_COL4_DOUBLE, "AAA");
653 table_end_row(test_tbl);
655 table_col_str(test_tbl, TEST_COL0_STR, "test");
656 table_col_int(test_tbl, TEST_COL1_INT, -100);
657 table_col_uint(test_tbl, TEST_COL2_UINT, 100);
658 table_col_bool(test_tbl, TEST_COL3_BOOL, false);
659 table_col_printf(test_tbl, TEST_COL4_DOUBLE, "%.2lf", 1.5);
660 table_end_row(test_tbl);
663 static void test_simple1(struct fastbuf *out)
665 struct table *tbl = table_init(&test_tbl);
667 // print table with header
668 table_set_col_order_by_name(tbl, "col3_bool");
669 table_start(tbl, out);
673 // print the same table as in the previous case without header
674 table_set_col_order_by_name(tbl, "col0_str,col2_uint,col1_int,col3_bool");
675 table_start(tbl, out);
679 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
680 tbl->print_header = false;
681 table_start(tbl, out);
684 tbl->print_header = true;
686 table_set_col_order_by_name(tbl, "col3_bool");
687 table_start(tbl, out);
691 table_set_col_order_by_name(tbl, "col3_bool,col0_str");
692 table_start(tbl, out);
696 table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint");
697 table_start(tbl, out);
701 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");
702 table_start(tbl, out);
706 table_set_col_order_by_name(tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
707 table_start(tbl, out);
712 // test table_col_order_fmt
713 struct table_col_instance col_order[] = { TBL_COL(TEST_COL0_STR), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_PRETTY), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_RAW), TBL_COL_ORDER_END };
714 table_set_col_order(tbl, col_order);
715 table_start(tbl, out);
717 table_col_str(tbl, TEST_COL0_STR, "test");
718 table_col_double(tbl, TEST_COL4_DOUBLE, 1.23456789);
721 table_col_str(tbl, TEST_COL0_STR, "test");
722 table_col_double(tbl, TEST_COL4_DOUBLE, 1.23456789);
730 enum test_any_table_cols {
731 TEST_ANY_COL0_INT, TEST_ANY_COL1_ANY
734 static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL_FMT(TEST_ANY_COL1_ANY, XTYPE_FMT_PRETTY), TBL_COL_ORDER_END };
736 static struct table_template test_any_tbl = {
738 [TEST_ANY_COL0_INT] = TBL_COL_INT("col0_int", 8),
739 [TEST_ANY_COL1_ANY] = TBL_COL_ANY_FMT("col1_any", 9, XTYPE_FMT_PRETTY),
742 TBL_COL_ORDER(test_any_column_order),
743 TBL_FMT_HUMAN_READABLE,
744 TBL_COL_DELIMITER("\t"),
747 static void test_any_type(struct fastbuf *out)
749 struct table *tbl = table_init(&test_any_tbl);
751 table_start(tbl, out);
753 table_col_int(tbl, TEST_ANY_COL0_INT, -10);
754 table_col_int(tbl, TEST_ANY_COL1_ANY, 10000);
757 table_col_int(tbl, TEST_ANY_COL0_INT, -10);
758 table_col_double(tbl, TEST_ANY_COL1_ANY, 1.4);
761 table_col_printf(tbl, TEST_ANY_COL0_INT, "%d", 10);
762 table_col_double(tbl, TEST_ANY_COL1_ANY, 1.4);
769 int main(int argc UNUSED, char **argv UNUSED)
772 out = bfdopen_shared(1, 4096);