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