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