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