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