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