]> mj.ucw.cz Git - libucw.git/blob - ucw/table.c
tableprinter: code cleanup, update of usage of table_set_col_opt
[libucw.git] / ucw / table.c
1 /*
2  *      UCW Library -- Table printer
3  *
4  *      (c) 2014 Robert Kessl <robert.kessl@economia.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/string.h>
9 #include <ucw/stkstring.h>
10 #include <ucw/gary.h>
11 #include <ucw/table.h>
12 #include <ucw/strtonum.h>
13
14 #include <stdlib.h>
15 #include <stdio.h>
16
17 /* Forward declarations */
18
19 static void table_update_ll(struct table *tbl);
20
21 /*** Management of tables ***/
22
23 struct table *table_init(const struct table_template *tbl_template)
24 {
25   struct mempool *pool = mp_new(4096);
26   struct table *new_inst = mp_alloc_zero(pool, sizeof(struct table));
27
28   new_inst->pool = pool;
29
30   // initialize column definitions
31   uint col_count = 0; // count the number of columns in the struct table
32   for(;;) {
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)
36       break;
37     ASSERT(tbl_template->columns[col_count].name != NULL);
38     ASSERT(tbl_template->columns[col_count].width != 0);
39
40     col_count++;
41   }
42   new_inst->column_count = col_count;
43
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;
48   }
49
50   // initialize column_order
51   if(tbl_template->column_order) {
52     new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
53     memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
54     for(uint i = 0; i < new_inst->cols_to_output; i++) {
55       new_inst->column_order[i].cell_content = NULL;
56       int col_def_idx = new_inst->column_order[i].idx;
57       new_inst->column_order[i].col_def = new_inst->columns + col_def_idx;
58       new_inst->column_order[i].fmt = tbl_template->columns[col_def_idx].fmt;
59     }
60
61     new_inst->cols_to_output = tbl_template->cols_to_output;
62   }
63
64   new_inst->col_delimiter = tbl_template->col_delimiter;
65   new_inst->print_header = true;
66   new_inst->out = 0;
67   new_inst->row_printing_started = false;
68   new_inst->col_out = -1;
69   new_inst->formatter = tbl_template->formatter;
70   if(!new_inst->formatter) {
71     new_inst->formatter = &table_fmt_human_readable;
72   }
73   new_inst->formatter_data = NULL;
74   return new_inst;
75 }
76
77 void table_cleanup(struct table *tbl)
78 {
79   mp_delete(tbl->pool);
80 }
81
82 // TODO: test default column order
83 static void table_make_default_column_order(struct table *tbl)
84 {
85   int *col_order_int = alloca(sizeof(int) * tbl->column_count);
86
87   for(int i = 0; i < tbl->column_count; i++) {
88     col_order_int[i] = i;
89   }
90   table_set_col_order(tbl, col_order_int, tbl->column_count);
91 }
92
93 void table_start(struct table *tbl, struct fastbuf *out)
94 {
95   tbl->row_printing_started = false;
96   tbl->out = out;
97
98   ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
99
100   if(tbl->column_order == NULL) table_make_default_column_order(tbl);
101   else {
102     // update linked lists
103     table_update_ll(tbl);
104   }
105   if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
106
107   mp_save(tbl->pool, &tbl->pool_state);
108
109   ASSERT_MSG(tbl->col_delimiter, "Column delimiter not specified.");
110 }
111
112 void table_end(struct table *tbl)
113 {
114   tbl->row_printing_started = false;
115
116   mp_restore(tbl->pool, &tbl->pool_state);
117
118   if(tbl->formatter->table_end) tbl->formatter->table_end(tbl);
119 }
120
121 /*** Configuration ***/
122
123 void table_set_formatter(struct table *tbl, const struct table_formatter *fmt)
124 {
125   tbl->formatter = fmt;
126 }
127
128 int table_get_col_idx(struct table *tbl, const char *col_name)
129 {
130   for(int i = 0; i < tbl->column_count; i++) {
131     if(strcmp(tbl->columns[i].name, col_name) == 0) return i;
132   }
133   return -1;
134 }
135
136 const char * table_get_col_list(struct table *tbl)
137 {
138   if(tbl->column_count == 0) return "";
139
140   char *tmp = mp_strdup(tbl->pool, tbl->columns[0].name);
141
142   for(int i = 1; i < tbl->column_count; i++) {
143     tmp = mp_printf_append(tbl->pool, tmp, ", %s", tbl->columns[i].name);
144   }
145
146   return tmp;
147 }
148
149 static void table_update_ll(struct table *tbl)
150 {
151   int cols_to_output = tbl->cols_to_output;
152
153   for(int i = 0; i < tbl->column_count; i++) {
154     tbl->ll_headers[i] = -1;
155   }
156
157   for(int i = 0; i < cols_to_output; i++) {
158     int col_def_idx = tbl->column_order[i].idx;
159     tbl->column_order[i].col_def = tbl->columns + col_def_idx;
160   }
161
162   for(int i = 0; i < cols_to_output; i++) {
163     int col_def_idx = tbl->column_order[i].idx;
164     int first = tbl->ll_headers[col_def_idx];
165     tbl->ll_headers[col_def_idx] = i;
166     tbl->column_order[i].next_column = first;
167   }
168 }
169
170 void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
171 {
172   for(int i = 0; i < cols_to_output; i++) {
173     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);
174   }
175
176   tbl->cols_to_output = cols_to_output;
177   tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * cols_to_output);
178   for(int i = 0; i < cols_to_output; i++) {
179     int col_def_idx = col_order[i];
180     tbl->column_order[i].idx = col_def_idx;
181     tbl->column_order[i].col_def = tbl->columns + col_def_idx;
182     tbl->column_order[i].cell_content = NULL; // it is in fact initialized by mp_alloc_zero, but for completeness ...
183     tbl->column_order[i].next_column = -1;
184     tbl->column_order[i].fmt = tbl->columns[col_def_idx].fmt;
185   }
186   table_update_ll(tbl);
187 }
188
189 bool table_col_is_printed(struct table *tbl, uint col_def_idx)
190 {
191   if(tbl->ll_headers[col_def_idx] == -1) return 0;
192
193   return 1;
194 }
195
196 static char * table_parse_col_arg(char *col_def)
197 {
198   // FIXME: should be switched to str_sepsplit
199   char * left_br = strchr(col_def, '[');
200   if(left_br == NULL) return NULL;
201   *left_br = 0;
202   left_br++;
203   char *right_br = strchr(left_br, ']');
204   *right_br = 0;
205   return left_br;
206 }
207
208 const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *col_opt)
209 {
210   const struct table_column *col_def = tbl->column_order[col_inst_idx].col_def;
211
212   // Make sure that we do not call table_set_col_opt, which would
213   // result in an infinite recursion.
214   if(col_def && col_def->set_col_opt) {
215     if(col_def->set_col_opt == table_set_col_opt) {
216       die("table_set_col_opt should not be used as a struct table_column::set_col_opt hook");
217     }
218     return col_def->set_col_opt(tbl, col_inst_idx, col_opt);
219   }
220
221   if(col_def && col_def->type_def && col_def->type_def->parse_fmt) {
222     uint fmt = 0;
223     const char *tmp_err = col_def->type_def->parse_fmt(col_opt, &fmt, tbl->pool);
224     if(tmp_err) return tmp_err;
225     tbl->column_order[col_inst_idx].fmt = fmt;
226     return NULL;
227   }
228
229   return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_inst_idx);
230 }
231
232 /**
233  * TODO: This function deliberately leaks memory. When it is called multiple times,
234  * previous column orders still remain allocated in the table's memory pool.
235  **/
236 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
237 {
238   if(col_order_str[0] == '*') {
239     table_make_default_column_order(tbl);
240     return NULL;
241   }
242
243   if(!col_order_str[0]) {
244     tbl->column_order = mp_alloc(tbl->pool, 0);
245     tbl->cols_to_output = 0;
246     return NULL;
247   }
248
249   char *tmp_col_order = stk_strdup(col_order_str);
250
251   int col_count = 1;
252   for(int i = 0; col_order_str[i] != 0; i++) {
253     if(col_order_str[i] == ',') {
254       col_count++;
255     }
256   }
257
258   tbl->cols_to_output = col_count;
259   tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * col_count);
260
261   int curr_col_inst_idx = 0;
262   char *name_start = tmp_col_order;
263   while(name_start) {
264     char *next = strchr(name_start, ',');
265     if(next) {
266       *next++ = 0;
267     }
268
269     char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
270     int col_def_idx = table_get_col_idx(tbl, name_start);
271
272     if(col_def_idx == -1) {
273       return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
274     }
275     tbl->column_order[curr_col_inst_idx].col_def = tbl->columns + col_def_idx;
276     tbl->column_order[curr_col_inst_idx].idx = col_def_idx;
277     tbl->column_order[curr_col_inst_idx].fmt = tbl->columns[col_def_idx].fmt;
278     if(arg) {
279       const char *err = NULL;
280       err = table_set_col_opt(tbl, curr_col_inst_idx, arg);
281       if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
282     }
283
284     name_start = next;
285     curr_col_inst_idx++;
286   }
287
288   table_update_ll(tbl);
289
290   return NULL;
291 }
292
293 /*** Table cells ***/
294
295 static void table_col_raw(struct table *tbl, int col_templ, const char *col_content)
296 {
297   TBL_COL_ITER_START(tbl, col_templ, curr_col_ptr, curr_col) {
298     curr_col_ptr->cell_content = col_content;
299   } TBL_COL_ITER_END
300 }
301
302 void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type)
303 {
304   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
305   ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == tbl->columns[col].type_def);
306   tbl->row_printing_started = true;
307   TBL_COL_ITER_START(tbl, col, curr_col, curr_col_inst_idx) {
308     enum xtype_fmt fmt = curr_col->fmt;
309     curr_col->cell_content = expected_type->format(value, fmt, tbl->pool);
310   } TBL_COL_ITER_END
311 }
312
313 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
314 {
315   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
316   tbl->row_printing_started = true;
317   va_list args;
318   va_start(args, fmt);
319   char *cell_content = mp_vprintf(tbl->pool, fmt, args);
320   table_col_raw(tbl, col, cell_content);
321   va_end(args);
322 }
323
324 TABLE_COL_BODY(int, int)
325 TABLE_COL_BODY(uint, uint)
326 TABLE_COL_BODY(double, double)
327 TABLE_COL_BODY(intmax, intmax_t)
328 TABLE_COL_BODY(uintmax, uintmax_t)
329 TABLE_COL_BODY(s64, s64)
330 TABLE_COL_BODY(u64, u64)
331 TABLE_COL_BODY(bool, bool)
332 TABLE_COL_BODY(str, const char *)
333
334 void table_reset_row(struct table *tbl)
335 {
336   for(uint i = 0; i < tbl->cols_to_output; i++) {
337     tbl->column_order[i].cell_content = NULL;
338   }
339   mp_restore(tbl->pool, &tbl->pool_state);
340   tbl->row_printing_started = false;
341 }
342
343 void table_end_row(struct table *tbl)
344 {
345   ASSERT(tbl->formatter->row_output);
346   if(tbl->row_printing_started == false) return;
347   tbl->formatter->row_output(tbl);
348   table_reset_row(tbl);
349 }
350
351 /* Construction of a cell using a fastbuf */
352
353 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
354 {
355   fbpool_init(&tbl->fb_col_out);
356   fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
357   tbl->col_out = col;
358   return &tbl->fb_col_out.fb;
359 }
360
361 void table_col_fbend(struct table *tbl)
362 {
363   char *cell_content = fbpool_end(&tbl->fb_col_out);
364   table_col_raw(tbl, tbl->col_out, cell_content);
365   tbl->col_out = -1;
366 }
367
368 /*** Option parsing ***/
369
370 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
371 {
372   // Options with no value
373   if(value == NULL || (value != NULL && strlen(value) == 0)) {
374     if(strcmp(key, "noheader") == 0) {
375       tbl->print_header = false;
376       return NULL;
377     }
378   }
379
380   // Options with a value
381   if(value) {
382     if(strcmp(key, "header") == 0) {
383       bool tmp;
384       const char *err = xt_bool.parse(value, &tmp, tbl->pool);
385       if(err)
386         return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
387
388       tbl->print_header = tmp;
389
390       return NULL;
391     } else if(strcmp(key, "cols") == 0) {
392       return table_set_col_order_by_name(tbl, value);
393     } else if(strcmp(key, "fmt") == 0) {
394       if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
395       else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
396       else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
397       else {
398         return "Invalid argument to output-type option.";
399       }
400       return NULL;
401     } else if(strcmp(key, "cells") == 0) {
402       u32 fmt = 0;
403       const char *err = xtype_parse_fmt(NULL, value, &fmt, tbl->pool);
404       if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
405       for(uint i = 0; i < tbl->cols_to_output; i++) {
406         tbl->column_order[i].fmt = fmt;
407       }
408       return NULL;
409     } else if(strcmp(key, "raw") == 0 || strcmp(key, "pretty") == 0) {
410       u32 fmt = 0;
411       const char *err = xtype_parse_fmt(NULL, key, &fmt, tbl->pool);
412       if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
413       for(uint i = 0; i < tbl->cols_to_output; i++) {
414         tbl->column_order[i].fmt = fmt;
415       }
416       return NULL;
417     } else if(strcmp(key, "col-delim") == 0) {
418       char * d = mp_printf(tbl->pool, "%s", value);
419       tbl->col_delimiter = d;
420       return NULL;
421     }
422   }
423
424   // Formatter options
425   if(tbl->formatter && tbl->formatter->process_option) {
426     const char *err = NULL;
427     if(tbl->formatter->process_option(tbl, key, value, &err)) {
428       return err;
429     }
430   }
431
432   // Unrecognized option
433   return mp_printf(tbl->pool, "Invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
434 }
435
436 const char *table_set_option(struct table *tbl, const char *opt)
437 {
438   char *key = stk_strdup(opt);
439   char *value = strchr(key, ':');
440   if(value) {
441     *value++ = 0;
442   }
443   return table_set_option_value(tbl, key, value);
444 }
445
446 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
447 {
448   for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
449     const char *rv = table_set_option(tbl, gary_table_opts[i]);
450     if(rv != NULL) {
451       return rv;
452     }
453   }
454   return NULL;
455 }
456
457 /*** Default formatter for human-readable output ***/
458
459 static void table_row_human_readable(struct table *tbl)
460 {
461   for(uint i = 0; i < tbl->cols_to_output; i++) {
462     const struct table_column *col_def = tbl->column_order[i].col_def;
463     if(i) {
464       bputs(tbl->out, tbl->col_delimiter);
465     }
466     int col_width = col_def->width & CELL_WIDTH_MASK;
467     if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
468     bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
469   }
470   bputc(tbl->out, '\n');
471 }
472
473 static void table_write_header(struct table *tbl)
474 {
475   for(uint i = 0; i < tbl->cols_to_output; i++) {
476     const struct table_column *col_def = tbl->column_order[i].col_def;
477     if(i) {
478       bputs(tbl->out, tbl->col_delimiter);
479     }
480     int col_width = col_def->width & CELL_WIDTH_MASK;
481     if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
482     bprintf(tbl->out, "%*s", col_width, col_def->name);
483   }
484   bputc(tbl->out, '\n');
485 }
486
487 static void table_start_human_readable(struct table *tbl)
488 {
489   if(tbl->col_delimiter == NULL) {
490     tbl->col_delimiter = " ";
491   }
492
493   if(tbl->print_header != false) {
494     table_write_header(tbl);
495   }
496 }
497
498 const struct table_formatter table_fmt_human_readable = {
499   .row_output = table_row_human_readable,
500   .table_start = table_start_human_readable,
501 };
502
503 /*** Default formatter for machine-readable output ***/
504
505 static void table_row_machine_readable(struct table *tbl)
506 {
507   for(uint i = 0; i < tbl->cols_to_output; i++) {
508     if(i) {
509       bputs(tbl->out, tbl->col_delimiter);
510     }
511     bputs(tbl->out, tbl->column_order[i].cell_content);
512   }
513   bputc(tbl->out, '\n');
514 }
515
516 static void table_start_machine_readable(struct table *tbl)
517 {
518   if(tbl->col_delimiter == NULL) {
519     tbl->col_delimiter = "\t";
520   }
521
522   if(tbl->print_header != false && tbl->cols_to_output > 0) {
523     bputs(tbl->out, tbl->column_order[0].col_def->name);
524     for(uint i = 1; i < tbl->cols_to_output; i++) {
525       bputs(tbl->out, tbl->col_delimiter);
526       bputs(tbl->out, tbl->column_order[i].col_def->name);
527     }
528     bputc(tbl->out, '\n');
529   }
530 }
531
532 const struct table_formatter table_fmt_machine_readable = {
533   .row_output = table_row_machine_readable,
534   .table_start = table_start_machine_readable,
535 };
536
537
538 /*** Blockline formatter ***/
539
540 static void table_row_blockline_output(struct table *tbl)
541 {
542   for(uint i = 0; i < tbl->cols_to_output; i++) {
543     const struct table_column *col_def = tbl->column_order[i].col_def;
544     bprintf(tbl->out, "%s: %s\n", col_def->name, tbl->column_order[i].cell_content);
545   }
546   bputc(tbl->out, '\n');
547 }
548
549 static void table_start_blockline(struct table *tbl)
550 {
551   if(tbl->col_delimiter == NULL) {
552     tbl->col_delimiter = "\n";
553   }
554 }
555
556 const struct table_formatter table_fmt_blockline = {
557   .row_output = table_row_blockline_output,
558   .table_start = table_start_blockline
559 };
560
561 /*** Tests ***/
562
563 #ifdef TEST
564
565 #include <stdio.h>
566
567 enum test_table_cols {
568   TEST_COL0_STR, TEST_COL1_INT, TEST_COL2_UINT, TEST_COL3_BOOL, TEST_COL4_DOUBLE
569 };
570
571 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) };
572
573 static struct table_template test_tbl = {
574   TBL_COLUMNS {
575     [TEST_COL0_STR] = TBL_COL_STR("col0_str", 20),
576     [TEST_COL1_INT] = TBL_COL_INT("col1_int", 8),
577     [TEST_COL2_UINT] = TBL_COL_UINT("col2_uint", 9),
578     [TEST_COL3_BOOL] = TBL_COL_BOOL_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
579     [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE("col4_double", 11),
580     TBL_COL_END
581   },
582   TBL_COL_ORDER(test_column_order),
583   TBL_FMT_HUMAN_READABLE,
584   TBL_COL_DELIMITER("\t"),
585 };
586
587 /**
588  * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
589  **/
590 static void do_print1(struct table *test_tbl)
591 {
592   table_col_str(test_tbl, TEST_COL0_STR, "sdsdf");
593   table_col_int(test_tbl, TEST_COL1_INT, -10);
594   table_col_int(test_tbl, TEST_COL1_INT, 10000);
595   table_col_uint(test_tbl, TEST_COL2_UINT, 10);
596   table_col_printf(test_tbl, TEST_COL2_UINT, "XXX-%u", 22222);
597   table_col_bool(test_tbl, TEST_COL3_BOOL, true);
598   table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
599   table_col_printf(test_tbl, TEST_COL4_DOUBLE, "AAA");
600   table_end_row(test_tbl);
601
602   table_col_str(test_tbl, TEST_COL0_STR, "test");
603   table_col_int(test_tbl, TEST_COL1_INT, -100);
604   table_col_uint(test_tbl, TEST_COL2_UINT, 100);
605   table_col_bool(test_tbl, TEST_COL3_BOOL, false);
606   table_col_printf(test_tbl, TEST_COL4_DOUBLE, "%.2lf", 1.5);
607   table_end_row(test_tbl);
608 }
609
610 static void test_simple1(struct fastbuf *out)
611 {
612   struct table *tbl = table_init(&test_tbl);
613
614   // print table with header
615   table_set_col_order_by_name(tbl, "col3_bool");
616   table_start(tbl, out);
617   do_print1(tbl);
618   table_end(tbl);
619
620   // print the same table as in the previous case without header
621   table_set_col_order_by_name(tbl, "col0_str,col2_uint,col1_int,col3_bool");
622   table_start(tbl, out);
623   do_print1(tbl);
624   table_end(tbl);
625
626   // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
627   tbl->print_header = false;
628   table_start(tbl, out);
629   do_print1(tbl);
630   table_end(tbl);
631   tbl->print_header = true;
632
633   table_set_col_order_by_name(tbl, "col3_bool");
634   table_start(tbl, out);
635   do_print1(tbl);
636   table_end(tbl);
637
638   table_set_col_order_by_name(tbl, "col3_bool,col0_str");
639   table_start(tbl, out);
640   do_print1(tbl);
641   table_end(tbl);
642
643   table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint");
644   table_start(tbl, out);
645   do_print1(tbl);
646   table_end(tbl);
647
648   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");
649   table_start(tbl, out);
650   do_print1(tbl);
651   table_end(tbl);
652
653   table_set_col_order_by_name(tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
654   table_start(tbl, out);
655   do_print1(tbl);
656   table_end(tbl);
657
658   table_cleanup(tbl);
659 }
660
661 enum test_any_table_cols {
662   TEST_ANY_COL0_INT, TEST_ANY_COL1_ANY
663 };
664
665 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) };
666
667 static struct table_template test_any_tbl = {
668   TBL_COLUMNS {
669     [TEST_ANY_COL0_INT] = TBL_COL_INT("col0_int", 8),
670     [TEST_ANY_COL1_ANY] = TBL_COL_ANY_FMT("col1_any", 9, XTYPE_FMT_PRETTY),
671     TBL_COL_END
672   },
673   TBL_COL_ORDER(test_any_column_order),
674   TBL_FMT_HUMAN_READABLE,
675   TBL_COL_DELIMITER("\t"),
676 };
677
678 static void test_any_type(struct fastbuf *out)
679 {
680   struct table *tbl = table_init(&test_any_tbl);
681
682   table_start(tbl, out);
683
684   table_col_int(tbl, TEST_ANY_COL0_INT, -10);
685   table_col_int(tbl, TEST_ANY_COL1_ANY, 10000);
686   table_end_row(tbl);
687
688   table_col_int(tbl, TEST_ANY_COL0_INT, -10);
689   table_col_double(tbl, TEST_ANY_COL1_ANY, 1.4);
690   table_end_row(tbl);
691
692   table_col_printf(tbl, TEST_ANY_COL0_INT, "%d", 10);
693   table_col_double(tbl, TEST_ANY_COL1_ANY, 1.4);
694   table_end_row(tbl);
695
696   table_end(tbl);
697   table_cleanup(tbl);
698 }
699
700 int main(int argc UNUSED, char **argv UNUSED)
701 {
702   struct fastbuf *out;
703   out = bfdopen_shared(1, 4096);
704
705   test_simple1(out);
706
707   test_any_type(out);
708
709   bclose(out);
710   return 0;
711 }
712
713 #endif