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