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