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