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