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