]> mj.ucw.cz Git - libucw.git/blob - ucw/table.c
Docs: Polish table printer documentation
[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 /* Construction of a cell using a fastbuf */
325
326 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
327 {
328   fbpool_init(&tbl->fb_col_out);
329   fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
330   tbl->col_out = col;
331   return &tbl->fb_col_out.fb;
332 }
333
334 void table_col_fbend(struct table *tbl)
335 {
336   tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
337   tbl->col_out = -1;
338 }
339
340 /*** Option parsing ***/
341
342 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
343 {
344   // Options with no value
345   if(value == NULL || (value != NULL && strlen(value) == 0)) {
346     if(strcmp(key, "noheader") == 0) {
347       tbl->print_header = 0;
348       return NULL;
349     }
350   }
351
352   // Options with a value
353   if(value) {
354     if(strcmp(key, "header") == 0) {
355       if(value[1] != 0)
356         return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
357       uint tmp = value[0] - '0';
358       if(tmp > 1)
359         return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
360       tbl->print_header = tmp;
361       return NULL;
362     } else if(strcmp(key, "cols") == 0) {
363       const char *err = table_set_col_order_by_name(tbl, value);
364       if(err != NULL) {
365         return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
366       }
367       return NULL;
368     } else if(strcmp(key, "fmt") == 0) {
369       if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
370       else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
371       else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
372       else {
373         return "Tableprinter: invalid argument to output-type option.";
374       }
375       return NULL;
376     } else if(strcmp(key, "col-delim") == 0) {
377       char * d = mp_printf(tbl->pool, "%s", value);
378       tbl->col_delimiter = d;
379       return NULL;
380     }
381   }
382
383   // Formatter options
384   if(tbl->formatter && tbl->formatter->process_option) {
385     const char *err = NULL;
386     if (tbl->formatter->process_option(tbl, key, value, &err)) {
387       return err;
388     }
389   }
390
391   // Unrecognized option
392   return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
393 }
394
395 const char *table_set_option(struct table *tbl, const char *opt)
396 {
397   char *key = stk_strdup(opt);
398   char *value = strchr(key, ':');
399   if(value) {
400     *value++ = 0;
401   }
402   return table_set_option_value(tbl, key, value);
403 }
404
405 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
406 {
407   for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
408     const char *rv = table_set_option(tbl, gary_table_opts[i]);
409     if(rv != NULL) {
410       return rv;
411     }
412   }
413   return NULL;
414 }
415
416 /*** Default formatter for human-readable output ***/
417
418 static void table_row_human_readable(struct table *tbl)
419 {
420   for(uint i = 0; i < tbl->cols_to_output; i++) {
421     int col_idx = tbl->column_order[i];
422     if(i) {
423       bputs(tbl->out, tbl->col_delimiter);
424     }
425     int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
426     if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
427     bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
428   }
429   bputc(tbl->out, '\n');
430 }
431
432 static void table_write_header(struct table *tbl)
433 {
434   for(uint i = 0; i < tbl->cols_to_output; i++) {
435     int col_idx = tbl->column_order[i];
436     if(i) {
437       bputs(tbl->out, tbl->col_delimiter);
438     }
439     int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
440     if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
441     bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
442   }
443   bputc(tbl->out, '\n');
444 }
445
446 static void table_start_human_readable(struct table *tbl)
447 {
448   if(tbl->col_delimiter == NULL) {
449     tbl->col_delimiter = " ";
450   }
451
452   if(tbl->append_delimiter == NULL) {
453     tbl->append_delimiter = ",";
454   }
455
456   if(tbl->print_header != 0) {
457     table_write_header(tbl);
458   }
459 }
460
461 struct table_formatter table_fmt_human_readable = {
462   .row_output = table_row_human_readable,
463   .table_start = table_start_human_readable,
464 };
465
466 /*** Default formatter for machine-readable output ***/
467
468 static void table_row_machine_readable(struct table *tbl)
469 {
470   for(uint i = 0; i < tbl->cols_to_output; i++) {
471     int col_idx = tbl->column_order[i];
472     if(i) {
473       bputs(tbl->out, tbl->col_delimiter);
474     }
475     bputs(tbl->out, tbl->col_str_ptrs[col_idx]);
476   }
477   bputc(tbl->out, '\n');
478 }
479
480 static void table_start_machine_readable(struct table *tbl)
481 {
482   if(tbl->col_delimiter == NULL) {
483     tbl->col_delimiter = ";";
484   }
485
486   if(tbl->append_delimiter == NULL) {
487     tbl->append_delimiter = ",";
488   }
489
490   if(tbl->print_header != 0) {
491     uint col_idx = tbl->column_order[0];
492     bputs(tbl->out, tbl->columns[col_idx].name);
493     for(uint i = 1; i < tbl->cols_to_output; i++) {
494       col_idx = tbl->column_order[i];
495       bputs(tbl->out, tbl->col_delimiter);
496       bputs(tbl->out, tbl->columns[col_idx].name);
497     }
498     bputc(tbl->out, '\n');
499   }
500 }
501
502 struct table_formatter table_fmt_machine_readable = {
503   .row_output = table_row_machine_readable,
504   .table_start = table_start_machine_readable,
505 };
506
507
508 /*** Blockline formatter ***/
509
510 static void table_row_blockline_output(struct table *tbl)
511 {
512   for(uint i = 0; i < tbl->cols_to_output; i++) {
513     int col_idx = tbl->column_order[i];
514     bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->col_str_ptrs[col_idx]);
515   }
516   bputc(tbl->out, '\n');
517 }
518
519 static void table_start_blockline(struct table *tbl)
520 {
521   if(tbl->col_delimiter == NULL) {
522     tbl->col_delimiter = " ";
523   }
524
525   if(tbl->append_delimiter == NULL) {
526     tbl->append_delimiter = ",";
527   }
528 }
529
530 struct table_formatter table_fmt_blockline = {
531   .row_output = table_row_blockline_output,
532   .table_start = table_start_blockline
533 };
534
535
536
537 /*** Tests ***/
538
539 #ifdef TEST
540
541 #include <stdio.h>
542
543 enum test_table_cols {
544   test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
545 };
546
547 static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str};
548
549 static struct table test_tbl = {
550   TBL_COLUMNS {
551     [test_col0_str] = TBL_COL_STR("col0_str", 20),
552     [test_col1_int] = TBL_COL_INT("col1_int", 8),
553     [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
554     [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
555     [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
556     TBL_COL_END
557   },
558   TBL_COL_ORDER(test_column_order),
559   TBL_OUTPUT_HUMAN_READABLE,
560   TBL_COL_DELIMITER("\t"),
561   TBL_APPEND_DELIMITER(",")
562 };
563
564 /**
565  * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
566  **/
567 static void do_print1(struct table *test_tbl)
568 {
569   table_col_str(test_tbl, test_col0_str, "sdsdf");
570   table_append_str(test_tbl, "aaaaa");
571   table_col_int(test_tbl, test_col1_int, -10);
572   table_col_int(test_tbl, test_col1_int, 10000);
573   table_col_uint(test_tbl, test_col2_uint, 10);
574   table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
575   table_col_bool(test_tbl, test_col3_bool, 1);
576   table_col_double(test_tbl, test_col4_double, 1.5);
577   table_col_printf(test_tbl, test_col4_double, "AAA");
578   table_end_row(test_tbl);
579
580   table_col_str(test_tbl, test_col0_str, "test");
581   table_append_str(test_tbl, "bbbbb");
582   table_col_int(test_tbl, test_col1_int, -100);
583   table_col_uint(test_tbl, test_col2_uint, 100);
584   table_col_bool(test_tbl, test_col3_bool, 0);
585   table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
586   table_end_row(test_tbl);
587 }
588
589 static void test_simple1(struct fastbuf *out)
590 {
591   table_init(&test_tbl);
592
593   // print table with header
594   table_set_col_order_by_name(&test_tbl, "col3_bool");
595   table_start(&test_tbl, out);
596   do_print1(&test_tbl);
597   table_end(&test_tbl);
598
599   // print the same table as in the previous case without header
600   table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
601   table_start(&test_tbl, out);
602   do_print1(&test_tbl);
603   table_end(&test_tbl);
604
605   // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
606   test_tbl.print_header = 0;
607   table_start(&test_tbl, out);
608   do_print1(&test_tbl);
609   table_end(&test_tbl);
610   test_tbl.print_header = 1;
611
612   table_set_col_order_by_name(&test_tbl, "col3_bool");
613   table_start(&test_tbl, out);
614   do_print1(&test_tbl);
615   table_end(&test_tbl);
616
617   table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
618   table_start(&test_tbl, out);
619   do_print1(&test_tbl);
620   table_end(&test_tbl);
621
622   table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
623   table_start(&test_tbl, out);
624   do_print1(&test_tbl);
625   table_end(&test_tbl);
626
627   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");
628   table_start(&test_tbl, out);
629   do_print1(&test_tbl);
630   table_end(&test_tbl);
631
632   table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
633   table_start(&test_tbl, out);
634   do_print1(&test_tbl);
635   table_end(&test_tbl);
636
637   table_cleanup(&test_tbl);
638 }
639
640 enum test_any_table_cols {
641   test_any_col0_int, test_any_col1_any
642 };
643
644 static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
645
646 static struct table test_any_tbl = {
647   TBL_COLUMNS {
648     [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
649     [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
650     TBL_COL_END
651   },
652   TBL_COL_ORDER(test_any_column_order),
653   TBL_OUTPUT_HUMAN_READABLE,
654   TBL_COL_DELIMITER("\t"),
655   TBL_APPEND_DELIMITER(",")
656 };
657
658 static void test_any_type(struct fastbuf *out)
659 {
660   table_init(&test_any_tbl);
661
662   table_start(&test_any_tbl, out);
663
664   table_col_int(&test_any_tbl, test_any_col0_int, -10);
665   table_col_int(&test_any_tbl, test_any_col1_any, 10000);
666   table_end_row(&test_any_tbl);
667
668   table_col_int(&test_any_tbl, test_any_col0_int, -10);
669   table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
670   table_end_row(&test_any_tbl);
671
672   table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
673   table_append_printf(&test_any_tbl, "%d", 20);
674   table_append_printf(&test_any_tbl, "%d", 30);
675   table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
676   table_append_printf(&test_any_tbl, "%.2lf", 1.5);
677   table_append_printf(&test_any_tbl, "%.2lf", 1.6);
678   table_end_row(&test_any_tbl);
679
680   table_end(&test_any_tbl);
681   table_cleanup(&test_any_tbl);
682 }
683
684 int main(int argc UNUSED, char **argv UNUSED)
685 {
686   struct fastbuf *out;
687   out = bfdopen_shared(1, 4096);
688
689   test_simple1(out);
690
691   test_any_type(out);
692
693   bclose(out);
694   return 0;
695 }
696
697 #endif