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