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