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