]> mj.ucw.cz Git - libucw.git/blob - ucw/table.c
Tableprinter: corrected INT64 -> S64
[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, 0);
138     tbl->cols_to_output = 0;
139     return NULL;
140   }
141
142   char *tmp_col_order = stk_strdup(col_order_str);
143
144   int col_count = 1;
145   for(int i = 0; col_order_str[i] != 0; i++) {
146     if(col_order_str[i] == ',') {
147       col_count++;
148     }
149   }
150
151   int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * col_count);
152   int curr_col_order_int = 0;
153   const char *name_start = tmp_col_order;
154   while(name_start) {
155     char *next = strchr(name_start, ',');
156     if(next) {
157       *next++ = 0;
158     }
159
160     int idx = table_get_col_idx(tbl, name_start);
161     if(idx == -1) {
162       return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
163     }
164     col_order_int[curr_col_order_int++] = idx;
165
166     name_start = next;
167   }
168
169   tbl->column_order = col_order_int;
170   tbl->cols_to_output = curr_col_order_int;
171   return NULL;
172 }
173
174 /*** Table cells ***/
175
176 void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
177 {
178   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
179   tbl->last_printed_col = col;
180   tbl->row_printing_started = 1;
181   va_list args;
182   va_start(args, fmt);
183   tbl->col_str_ptrs[col] = mp_vprintf(tbl->pool, fmt, args);
184   va_end(args);
185 }
186
187 static const char *table_col_default_fmts[] = {
188   [COL_TYPE_STR] = "%s",
189   [COL_TYPE_INT] = "%d",
190   [COL_TYPE_INTMAX] = "%jd",
191   [COL_TYPE_UINT] = "%u",
192   [COL_TYPE_UINTMAX] = "%ju",
193   [COL_TYPE_BOOL] = "%d",
194   [COL_TYPE_DOUBLE] = "%.2lf",
195   [COL_TYPE_ANY] = NULL,
196   [COL_TYPE_LAST] = NULL
197 };
198
199 #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val) \
200   {\
201     const char *fmt = tbl->columns[col].fmt;\
202     if(tbl->columns[col].type == COL_TYPE_ANY) {\
203        fmt = table_col_default_fmts[_typeconst_];\
204     }\
205     table_col_##_name_##_fmt(tbl, col, fmt, val);\
206   }
207
208 #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val) \
209   {\
210     int col = table_get_col_idx(tbl, col_name);\
211     table_col_##_name_(tbl, col, val);\
212   }
213
214 #define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
215   {\
216      ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
217      ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
218      ASSERT(fmt != NULL);\
219      tbl->last_printed_col = col;\
220      tbl->row_printing_started = 1;\
221      tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val);\
222   }
223
224 #define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_);\
225   TABLE_COL_STR(_name_, _type_, _typeconst_);\
226   TABLE_COL_FMT(_name_, _type_, _typeconst_);
227
228 TABLE_COL_BODIES(int, int, COL_TYPE_INT)
229 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
230 TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
231 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
232 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
233 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
234 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
235 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
236 #undef TABLE_COL
237 #undef TABLE_COL_FMT
238 #undef TABLE_COL_STR
239 #undef TABLE_COL_BODIES
240
241 void table_col_bool(struct table *tbl, int col, uint val)
242 {
243   table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
244 }
245
246 void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
247 {
248   int col = table_get_col_idx(tbl, col_name);
249   table_col_bool(tbl, col, val);
250 }
251
252 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
253 {
254   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
255   ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
256
257   tbl->last_printed_col = col;
258   tbl->row_printing_started = 1;
259   tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val ? "true" : "false");
260 }
261
262 #define TABLE_APPEND(_name_, _type_, _typeconst_) void table_append_##_name_(struct table *tbl, _type_ val) \
263   {\
264      ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);\
265      ASSERT(_typeconst_ == tbl->columns[tbl->last_printed_col].type);\
266      int col = tbl->last_printed_col;\
267      mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);\
268      tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val);\
269   }
270
271 TABLE_APPEND(int, int, COL_TYPE_INT)
272 TABLE_APPEND(uint, uint, COL_TYPE_UINT)
273 TABLE_APPEND(double, double, COL_TYPE_DOUBLE)
274 TABLE_APPEND(str, const char *, COL_TYPE_STR)
275 TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
276 TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
277 TABLE_APPEND(u64, u64, COL_TYPE_U64)
278 #undef TABLE_APPEND
279
280 void table_append_bool(struct table *tbl, int val)
281 {
282   ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
283   ASSERT(COL_TYPE_BOOL == tbl->columns[tbl->last_printed_col].type);
284
285   int col = tbl->last_printed_col;
286
287   mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
288
289   tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val ? "true" : "false");
290 }
291
292 void table_append_printf(struct table *tbl, const char *fmt, ...)
293 {
294   ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
295   int col = tbl->last_printed_col;
296
297   va_list args;
298   va_start(args, fmt);
299
300   mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
301   tbl->col_str_ptrs[col] = mp_vprintf_append(tbl->pool, tbl->col_str_ptrs[col], fmt, args);
302
303   va_end(args);
304 }
305
306 void table_end_row(struct table *tbl)
307 {
308   ASSERT(tbl->formatter->row_output);
309   tbl->formatter->row_output(tbl);
310   memset(tbl->col_str_ptrs, 0, sizeof(char *) * tbl->column_count);
311   mp_restore(tbl->pool, &tbl->pool_state);
312   tbl->last_printed_col = -1;
313   tbl->row_printing_started = 0;
314 }
315
316 /* Construction of a cell using a fastbuf */
317
318 struct fastbuf *table_col_fbstart(struct table *tbl, int col)
319 {
320   fbpool_init(&tbl->fb_col_out);
321   fbpool_start(&tbl->fb_col_out, tbl->pool, 1);
322   tbl->col_out = col;
323   return &tbl->fb_col_out.fb;
324 }
325
326 void table_col_fbend(struct table *tbl)
327 {
328   tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
329   tbl->col_out = -1;
330 }
331
332 /*** Option parsing ***/
333
334 const char *table_set_option_value(struct table *tbl, const char *key, const char *value)
335 {
336   // Options with no value
337   if(value == NULL || (value != NULL && strlen(value) == 0)) {
338     if(strcmp(key, "noheader") == 0) {
339       tbl->print_header = 0;
340       return NULL;
341     }
342   }
343
344   // Options with a value
345   if(value) {
346     if(strcmp(key, "header") == 0) {
347       if(value[1] != 0)
348         return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
349       uint tmp = value[0] - '0';
350       if(tmp > 1)
351         return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
352       tbl->print_header = tmp;
353       return NULL;
354     } else if(strcmp(key, "cols") == 0) {
355       const char *err = table_set_col_order_by_name(tbl, value);
356       if(err != NULL) {
357         return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
358       }
359       return NULL;
360     } else if(strcmp(key, "fmt") == 0) {
361       if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
362       else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
363       else {
364         return "Tableprinter: invalid argument to output-type option.";
365       }
366       return NULL;
367     } else if(strcmp(key, "col-delim") == 0) {
368       char * d = mp_printf(tbl->pool, "%s", value);
369       tbl->col_delimiter = d;
370       return NULL;
371     }
372   }
373
374   // Formatter options
375   if(tbl->formatter && tbl->formatter->process_option) {
376     const char *err = NULL;
377     if (tbl->formatter->process_option(tbl, key, value, &err)) {
378       return err;
379     }
380   }
381
382   // Unrecognized option
383   return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
384 }
385
386 const char *table_set_option(struct table *tbl, const char *opt)
387 {
388   char *key = stk_strdup(opt);
389   char *value = strchr(key, ':');
390   if(value) {
391     *value++ = 0;
392   }
393   return table_set_option_value(tbl, key, value);
394 }
395
396 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts)
397 {
398   for (uint i = 0; i < GARY_SIZE(gary_table_opts); i++) {
399     const char *rv = table_set_option(tbl, gary_table_opts[i]);
400     if(rv != NULL) {
401       return rv;
402     }
403   }
404   return NULL;
405 }
406
407 /*** Default formatter for human-readable output ***/
408
409 static void table_row_human_readable(struct table *tbl)
410 {
411   for(uint i = 0; i < tbl->cols_to_output; i++) {
412     int col_idx = tbl->column_order[i];
413     if(i) {
414       bputs(tbl->out, tbl->col_delimiter);
415     }
416     int col_width = tbl->columns[col_idx].width & CELL_ALIGN_MASK;
417     if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
418     bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
419   }
420   bputc(tbl->out, '\n');
421 }
422
423 static void table_write_header(struct table *tbl)
424 {
425   for(uint i = 0; i < tbl->cols_to_output; i++) {
426     int col_idx = tbl->column_order[i];
427     if(i) {
428       bputs(tbl->out, tbl->col_delimiter);
429     }
430     int col_width = tbl->columns[col_idx].width & CELL_ALIGN_MASK;
431     if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
432     bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
433   }
434   bputc(tbl->out, '\n');
435 }
436
437 static void table_start_human_readable(struct table *tbl)
438 {
439   if(tbl->col_delimiter == NULL) {
440     tbl->col_delimiter = " ";
441   }
442
443   if(tbl->append_delimiter == NULL) {
444     tbl->append_delimiter = ",";
445   }
446
447   if(tbl->print_header != 0) {
448     table_write_header(tbl);
449   }
450 }
451
452 struct table_formatter table_fmt_human_readable = {
453   .row_output = table_row_human_readable,
454   .table_start = table_start_human_readable,
455 };
456
457 /*** Default formatter for machine-readable output ***/
458
459 static void table_row_machine_readable(struct table *tbl)
460 {
461   for(uint i = 0; i < tbl->cols_to_output; i++) {
462     int col_idx = tbl->column_order[i];
463     if(i) {
464       bputs(tbl->out, tbl->col_delimiter);
465     }
466     bputs(tbl->out, tbl->col_str_ptrs[col_idx]);
467   }
468   bputc(tbl->out, '\n');
469 }
470
471 static void table_start_machine_readable(struct table *tbl)
472 {
473   if(tbl->col_delimiter == NULL) {
474     tbl->col_delimiter = ";";
475   }
476
477   if(tbl->append_delimiter == NULL) {
478     tbl->append_delimiter = ",";
479   }
480
481   if(tbl->print_header != 0) {
482     uint col_idx = tbl->column_order[0];
483     bputs(tbl->out, tbl->columns[col_idx].name);
484     for(uint i = 1; i < tbl->cols_to_output; i++) {
485       col_idx = tbl->column_order[i];
486       bputs(tbl->out, tbl->col_delimiter);
487       bputs(tbl->out, tbl->columns[col_idx].name);
488     }
489     bputc(tbl->out, '\n');
490   }
491 }
492
493 struct table_formatter table_fmt_machine_readable = {
494   .row_output = table_row_machine_readable,
495   .table_start = table_start_machine_readable,
496 };
497
498 /*** Tests ***/
499
500 #ifdef TEST
501
502 #include <stdio.h>
503
504 enum test_table_cols {
505   test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
506 };
507
508 static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str};
509
510 static struct table test_tbl = {
511   TBL_COLUMNS {
512     [test_col0_str] = TBL_COL_STR("col0_str", 20),
513     [test_col1_int] = TBL_COL_INT("col1_int", 8),
514     [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
515     [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
516     [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
517     TBL_COL_END
518   },
519   TBL_COL_ORDER(test_column_order),
520   TBL_OUTPUT_HUMAN_READABLE,
521   TBL_COL_DELIMITER("\t"),
522   TBL_APPEND_DELIMITER(",")
523 };
524
525 /**
526  * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
527  **/
528 static void do_print1(struct table *test_tbl)
529 {
530   table_col_str(test_tbl, test_col0_str, "sdsdf");
531   table_append_str(test_tbl, "aaaaa");
532   table_col_int(test_tbl, test_col1_int, -10);
533   table_col_int(test_tbl, test_col1_int, 10000);
534   table_col_uint(test_tbl, test_col2_uint, 10);
535   table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
536   table_col_bool(test_tbl, test_col3_bool, 1);
537   table_col_double(test_tbl, test_col4_double, 1.5);
538   table_col_printf(test_tbl, test_col4_double, "AAA");
539   table_end_row(test_tbl);
540
541   table_col_str(test_tbl, test_col0_str, "test");
542   table_append_str(test_tbl, "bbbbb");
543   table_col_int(test_tbl, test_col1_int, -100);
544   table_col_uint(test_tbl, test_col2_uint, 100);
545   table_col_bool(test_tbl, test_col3_bool, 0);
546   table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
547   table_end_row(test_tbl);
548 }
549
550 static void test_simple1(struct fastbuf *out)
551 {
552   table_init(&test_tbl);
553
554   // print table with header
555   table_set_col_order_by_name(&test_tbl, "col3_bool");
556   table_start(&test_tbl, out);
557   do_print1(&test_tbl);
558   table_end(&test_tbl);
559
560   // print the same table as in the previous case without header
561   table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
562   table_start(&test_tbl, out);
563   do_print1(&test_tbl);
564   table_end(&test_tbl);
565
566   // this also tests whether there is need to call table_set_col_order_by_name after table_end was called
567   test_tbl.print_header = 0;
568   table_start(&test_tbl, out);
569   do_print1(&test_tbl);
570   table_end(&test_tbl);
571   test_tbl.print_header = 1;
572
573   table_set_col_order_by_name(&test_tbl, "col3_bool");
574   table_start(&test_tbl, out);
575   do_print1(&test_tbl);
576   table_end(&test_tbl);
577
578   table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
579   table_start(&test_tbl, out);
580   do_print1(&test_tbl);
581   table_end(&test_tbl);
582
583   table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
584   table_start(&test_tbl, out);
585   do_print1(&test_tbl);
586   table_end(&test_tbl);
587
588   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");
589   table_start(&test_tbl, out);
590   do_print1(&test_tbl);
591   table_end(&test_tbl);
592
593   table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
594   table_start(&test_tbl, out);
595   do_print1(&test_tbl);
596   table_end(&test_tbl);
597
598   table_cleanup(&test_tbl);
599 }
600
601 enum test_any_table_cols {
602   test_any_col0_int, test_any_col1_any
603 };
604
605 static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
606
607 static struct table test_any_tbl = {
608   TBL_COLUMNS {
609     [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
610     [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
611     TBL_COL_END
612   },
613   TBL_COL_ORDER(test_any_column_order),
614   TBL_OUTPUT_HUMAN_READABLE,
615   TBL_COL_DELIMITER("\t"),
616   TBL_APPEND_DELIMITER(",")
617 };
618
619 static void test_any_type(struct fastbuf *out)
620 {
621   table_init(&test_any_tbl);
622
623   table_start(&test_any_tbl, out);
624
625   table_col_int(&test_any_tbl, test_any_col0_int, -10);
626   table_col_int(&test_any_tbl, test_any_col1_any, 10000);
627   table_end_row(&test_any_tbl);
628
629   table_col_int(&test_any_tbl, test_any_col0_int, -10);
630   table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
631   table_end_row(&test_any_tbl);
632
633   table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
634   table_append_printf(&test_any_tbl, "%d", 20);
635   table_append_printf(&test_any_tbl, "%d", 30);
636   table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
637   table_append_printf(&test_any_tbl, "%.2lf", 1.5);
638   table_append_printf(&test_any_tbl, "%.2lf", 1.6);
639   table_end_row(&test_any_tbl);
640
641   table_end(&test_any_tbl);
642   table_cleanup(&test_any_tbl);
643 }
644
645 int main(int argc UNUSED, char **argv UNUSED)
646 {
647   struct fastbuf *out;
648   out = bfdopen_shared(1, 4096);
649
650   test_simple1(out);
651
652   test_any_type(out);
653
654   bclose(out);
655   return 0;
656 }
657
658 #endif