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 void table_init(struct table *tbl)
25 int col_count = 0; // count the number of columns in the struct table
28 if(tbl->columns[col_count].name == NULL &&
29 tbl->columns[col_count].fmt == NULL &&
30 tbl->columns[col_count].width == 0 &&
31 tbl->columns[col_count].type == COL_TYPE_LAST)
33 ASSERT(tbl->columns[col_count].name != NULL);
34 ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl->columns[col_count].fmt != NULL);
35 ASSERT(tbl->columns[col_count].width != 0);
39 tbl->pool = mp_new(4096);
41 tbl->column_count = col_count;
44 tbl->formatter = &table_fmt_human_readable;
47 tbl->print_header = 1; // by default, print header
50 void table_cleanup(struct table *tbl)
53 memset(tbl, 0, sizeof(struct table));
56 // TODO: test default column order
57 static void table_make_default_column_order(struct table *tbl)
59 int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count);
60 for(int i = 0; i < tbl->column_count; i++) {
63 table_set_col_order(tbl, col_order_int, tbl->column_count);
66 void table_start(struct table *tbl, struct fastbuf *out)
68 tbl->last_printed_col = -1;
69 tbl->row_printing_started = 0;
72 ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
74 if(tbl->column_order == NULL) table_make_default_column_order(tbl);
76 // update linked lists
79 if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
81 mp_save(tbl->pool, &tbl->pool_state);
83 ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
86 void table_end(struct table *tbl)
88 tbl->last_printed_col = -1;
89 tbl->row_printing_started = 0;
91 mp_restore(tbl->pool, &tbl->pool_state);
93 if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
96 /*** Configuration ***/
98 void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
100 tbl->formatter = fmt;
103 int table_get_col_idx(struct table *tbl, const char *col_name)
105 for(int i = 0; i < tbl->column_count; i++) {
106 if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
111 const char * table_get_col_list(struct table *tbl)
113 if(tbl->column_count == 0) return "";
115 char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
117 for(int i = 1; i < tbl->column_count; i++) {
118 tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
124 static void table_update_ll(struct table *tbl)
126 int cols_to_output = tbl->cols_to_output;
128 for(int i = 0; i < tbl->column_count; i++) {
129 tbl->columns[i].first_column = -1;
130 tbl->columns[i].last_column = -1;
133 for(int i = 0; i < cols_to_output; i++) {
134 int idx = tbl->column_order[i].idx;
135 tbl->column_order[i].col_def = tbl->columns + idx;
138 for(int i = 0; i < cols_to_output; i++) {
139 int last = tbl->column_order[i].col_def->last_column;
141 tbl->column_order[i].col_def->last_column = i;
142 tbl->column_order[last].next_column = i;
144 tbl->column_order[i].col_def->last_column = i;
145 tbl->column_order[i].col_def->first_column = i;
147 tbl->column_order[i].next_column = -1;
151 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
153 for(int i = 0; i < cols_to_output; i++) {
154 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);
157 tbl->cols_to_output = cols_to_output;
158 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * cols_to_output);
159 for(int i = 0; i < cols_to_output; i++) {
160 int col_idx = col_order[i];
161 tbl->column_order[i].idx = col_idx;
162 tbl->column_order[i].col_def = tbl->columns + col_idx;
163 tbl->column_order[i].cell_content = NULL;
164 tbl->column_order[i].output_type = CELL_OUT_UNINITIALIZED;
166 table_update_ll(tbl);
169 bool table_col_is_printed(struct table *tbl, uint col_idx)
171 if(tbl->columns[col_idx].first_column == -1) return 0;
176 static char * table_parse_col_arg(char *col_def)
178 // FIXME: should be switched to str_sepsplit
179 char * left_br = strchr(col_def, '[');
180 if(left_br == NULL) return NULL;
183 char *right_br = strchr(left_br, ']');
190 * Setting options for basic table types (as defined in table.h)
192 bool table_set_col_opt_default(struct table *tbl, int col_copy_idx, const char *col_arg, char **err)
194 struct table_column *tmp_col = tbl->column_order[col_copy_idx].col_def;
195 int col_type_idx = tbl->column_order[col_copy_idx].idx;
197 if(tmp_col->type == COL_TYPE_DOUBLE) {
199 str_to_uint(&precision, col_arg, NULL, 0);
200 tbl->column_order[col_type_idx].output_type = precision;
204 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
209 * TODO: This function deliberately leaks memory. When it is called multiple times,
210 * previous column orders still remain allocated in the table's memory pool.
212 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
214 if(col_order_str[0] == '*') {
215 int *col_order_int = alloca(sizeof(int) * tbl->column_count);
216 for(int i = 0; i < tbl->column_count; i++) {
217 col_order_int[i] = i;
219 table_set_col_order(tbl, col_order_int, tbl->column_count);
224 if(!col_order_str[0]) {
225 tbl->column_order = mp_alloc(tbl->pool, 0);
226 tbl->cols_to_output = 0;
230 char *tmp_col_order = stk_strdup(col_order_str);
233 for(int i = 0; col_order_str[i] != 0; i++) {
234 if(col_order_str[i] == ',') {
239 tbl->cols_to_output = col_count;
240 tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * col_count);
242 int curr_col_idx = 0;
243 char *name_start = tmp_col_order;
245 char *next = strchr(name_start, ',');
250 char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
251 int col_idx = table_get_col_idx(tbl, name_start);
254 return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
256 tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
257 tbl->column_order[curr_col_idx].idx = col_idx;
258 tbl->column_order[curr_col_idx].cell_content = NULL;
259 tbl->column_order[curr_col_idx].output_type = CELL_OUT_UNINITIALIZED;
260 if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].type_def->set_col_instance_option) {
262 tbl->columns[col_idx].type_def->set_col_instance_option(tbl, curr_col_idx, arg, &err);
263 if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
270 table_update_ll(tbl);
275 /*** Table cells ***/
277 static void table_set_all_cols_content(struct table *tbl, int col, char *col_content, int override)
279 int curr_col = tbl->columns[col].first_column;
280 while(curr_col != -1) {
281 if(override == 0 && tbl->column_order[curr_col].output_type != CELL_OUT_UNINITIALIZED ) {
282 die("Error while setting content of all cells of a single type column, cell format should not be overriden.");
284 tbl->column_order[curr_col].cell_content = col_content;
285 curr_col = tbl->column_order[curr_col].next_column;
289 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
291 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
292 tbl->last_printed_col = col;
293 tbl->row_printing_started = 1;
296 char *cell_content = mp_vprintf(tbl->pool, fmt, args);
297 table_set_all_cols_content(tbl, col, cell_content, 1);
301 static const char *table_col_default_fmts[] = {
302 [COL_TYPE_STR] = "%s",
303 [COL_TYPE_INT] = "%d",
304 [COL_TYPE_S64] = "%lld",
305 [COL_TYPE_INTMAX] = "%jd",
306 [COL_TYPE_UINT] = "%u",
307 [COL_TYPE_U64] = "%llu",
308 [COL_TYPE_UINTMAX] = "%ju",
309 [COL_TYPE_BOOL] = "%d",
310 [COL_TYPE_DOUBLE] = "%.2lf",
311 [COL_TYPE_ANY] = NULL,
312 [COL_TYPE_LAST] = NULL
315 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
317 const char *fmt = tbl->columns[col].fmt;\
318 if(tbl->columns[col].type == COL_TYPE_ANY) {\
319 fmt = table_col_default_fmts[_typeconst_];\
321 table_col_##_name_##_fmt(tbl, col, fmt, val);\
324 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
326 int col = table_get_col_idx(tbl, col_name);\
327 table_col_##_name_(tbl, col, val);\
330 #define TABLE_COL_FMT(_name_, _type_, _typeconst_, _override) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val) \
332 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
333 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
334 ASSERT(fmt != NULL);\
335 tbl->last_printed_col = col;\
336 tbl->row_printing_started = 1;\
337 char *cell_content = mp_printf(tbl->pool, fmt, val);\
338 table_set_all_cols_content(tbl, col, cell_content, _override);\
341 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_, _override) TABLE_COL(_name_, _type_, _typeconst_); \
342 TABLE_COL_STR(_name_, _type_, _typeconst_);\
343 TABLE_COL_FMT(_name_, _type_, _typeconst_, _override);
345 TABLE_COL_BODIES(int, int, COL_TYPE_INT, 0)
346 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT, 0)
347 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR, 1)
348 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX, 0)
349 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX, 0)
350 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64, 0)
351 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64, 0)
353 // column type double is a special case
354 TABLE_COL(double, double, COL_TYPE_DOUBLE);
355 TABLE_COL_STR(double, double, COL_TYPE_DOUBLE);
359 #undef TABLE_COL_BODIES
361 void table_col_double_fmt(struct table *tbl, int col, const char *fmt, double val)
363 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
364 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_DOUBLE == tbl->columns[col].type);
366 tbl->last_printed_col = col;
367 tbl->row_printing_started = 1;
368 char *cell_content = mp_printf(tbl->pool, fmt, val);
369 int curr_col = tbl->columns[col].first_column;
370 while(curr_col != -1) {
371 if(tbl->column_order[curr_col].output_type < 0) tbl->column_order[curr_col].cell_content = cell_content;
373 char *cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val);
374 tbl->column_order[curr_col].cell_content = cell_content_tmp;
376 curr_col = tbl->column_order[curr_col].next_column;
380 void table_col_bool(struct table *tbl, int col, uint val)
382 table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
385 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
387 int col = table_get_col_idx(tbl, col_name);
388 table_col_bool(tbl, col, val);
391 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
393 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
394 ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
396 tbl->last_printed_col = col;
397 tbl->row_printing_started = 1;
399 int curr_col = tbl->columns[col].first_column;
400 while(curr_col != -1) {
401 switch(tbl->column_order[curr_col].output_type) {
402 case CELL_OUT_HUMAN_READABLE:
403 case CELL_OUT_UNINITIALIZED:
404 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "true" : "false");
406 case CELL_OUT_MACHINE_READABLE:
407 // FIXME: this is just an example of printing in different formats
408 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "1" : "0");
411 die("Unsupported output type.");
413 curr_col = tbl->column_order[curr_col].next_column;
416 // FIXME: add to printing of all columns
419 void table_reset_row(struct table *tbl)
421 for(uint i = 0; i < tbl->cols_to_output; i++) {
422 tbl->column_order[i].cell_content = NULL;
424 mp_restore(tbl->pool, &tbl->pool_state);
425 tbl->last_printed_col = -1;
426 tbl->row_printing_started = 0;
429 void table_end_row(struct table *tbl)
431 ASSERT(tbl->formatter->row_output);
432 if(tbl->row_printing_started == 0) return;
433 tbl->formatter->row_output(tbl);
434 table_reset_row(tbl);
437 /* Construction of a cell using a fastbuf */
439 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
441 fbpool_init(&tbl->fb_col_out);
442 fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
444 return &tbl->fb_col_out.fb;
447 void table_col_fbend(struct table *tbl)
449 char *cell_content = fbpool_end(&tbl->fb_col_out);
450 table_set_all_cols_content(tbl, tbl->col_out, cell_content, 1);
454 /*** Option parsing ***/
456 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
458 // Options with no value
459 if(value == NULL || (value != NULL && strlen(value) == 0)) {
460 if(strcmp(key, "noheader") == 0) {
461 tbl->print_header = 0;
466 // Options with a value
468 if(strcmp(key, "header") == 0) {
470 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
471 uint tmp = value[0] - '0';
473 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
474 tbl->print_header = tmp;
476 } else if(strcmp(key, "cols") == 0) {
477 const char *err = table_set_col_order_by_name(tbl, value);
479 } else if(strcmp(key, "fmt") == 0) {
480 if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
481 else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
482 else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
484 return "Tableprinter: invalid argument to output-type option.";
487 } else if(strcmp(key, "col-delim") == 0) {
488 char * d = mp_printf(tbl->pool, "%s", value);
489 tbl->col_delimiter = d;
495 if(tbl->formatter && tbl->formatter->process_option) {
496 const char *err = NULL;
497 if(tbl->formatter->process_option(tbl, key, value, &err)) {
502 // Unrecognized option
503 return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
506 const char *table_set_option(struct table *tbl, const char *opt)
508 char *key = stk_strdup(opt);
509 char *value = strchr(key, ':');
513 return table_set_option_value(tbl, key, value);
516 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
518 for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
519 const char *rv = table_set_option(tbl, gary_table_opts[i]);
527 /*** Default formatter for human-readable output ***/
529 static void table_row_human_readable(struct table *tbl)
531 for(uint i = 0; i < tbl->cols_to_output; i++) {
532 int col_idx = tbl->column_order[i].idx;
534 bputs(tbl->out, tbl->col_delimiter);
536 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
537 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
538 bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
540 bputc(tbl->out, '\n');
543 static void table_write_header(struct table *tbl)
545 for(uint i = 0; i < tbl->cols_to_output; i++) {
546 int col_idx = tbl->column_order[i].col_def - tbl->columns;
548 bputs(tbl->out, tbl->col_delimiter);
550 int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
551 if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
552 bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
554 bputc(tbl->out, '\n');
557 static void table_start_human_readable(struct table *tbl)
559 if(tbl->col_delimiter == NULL) {
560 tbl->col_delimiter = " ";
563 if(tbl->print_header != 0) {
564 table_write_header(tbl);
568 struct table_formatter table_fmt_human_readable = {
569 .row_output = table_row_human_readable,
570 .table_start = table_start_human_readable,
573 /*** Default formatter for machine-readable output ***/
575 static void table_row_machine_readable(struct table *tbl)
577 for(uint i = 0; i < tbl->cols_to_output; i++) {
579 bputs(tbl->out, tbl->col_delimiter);
581 bputs(tbl->out, tbl->column_order[i].cell_content);
583 bputc(tbl->out, '\n');
586 static void table_start_machine_readable(struct table *tbl)
588 if(tbl->col_delimiter == NULL) {
589 tbl->col_delimiter = "\t";
592 if(tbl->print_header != 0) {
593 uint col_idx = tbl->column_order[0].col_def - tbl->columns;
594 bputs(tbl->out, tbl->columns[col_idx].name);
595 for(uint i = 1; i < tbl->cols_to_output; i++) {
596 col_idx = tbl->column_order[i].col_def - tbl->columns;
597 bputs(tbl->out, tbl->col_delimiter);
598 bputs(tbl->out, tbl->columns[col_idx].name);
600 bputc(tbl->out, '\n');
604 struct table_formatter table_fmt_machine_readable = {
605 .row_output = table_row_machine_readable,
606 .table_start = table_start_machine_readable,
610 /*** Blockline formatter ***/
612 static void table_row_blockline_output(struct table *tbl)
614 for(uint i = 0; i < tbl->cols_to_output; i++) {
615 int col_idx = tbl->column_order[i].idx;
616 bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->column_order[i].cell_content);
618 bputc(tbl->out, '\n');
621 static void table_start_blockline(struct table *tbl)
623 if(tbl->col_delimiter == NULL) {
624 tbl->col_delimiter = "\n";
628 struct table_formatter table_fmt_blockline = {
629 .row_output = table_row_blockline_output,
630 .table_start = table_start_blockline
641 enum test_table_cols {
642 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
645 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) };
647 static struct table test_tbl = {
649 [test_col0_str] = TBL_COL_STR("col0_str", 20),
650 [test_col1_int] = TBL_COL_INT("col1_int", 8),
651 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
652 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
653 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
656 TBL_COL_ORDER(test_column_order),
657 TBL_OUTPUT_HUMAN_READABLE,
658 TBL_COL_DELIMITER("\t"),
662 * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
664 static void do_print1(struct table *test_tbl)
666 table_col_str(test_tbl, test_col0_str, "sdsdf");
667 table_col_int(test_tbl, test_col1_int, -10);
668 table_col_int(test_tbl, test_col1_int, 10000);
669 table_col_uint(test_tbl, test_col2_uint, 10);
670 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
671 table_col_bool(test_tbl, test_col3_bool, 1);
672 table_col_double(test_tbl, test_col4_double, 1.5);
673 table_col_printf(test_tbl, test_col4_double, "AAA");
674 table_end_row(test_tbl);
676 table_col_str(test_tbl, test_col0_str, "test");
677 table_col_int(test_tbl, test_col1_int, -100);
678 table_col_uint(test_tbl, test_col2_uint, 100);
679 table_col_bool(test_tbl, test_col3_bool, 0);
680 table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
681 table_end_row(test_tbl);
684 static void test_simple1(struct fastbuf *out)
686 table_init(&test_tbl);
688 // print table with header
689 table_set_col_order_by_name(&test_tbl, "col3_bool");
690 table_start(&test_tbl, out);
691 do_print1(&test_tbl);
692 table_end(&test_tbl);
694 // print the same table as in the previous case without header
695 table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
696 table_start(&test_tbl, out);
697 do_print1(&test_tbl);
698 table_end(&test_tbl);
700 // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
701 test_tbl.print_header = 0;
702 table_start(&test_tbl, out);
703 do_print1(&test_tbl);
704 table_end(&test_tbl);
705 test_tbl.print_header = 1;
707 table_set_col_order_by_name(&test_tbl, "col3_bool");
708 table_start(&test_tbl, out);
709 do_print1(&test_tbl);
710 table_end(&test_tbl);
712 table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
713 table_start(&test_tbl, out);
714 do_print1(&test_tbl);
715 table_end(&test_tbl);
717 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
718 table_start(&test_tbl, out);
719 do_print1(&test_tbl);
720 table_end(&test_tbl);
722 table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
723 table_start(&test_tbl, out);
724 do_print1(&test_tbl);
725 table_end(&test_tbl);
727 table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
728 table_start(&test_tbl, out);
729 do_print1(&test_tbl);
730 table_end(&test_tbl);
732 table_cleanup(&test_tbl);
735 enum test_any_table_cols {
736 test_any_col0_int, test_any_col1_any
739 static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
741 static struct table test_any_tbl = {
743 [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
744 [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
747 TBL_COL_ORDER(test_any_column_order),
748 TBL_OUTPUT_HUMAN_READABLE,
749 TBL_COL_DELIMITER("\t"),
752 static void test_any_type(struct fastbuf *out)
754 table_init(&test_any_tbl);
756 table_start(&test_any_tbl, out);
758 table_col_int(&test_any_tbl, test_any_col0_int, -10);
759 table_col_int(&test_any_tbl, test_any_col1_any, 10000);
760 table_end_row(&test_any_tbl);
762 table_col_int(&test_any_tbl, test_any_col0_int, -10);
763 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
764 table_end_row(&test_any_tbl);
766 table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
767 table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
768 table_end_row(&test_any_tbl);
770 table_end(&test_any_tbl);
771 table_cleanup(&test_any_tbl);
774 int main(int argc UNUSED, char **argv UNUSED)
777 out = bfdopen_shared(1, 4096);