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