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