]> mj.ucw.cz Git - libucw.git/blob - ucw/table.c
tableprinter: updated setting of column option
[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 const char *table_set_col_opt(struct table *tbl, uint col_idx, const char *col_opt)
219 {
220   const struct table_column *col_def = tbl->column_order[col_idx].col_def;
221   if(col_def && col_def->set_col_opt && col_def->set_col_opt != table_set_col_opt) {
222     return col_def->set_col_opt(tbl, col_idx, col_opt);
223   }
224
225   if(col_def && col_def->type_def && col_def->type_def->parse_fmt) {
226     uint fmt = 0;
227     const char *tmp_err = col_def->type_def->parse_fmt(col_opt, &fmt, tbl->pool);
228     if(tmp_err) return tmp_err;
229     tbl->column_order[col_idx].fmt = fmt;
230     return NULL;
231   }
232
233   return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_idx);
234 }
235
236 /**
237  * TODO: This function deliberately leaks memory. When it is called multiple times,
238  * previous column orders still remain allocated in the table's memory pool.
239  **/
240 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
241 {
242   if(col_order_str[0] == '*') {
243     table_make_default_column_order(tbl);
244     return NULL;
245   }
246
247   if(!col_order_str[0]) {
248     tbl->column_order = mp_alloc(tbl->pool, 0);
249     tbl->cols_to_output = 0;
250     return NULL;
251   }
252
253   char *tmp_col_order = stk_strdup(col_order_str);
254
255   int col_count = 1;
256   for(int i = 0; col_order_str[i] != 0; i++) {
257     if(col_order_str[i] == ',') {
258       col_count++;
259     }
260   }
261
262   tbl->cols_to_output = col_count;
263   tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * col_count);
264
265   int curr_col_idx = 0;
266   char *name_start = tmp_col_order;
267   while(name_start) {
268     char *next = strchr(name_start, ',');
269     if(next) {
270       *next++ = 0;
271     }
272
273     char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
274     int col_idx = table_get_col_idx(tbl, name_start);
275
276     if(col_idx == -1) {
277       return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
278     }
279     tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
280     tbl->column_order[curr_col_idx].idx = col_idx;
281     tbl->column_order[curr_col_idx].fmt = tbl->columns[col_idx].fmt;
282     if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_opt) {
283       const char *err = NULL;
284       err = tbl->columns[col_idx].set_col_opt(tbl, curr_col_idx, arg);
285       if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
286     }
287
288     name_start = next;
289     curr_col_idx++;
290   }
291
292   table_update_ll(tbl);
293
294   return NULL;
295 }
296
297 /*** Table cells ***/
298
299 static void table_set_all_inst_content(struct table *tbl, int col_templ, const char *col_content)
300 {
301   TBL_COL_ITER_START(tbl, col_templ, curr_col_ptr, curr_col) {
302     curr_col_ptr->cell_content = col_content;
303   } TBL_COL_ITER_END
304 }
305
306 void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type)
307 {
308   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
309   ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == tbl->columns[col].type_def);
310   tbl->last_printed_col = col;
311   tbl->row_printing_started = 1;
312   TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
313     enum xtype_fmt fmt = curr_col->fmt;
314     curr_col->cell_content = expected_type->format(value, fmt, tbl->pool);
315   } TBL_COL_ITER_END
316 }
317
318 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
319 {
320   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
321   tbl->last_printed_col = col;
322   tbl->row_printing_started = 1;
323   va_list args;
324   va_start(args, fmt);
325   char *cell_content = mp_vprintf(tbl->pool, fmt, args);
326   table_set_all_inst_content(tbl, col, cell_content);
327   va_end(args);
328 }
329
330 TABLE_COL_BODY(int, int)
331 TABLE_COL_BODY(uint, uint)
332 TABLE_COL_BODY(double, double)
333 TABLE_COL_BODY(intmax, intmax_t)
334 TABLE_COL_BODY(uintmax, uintmax_t)
335 TABLE_COL_BODY(s64, s64)
336 TABLE_COL_BODY(u64, u64)
337 TABLE_COL_BODY(bool, bool)
338 TABLE_COL_BODY(str, const char *)
339
340 void table_reset_row(struct table *tbl)
341 {
342   for(uint i = 0; i < tbl->cols_to_output; i++) {
343     tbl->column_order[i].cell_content = NULL;
344   }
345   mp_restore(tbl->pool, &tbl->pool_state);
346   tbl->last_printed_col = -1;
347   tbl->row_printing_started = 0;
348 }
349
350 void table_end_row(struct table *tbl)
351 {
352   ASSERT(tbl->formatter->row_output);
353   if(tbl->row_printing_started == 0) return;
354   tbl->formatter->row_output(tbl);
355   table_reset_row(tbl);
356 }
357
358 /* Construction of a cell using a fastbuf */
359
360 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
361 {
362   fbpool_init(&tbl->fb_col_out);
363   fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
364   tbl->col_out = col;
365   return &tbl->fb_col_out.fb;
366 }
367
368 void table_col_fbend(struct table *tbl)
369 {
370   char *cell_content = fbpool_end(&tbl->fb_col_out);
371   table_set_all_inst_content(tbl, tbl->col_out, cell_content);
372   tbl->col_out = -1;
373 }
374
375 /*** Option parsing ***/
376
377 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
378 {
379   // Options with no value
380   if(value == NULL || (value != NULL && strlen(value) == 0)) {
381     if(strcmp(key, "noheader") == 0) {
382       tbl->print_header = 0;
383       return NULL;
384     }
385   }
386
387   // Options with a value
388   if(value) {
389     if(strcmp(key, "header") == 0) {
390       if(value[1] != 0)
391         return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
392       uint tmp = value[0] - '0';
393       if(tmp > 1)
394         return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
395       tbl->print_header = tmp;
396       return NULL;
397     } else if(strcmp(key, "cols") == 0) {
398       return table_set_col_order_by_name(tbl, value);
399     } else if(strcmp(key, "fmt") == 0) {
400       if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
401       else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
402       else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
403       else {
404         return "Invalid argument to output-type option.";
405       }
406       return NULL;
407     } else if(strcmp(key, "cells") == 0) {
408       u32 fmt = 0;
409       const char *err = xtype_parse_fmt(NULL, value, &fmt, tbl->pool);
410       if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
411       for(uint i = 0; i < tbl->cols_to_output; i++) {
412         tbl->column_order[i].fmt = fmt;
413       }
414       return NULL;
415     } else if(strcmp(key, "raw") == 0 || strcmp(key, "pretty") == 0) {
416       u32 fmt = 0;
417       const char *err = xtype_parse_fmt(NULL, key, &fmt, tbl->pool);
418       if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
419       for(uint i = 0; i < tbl->cols_to_output; i++) {
420         tbl->column_order[i].fmt = fmt;
421       }
422       return NULL;
423     } else if(strcmp(key, "col-delim") == 0) {
424       char * d = mp_printf(tbl->pool, "%s", value);
425       tbl->col_delimiter = d;
426       return NULL;
427     }
428   }
429
430   // Formatter options
431   if(tbl->formatter && tbl->formatter->process_option) {
432     const char *err = NULL;
433     if(tbl->formatter->process_option(tbl, key, value, &err)) {
434       return err;
435     }
436   }
437
438   // Unrecognized option
439   return mp_printf(tbl->pool, "Invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
440 }
441
442 const char *table_set_option(struct table *tbl, const char *opt)
443 {
444   char *key = stk_strdup(opt);
445   char *value = strchr(key, ':');
446   if(value) {
447     *value++ = 0;
448   }
449   return table_set_option_value(tbl, key, value);
450 }
451
452 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
453 {
454   for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
455     const char *rv = table_set_option(tbl, gary_table_opts[i]);
456     if(rv != NULL) {
457       return rv;
458     }
459   }
460   return NULL;
461 }
462
463 /*** Default formatter for human-readable output ***/
464
465 static void table_row_human_readable(struct table *tbl)
466 {
467   for(uint i = 0; i < tbl->cols_to_output; i++) {
468     const struct table_column *col_def = tbl->column_order[i].col_def;
469     if(i) {
470       bputs(tbl->out, tbl->col_delimiter);
471     }
472     int col_width = col_def->width & CELL_WIDTH_MASK;
473     if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
474     bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
475   }
476   bputc(tbl->out, '\n');
477 }
478
479 static void table_write_header(struct table *tbl)
480 {
481   for(uint i = 0; i < tbl->cols_to_output; i++) {
482     const struct table_column *col_def = tbl->column_order[i].col_def;
483     if(i) {
484       bputs(tbl->out, tbl->col_delimiter);
485     }
486     int col_width = col_def->width & CELL_WIDTH_MASK;
487     if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
488     bprintf(tbl->out, "%*s", col_width, col_def->name);
489   }
490   bputc(tbl->out, '\n');
491 }
492
493 static void table_start_human_readable(struct table *tbl)
494 {
495   if(tbl->col_delimiter == NULL) {
496     tbl->col_delimiter = " ";
497   }
498
499   if(tbl->print_header != 0) {
500     table_write_header(tbl);
501   }
502 }
503
504 const struct table_formatter table_fmt_human_readable = {
505   .row_output = table_row_human_readable,
506   .table_start = table_start_human_readable,
507 };
508
509 /*** Default formatter for machine-readable output ***/
510
511 static void table_row_machine_readable(struct table *tbl)
512 {
513   for(uint i = 0; i < tbl->cols_to_output; i++) {
514     if(i) {
515       bputs(tbl->out, tbl->col_delimiter);
516     }
517     bputs(tbl->out, tbl->column_order[i].cell_content);
518   }
519   bputc(tbl->out, '\n');
520 }
521
522 static void table_start_machine_readable(struct table *tbl)
523 {
524   if(tbl->col_delimiter == NULL) {
525     tbl->col_delimiter = "\t";
526   }
527
528   if(tbl->print_header != 0 && tbl->cols_to_output > 0) {
529     bputs(tbl->out, tbl->column_order[0].col_def->name);
530     for(uint i = 1; i < tbl->cols_to_output; i++) {
531       bputs(tbl->out, tbl->col_delimiter);
532       bputs(tbl->out, tbl->column_order[i].col_def->name);
533     }
534     bputc(tbl->out, '\n');
535   }
536 }
537
538 const struct table_formatter table_fmt_machine_readable = {
539   .row_output = table_row_machine_readable,
540   .table_start = table_start_machine_readable,
541 };
542
543
544 /*** Blockline formatter ***/
545
546 static void table_row_blockline_output(struct table *tbl)
547 {
548   for(uint i = 0; i < tbl->cols_to_output; i++) {
549     const struct table_column *col_def = tbl->column_order[i].col_def;
550     bprintf(tbl->out, "%s: %s\n", col_def->name, tbl->column_order[i].cell_content);
551   }
552   bputc(tbl->out, '\n');
553 }
554
555 static void table_start_blockline(struct table *tbl)
556 {
557   if(tbl->col_delimiter == NULL) {
558     tbl->col_delimiter = "\n";
559   }
560 }
561
562 const struct table_formatter table_fmt_blockline = {
563   .row_output = table_row_blockline_output,
564   .table_start = table_start_blockline
565 };
566
567 /*** Tests ***/
568
569 #ifdef TEST
570
571 #include <stdio.h>
572
573 enum test_table_cols {
574   TEST_COL0_STR, TEST_COL1_INT, TEST_COL2_UINT, TEST_COL3_BOOL, TEST_COL4_DOUBLE
575 };
576
577 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) };
578
579 static struct table_template test_tbl = {
580   TBL_COLUMNS {
581     [TEST_COL0_STR] = TBL_COL_STR("col0_str", 20),
582     [TEST_COL1_INT] = TBL_COL_INT("col1_int", 8),
583     [TEST_COL2_UINT] = TBL_COL_UINT("col2_uint", 9),
584     [TEST_COL3_BOOL] = TBL_COL_BOOL_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
585     [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE("col4_double", 11),
586     TBL_COL_END
587   },
588   TBL_COL_ORDER(test_column_order),
589   TBL_FMT_HUMAN_READABLE,
590   TBL_COL_DELIMITER("\t"),
591 };
592
593 /**
594  * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
595  **/
596 static void do_print1(struct table *test_tbl)
597 {
598   table_col_str(test_tbl, TEST_COL0_STR, "sdsdf");
599   table_col_int(test_tbl, TEST_COL1_INT, -10);
600   table_col_int(test_tbl, TEST_COL1_INT, 10000);
601   table_col_uint(test_tbl, TEST_COL2_UINT, 10);
602   table_col_printf(test_tbl, TEST_COL2_UINT, "XXX-%u", 22222);
603   table_col_bool(test_tbl, TEST_COL3_BOOL, true);
604   table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
605   table_col_printf(test_tbl, TEST_COL4_DOUBLE, "AAA");
606   table_end_row(test_tbl);
607
608   table_col_str(test_tbl, TEST_COL0_STR, "test");
609   table_col_int(test_tbl, TEST_COL1_INT, -100);
610   table_col_uint(test_tbl, TEST_COL2_UINT, 100);
611   table_col_bool(test_tbl, TEST_COL3_BOOL, false);
612   table_col_printf(test_tbl, TEST_COL4_DOUBLE, "%.2lf", 1.5);
613   table_end_row(test_tbl);
614 }
615
616 static void test_simple1(struct fastbuf *out)
617 {
618   struct table *tbl = table_init(&test_tbl);
619
620   // print table with header
621   table_set_col_order_by_name(tbl, "col3_bool");
622   table_start(tbl, out);
623   do_print1(tbl);
624   table_end(tbl);
625
626   // print the same table as in the previous case without header
627   table_set_col_order_by_name(tbl, "col0_str,col2_uint,col1_int,col3_bool");
628   table_start(tbl, out);
629   do_print1(tbl);
630   table_end(tbl);
631
632   // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
633   tbl->print_header = 0;
634   table_start(tbl, out);
635   do_print1(tbl);
636   table_end(tbl);
637   tbl->print_header = 1;
638
639   table_set_col_order_by_name(tbl, "col3_bool");
640   table_start(tbl, out);
641   do_print1(tbl);
642   table_end(tbl);
643
644   table_set_col_order_by_name(tbl, "col3_bool,col0_str");
645   table_start(tbl, out);
646   do_print1(tbl);
647   table_end(tbl);
648
649   table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint");
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,col0_str,col3_bool,col2_uint,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,col1_int,col2_uint,col3_bool,col4_double");
660   table_start(tbl, out);
661   do_print1(tbl);
662   table_end(tbl);
663
664   table_cleanup(tbl);
665 }
666
667 enum test_any_table_cols {
668   TEST_ANY_COL0_INT, TEST_ANY_COL1_ANY
669 };
670
671 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) };
672
673 static struct table_template test_any_tbl = {
674   TBL_COLUMNS {
675     [TEST_ANY_COL0_INT] = TBL_COL_INT("col0_int", 8),
676     [TEST_ANY_COL1_ANY] = TBL_COL_ANY_FMT("col1_any", 9, XTYPE_FMT_PRETTY),
677     TBL_COL_END
678   },
679   TBL_COL_ORDER(test_any_column_order),
680   TBL_FMT_HUMAN_READABLE,
681   TBL_COL_DELIMITER("\t"),
682 };
683
684 static void test_any_type(struct fastbuf *out)
685 {
686   struct table *tbl = table_init(&test_any_tbl);
687
688   table_start(tbl, out);
689
690   table_col_int(tbl, TEST_ANY_COL0_INT, -10);
691   table_col_int(tbl, TEST_ANY_COL1_ANY, 10000);
692   table_end_row(tbl);
693
694   table_col_int(tbl, TEST_ANY_COL0_INT, -10);
695   table_col_double(tbl, TEST_ANY_COL1_ANY, 1.4);
696   table_end_row(tbl);
697
698   table_col_printf(tbl, TEST_ANY_COL0_INT, "%d", 10);
699   table_col_double(tbl, TEST_ANY_COL1_ANY, 1.4);
700   table_end_row(tbl);
701
702   table_end(tbl);
703   table_cleanup(tbl);
704 }
705
706 int main(int argc UNUSED, char **argv UNUSED)
707 {
708   struct fastbuf *out;
709   out = bfdopen_shared(1, 4096);
710
711   test_simple1(out);
712
713   test_any_type(out);
714
715   bclose(out);
716   return 0;
717 }
718
719 #endif