2 * UCW Library -- Table printer
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
12 #include <ucw/fastbuf.h>
13 #include <ucw/mempool.h>
14 #include <ucw/xtypes.h>
16 #ifdef CONFIG_UCW_CLEAN_ABI
17 #define table_cleanup ucw_table_cleanup
18 #define table_col_bool ucw_table_col_bool
19 #define table_col_double ucw_table_col_double
20 #define table_col_fbend ucw_table_col_fbend
21 #define table_col_fbstart ucw_table_col_fbstart
22 #define table_col_generic_format ucw_table_col_generic_format
23 #define table_col_int ucw_table_col_int
24 #define table_col_intmax ucw_table_col_intmax
25 #define table_col_is_printed ucw_table_col_is_printed
26 #define table_col_printf ucw_table_col_printf
27 #define table_col_s64 ucw_table_col_s64
28 #define table_col_str ucw_table_col_str
29 #define table_col_str ucw_table_col_str
30 #define table_col_u64 ucw_table_col_u64
31 #define table_col_uint ucw_table_col_uint
32 #define table_col_uintmax ucw_table_col_uintmax
33 #define table_end ucw_table_end
34 #define table_end_row ucw_table_end_row
35 #define table_fmt_blockline ucw_table_fmt_blockline
36 #define table_fmt_human_readable ucw_table_fmt_human_readable
37 #define table_fmt_machine_readable ucw_table_fmt_machine_readable
38 #define table_get_col_idx ucw_table_get_col_idx
39 #define table_get_col_list ucw_table_get_col_list
40 #define table_init ucw_table_init
41 #define table_reset_row ucw_table_reset_row
42 #define table_set_col_opt ucw_table_set_col_opt
43 #define table_set_col_order ucw_table_set_col_order
44 #define table_set_col_order_by_name ucw_table_set_col_order_by_name
45 #define table_set_formatter ucw_table_set_formatter
46 #define table_set_gary_options ucw_table_set_gary_options
47 #define table_set_option ucw_table_set_option
48 #define table_set_option_value ucw_table_set_option_value
49 #define table_start ucw_table_start
57 // FIXME: update documentation according to the changes made in recent commits!
59 /** The COL_TYPE_ANY macro specifies a column type which can be filled with arbitrary type. **/
61 #define COL_TYPE_ANY NULL
63 /** Justify cell contents to the left. **/
64 #define CELL_ALIGN_LEFT (1U << 31)
66 // CELL_FLAG_MASK has 1's in bits used for column flags,
67 // CELL_WIDTH_MASK has 1's in bits used for column width.
68 #define CELL_FLAG_MASK (CELL_ALIGN_LEFT)
69 #define CELL_WIDTH_MASK (~CELL_FLAG_MASK)
74 * Definition of a single table column.
75 * Usually, this is generated using the `TABLE_COL_`'type' macros.
76 * Fields marked with `[*]` are user-accessible.
79 const char *name; // [*] Name of the column displayed in table header
80 uint width; // [*] Width of the column (in characters) OR'ed with column flags
81 uint fmt; // [*] default format of the column
82 const struct xtype *type_def; // [*] pointer to xtype of this column
84 const char * (*set_col_opt)(struct table *tbl, uint col_inst_idx, const char *col_opt);
85 // [*] process table option for a column instance. @col_inst_idx is the index of the column
86 // instance to which the @col_opt is set. Return value is the error string.
90 * Definition of a column instance. The table_col_instance belongs to a struct table. col_def is
91 * pointing to a definition of the column in struct table::columns. The user can fill only the @idx
92 * and @fmt. The @col_def, @cell_content, @next_column are private fields.
94 * Please use only fields marked with `[*]`.
96 struct table_col_instance {
97 uint idx; // [*] idx is a index into struct table::columns
98 const struct table_column *col_def; // this is pointer to the column definition, located in the array struct table::columns
99 const char *cell_content; // content of the cell of the current row
100 int next_column; // index of next column in linked list of columns of the same type
101 uint fmt; // [*] format of this column
105 * Definition of a table. Contains column definitions, and some per-table settings.
106 * Please use only fields marked with `[*]`.
108 struct table_template {
109 const struct table_column *columns; // [*] Definition of columns
110 struct table_col_instance *column_order; // [*] Order of the columns in the print-out of the table
111 const char *col_delimiter; // [*] Delimiter that is placed between columns
112 // Back-end used for table formatting and its private data
113 const struct table_formatter *formatter;
117 * Handle of a table. Contains column definitions, per-table settings
118 * and internal data. To change the table definition, please use only
119 * fields marked with `[*]`.
122 const struct table_column *columns; // [*] Definition of columns
123 int column_count; // [*] Number of columns (calculated by table_init())
124 int *ll_headers; // headers of linked lists that connects column instances
125 struct mempool *pool; // Memory pool used for storing table data. Contains global state
126 // and data of the current row.
127 struct mempool_state pool_state; // State of the pool after the table is initialized, i.e., before
128 // per-row data have been allocated.
130 struct table_col_instance *column_order; // [*] Order of the columns in the print-out of the table
131 uint cols_to_output; // [*] Number of columns that are printed
132 const char *col_delimiter; // [*] Delimiter that is placed between columns
133 bool print_header; // [*] false indicates that table header should not be printed
135 struct fastbuf *out; // [*] Fastbuffer to which the table is printed
136 bool row_printing_started; // Indicates that a row has been started.
137 struct fbpool fb_col_out; // Per-cell fastbuf, see table_col_fbstart()
138 int col_out; // Index of the column that is currently printed using fb_col_out
140 // Back-end used for table formatting and its private data
141 const struct table_formatter *formatter;
142 void *formatter_data;
146 * In most cases, table descriptions are constructed using the following macros.
147 * See the examples above for more details.
149 * * `TBL_COLUMNS` indicates the start of definition of columns
150 * * `TBL_COL_`'type'`(name, width)` defines a column of a given type
151 * * `TBL_COL_`'type'`_FMT(name, width, fmt)` defines a column with a custom format string
152 * * `TBL_COL_END` ends the column definitions
153 * * `TBL_COL_ORDER` specifies custom ordering of columns in the output
154 * * `TBL_COL_DELIMITER` and `TBL_APPEND_DELIMITER` override default delimiters
155 * * `TBL_FMT_HUMAN_READABLE` requests human-readable formatting (this is the default)
156 * * `TBL_FMT_MACHINE_READABLE` requests machine-readable TSV output
157 * * `TBL_FMT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line)
161 #define TBL_COL_STR(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_str }
162 #define TBL_COL_INT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_int }
163 #define TBL_COL_S64(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_s64 }
164 #define TBL_COL_UINT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_uint }
165 #define TBL_COL_U64(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_u64 }
166 #define TBL_COL_INTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_intmax }
167 #define TBL_COL_UINTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_uintmax }
168 #define TBL_COL_HEXUINT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_uint }
169 #define TBL_COL_DOUBLE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_double }
170 #define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_bool }
171 #define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_ANY }
172 #define TBL_COL_XTYPE(_name, _width, _xtype) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = _xtype }
174 #define TBL_COL_STR_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_str }
175 #define TBL_COL_INT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_int }
176 #define TBL_COL_S64_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_s64 }
177 #define TBL_COL_UINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_uint }
178 #define TBL_COL_U64_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_u64 }
179 #define TBL_COL_INTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_intmax }
180 #define TBL_COL_UINTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_uintmax }
181 #define TBL_COL_HEXUINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_uint }
182 #define TBL_COL_BOOL_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_bool }
183 #define TBL_COL_ANY_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_ANY }
184 #define TBL_COL_DOUBLE_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_double }
186 #define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type_def = NULL }
188 #define TBL_COLUMNS .columns = (struct table_column [])
189 #define TBL_COL_ORDER(order) .column_order = (struct table_col_instance *) order
190 #define TBL_COL_DELIMITER(_delimiter) .col_delimiter = _delimiter
193 * These macros are used for definition of column order
195 #define TBL_COL(_idx) { .idx = _idx, .fmt = XTYPE_FMT_DEFAULT, .next_column = -1 }
196 #define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .fmt = _fmt, .next_column = -1 }
197 #define TBL_COL_ORDER_END { .col_def = 0, .idx = ~0U, .fmt = 0, .next_column = -1 }
200 * These macros are aliases to various kinds of table formats.
202 #define TBL_FMT_HUMAN_READABLE .formatter = &table_fmt_human_readable
203 #define TBL_FMT_BLOCKLINE .formatter = &table_fmt_blockline
204 #define TBL_FMT_MACHINE_READABLE .formatter = &table_fmt_machine_readable
205 #define TBL_FMT(_fmt) .formatter = _fmt
208 * Creates a new table from a table template. The template should already contain
209 * the definitions of columns.
211 struct table *table_init(const struct table_template *tbl_template);
213 /** Destroy a table instance, freeing all memory used by it. **/
214 void table_cleanup(struct table *tbl);
217 * Start printing of a table. This is a prerequisite to setting of column values.
218 * After @table_start() is called, it is no longer possible to change parameters
219 * of the table by `table_set_`'something' nor by direct access to the table structure.
221 void table_start(struct table *tbl, struct fastbuf *out);
224 * This function must be called after all the rows of the current table are printed,
225 * making the table structure ready for the next table. You can call `table_set_`'something'
226 * between @table_end() and @table_start().
228 void table_end(struct table *tbl);
231 * Filling tables with data
232 * ------------------------
234 * For each column type, there are functions for filling of cells
235 * of the particular type:
237 * * `table_col_`'type'`(table, col_def_idx, value)` sets the cell in column `col_def_idx`
242 #define TABLE_COL_PROTO(_name, _type) void table_col_##_name(struct table *tbl, int col, _type val);
244 TABLE_COL_PROTO(int, int)
245 TABLE_COL_PROTO(uint, uint)
246 TABLE_COL_PROTO(double, double)
247 TABLE_COL_PROTO(intmax, intmax_t)
248 TABLE_COL_PROTO(uintmax, uintmax_t)
249 TABLE_COL_PROTO(s64, s64)
250 TABLE_COL_PROTO(u64, u64)
251 TABLE_COL_PROTO(bool, bool)
252 TABLE_COL_PROTO(str, const char *)
254 /** TABLE_COL_BODY macro enables easy definitions of bodies of table_col_<something> functions **/
255 #define TABLE_COL_BODY(_name, _type) void table_col_##_name(struct table *tbl, int col, _type val) {\
256 table_col_generic_format(tbl, col, (void*)&val, &xt_##_name);\
260 * The table_col_generic_format performs all the checks necessary while filling cell with value,
261 * calls the format function from expected_type and stores its result as a cell value. The function
262 * guarantees that each column instance is printed with its format.
264 void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type);
267 * Set a particular cell of the current row to a string formatted
268 * by sprintf(). This function can set a column of an arbitrary type.
270 void table_col_printf(struct table *tbl, int col, const char *fmt, ...) FORMAT_CHECK(printf, 3, 4);
273 * Alternatively, a string cell can be constructed as a stream.
274 * This function creates a fastbuf stream connected to the contents
275 * of the particular cell. Before you close the stream by @table_col_fbend(),
276 * no other operations with cells are allowed.
278 struct fastbuf *table_col_fbstart(struct table *tbl, int col);
281 * Close the stream that is used for printing of the current column.
283 void table_col_fbend(struct table *tbl);
286 * Called when all cells of the current row have their values filled in.
287 * It sends the completed row to the output stream.
289 void table_end_row(struct table *tbl);
292 * Resets data in the current row.
294 void table_reset_row(struct table *tbl);
297 * Configuration functions
298 * -----------------------
302 * Find the index of a column definition with name @col_name. Returns -1 if there is no such column.
304 int table_get_col_idx(struct table *tbl, const char *col_name);
307 * Sets a string option to an instance of a column type. This is the default version that checks
308 * whether the xtype::parse_fmt can be called and calls it. However, there are situation in which
309 * the xtype::parse_fmt is not sufficient, e.g., column decoration, post-processing, etc.
311 * Each struct table_column has a pointer to a customized version of table_set_col_opt (called
312 * set_col_opt). The hook set_call_opt should be always called through @table_set_col_opt. The hook
313 * and @table_set_col_opt has the same prototype, but @table_set_col_opt should never be used as the
314 * table_set_opt hook.
316 const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *col_opt);
319 * Returns a comma-and-space-separated list of column names, allocated from table's internal
322 const char *table_get_col_list(struct table *tbl);
325 * Sets the order in which the columns are printed. The columns are specified by array of struct
326 * @table_col_instance. This allows specification of format. The user should make an array of struct
327 * @table_col_instance and fill the array using the TBL_COL and TBL_COL_FMT. The array has a special
328 * last element: @TBL_COL_ORDER_END.
330 * The table copies content of @col_order into an internal representation stored
331 * in `column_order`. Options to column instances can be set using @table_set_col_opt().
333 void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order);
336 * Sets the order in which the columns are printed. The specification is a string with comma-separated column
337 * names. Returns NULL for success and an error message otherwise. The string is not referenced after
338 * this function returns.
340 * The format of the col_order string is the following:
341 * <col-order-string> := <col-def>[,<col-def>]*
343 * <col-def> := <col-name> [ '[' <col-opts> ']' ]
345 * <col-name> is a string that does not contain comma ',' or '[',']' brackets
347 * <col-opts> := <string> [ ',' <string> ]
348 * - <col-opts> is a comma-separated list of options
350 const char *table_set_col_order_by_name(struct table *tbl, const char *col_order);
353 * Returns true if @col_def_idx will be printed, false otherwise.
355 bool table_col_is_printed(struct table *tbl, uint col_def_idx);
358 * Sets table formatter. See below for the list of formatters.
360 void table_set_formatter(struct table *tbl, const struct table_formatter *fmt);
363 * Set a table option. All options have a key and a value. Currently,
364 * the following keys are defined (other keys can be accepted by formatters):
367 * |===================================================================================================
368 * | key | value | meaning
369 * | `header` | 0 or 1 | set whether a table header should be printed
370 * | `noheader` | 'none' | equivalent to `header`=0
371 * | `cols` | comma-separated column list | set order of columns
372 * | `fmt` | `human`/`machine`/`block` | set table formatter to one of the built-in formatters
373 * | `col-delim`| string | set column delimiter
374 * | `cells` | string | set column format mode
375 * | `raw` | 'none' | set column format to raw data
376 * | `pretty` | 'none' | set column format to pretty-printing
377 * |===================================================================================================
379 const char *table_set_option_value(struct table *tbl, const char *key, const char *value);
382 * Sets a table option given as 'key'`:`'value' or 'key' (with no value).
384 const char *table_set_option(struct table *tbl, const char *opt);
387 * Sets several table option in 'key'`:`'value' form, stored in a growing array.
388 * This is frequently used for options given on the command line.
390 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts);
396 * Transformation of abstract cell data to the characters in the output stream
397 * is under control of a formatter (which serves as a back-end of the table printer).
398 * There are several built-in formatters, but you can define your own.
400 * A formatter is described by a structure, which contains pointers to several
401 * call-back functions, which are called by the table printer at specific occasions.
403 * The formatter can keep its internal state in the `data` field of `struct table`
404 * and allocate temporary data from the table's memory pool. Memory allocated in
405 * the `row_output` call-back is freed before the next row begins. Memory allocated
406 * between the beginning of `table_start` and the end of `table_end` is freed after
407 * `table_end`. Memory allocated by `process_option` when no table is started
408 * is kept until @table_cleanup().
411 /** Definition of a formatter back-end. **/
412 struct table_formatter {
413 void (*row_output)(struct table *tbl); // [*] Function that outputs one row
414 void (*table_start)(struct table *tbl); // [*] table_start callback (optional)
415 void (*table_end)(struct table *tbl); // [*] table_end callback (optional)
416 bool (*process_option)(struct table *tbl, const char *key, const char *value, const char **err);
417 // [*] Process table option and possibly return an error message (optional)
420 /** Standard formatter for human-readable output. **/
421 extern const struct table_formatter table_fmt_human_readable;
423 /** Standard formatter for machine-readable output (tab-separated values). **/
424 extern const struct table_formatter table_fmt_machine_readable;
427 * Standard formatter for block output. Each cell is output on its own line
428 * of the form `column_name: value`. Rows are separated by blank lines.
430 extern const struct table_formatter table_fmt_blockline;