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