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