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