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