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