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