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