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