2 * UCW Library -- Table printer
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
10 #include <ucw/fastbuf.h>
11 #include <ucw/mempool.h>
13 #ifdef CONFIG_UCW_CLEAN_ABI
14 #define table_append_bool ucw_table_append_bool
15 #define table_append_double ucw_table_append_double
16 #define table_append_int ucw_table_append_int
17 #define table_append_intmax ucw_table_append_intmax
18 #define table_append_printf ucw_table_append_printf
19 #define table_append_str ucw_table_append_str
20 #define table_append_u64 ucw_table_append_u64
21 #define table_append_uint ucw_table_append_uint
22 #define table_append_uintmax ucw_table_append_uintmax
23 #define table_cleanup ucw_table_cleanup
24 #define table_col_bool ucw_table_col_bool
25 #define table_col_bool_fmt ucw_table_col_bool_fmt
26 #define table_col_bool_name ucw_table_col_bool_name
27 #define table_col_double ucw_table_col_double
28 #define table_col_fbend ucw_table_col_fbend
29 #define table_col_fbstart ucw_table_col_fbstart
30 #define table_col_int ucw_table_col_int
31 #define table_col_intmax ucw_table_col_intmax
32 #define table_col_printf ucw_table_col_printf
33 #define table_col_str ucw_table_col_str
34 #define table_col_u64 ucw_table_col_u64
35 #define table_col_s64 ucw_table_col_s64
36 #define table_col_uint ucw_table_col_uint
37 #define table_col_uintmax ucw_table_col_uintmax
38 #define table_end ucw_table_end
39 #define table_end_row ucw_table_end_row
40 #define table_fmt_human_readable ucw_table_fmt_human_readable
41 #define table_fmt_machine_readable ucw_table_fmt_machine_readable
42 #define table_get_col_idx ucw_table_get_col_idx
43 #define table_get_col_list ucw_table_get_col_list
44 #define table_init ucw_table_init
45 #define table_set_col_order ucw_table_set_col_order
46 #define table_set_col_order_by_name ucw_table_set_col_order_by_name
47 #define table_set_formatter ucw_table_set_formatter
48 #define table_set_gary_options ucw_table_set_gary_options
49 #define table_set_option ucw_table_set_option
50 #define table_set_option_value ucw_table_set_option_value
51 #define table_start ucw_table_start
59 /** Types of columns. These are seldom used explicitly, a column definition macro is used instead. **/
61 COL_TYPE_STR, // String
63 COL_TYPE_S64, // Signed 64-bit integer
64 COL_TYPE_INTMAX, // intmax_t
65 COL_TYPE_UINT, // unsigned int
66 COL_TYPE_U64, // Unsigned 64-bit integer
67 COL_TYPE_UINTMAX, // uintmax_t
68 COL_TYPE_BOOL, // bool
69 COL_TYPE_DOUBLE, // double
70 COL_TYPE_ANY, // Any type
74 /** Column flag (to be OR-ed to column's width) **/
75 #define CELL_ALIGN_LEFT (1U << 31) // Justify cell contents to the left
77 // CELL_FLAG_MASK has 1's in bits used for column flags,
78 // CELL_WIDTH_MASK has 1's in bits used for column width.
79 #define CELL_FLAG_MASK (CELL_ALIGN_LEFT)
80 #define CELL_WIDTH_MASK (~CELL_FLAG_MASK)
83 * Definition of a single table column.
84 * Usually, this is generated using the `TABLE_COL_xxx` macros.
85 * Fields marked with `[*]` are user-accessible.
88 const char *name; // [*] Name of the column displayed in table header
89 int width; // [*] Width of the column (in characters) OR'ed with column flags
90 const char *fmt; // [*] Default format of each cell in the column
91 enum column_type type; // [*] Type of the cells in the column
95 * Definition of a table. Contains column definitions, per-table settings
96 * and internal data. Please use only fields marked with `[*]`.
99 struct table_column *columns; // [*] Definition of columns
100 int column_count; // [*] Number of columns (calculated by table_init())
101 struct mempool *pool; // Memory pool used for storing table data. Contains global state
102 // and data of the current row.
103 struct mempool_state pool_state; // State of the pool after the table is initialized, i.e., before
104 // per-row data have been allocated.
106 char **col_str_ptrs; // Values of cells in the current row (allocated from the pool)
108 uint *column_order; // [*] Order of the columns in the print-out of the table
109 uint cols_to_output; // [*] Number of columns that are printed
110 const char *col_delimiter; // [*] Delimiter that is placed between columns
111 const char *append_delimiter; // [*] Separator of multiple values in a single cell (see table_append_...())
112 uint print_header; // [*] 0 indicates that table header should not be printed
114 struct fastbuf *out; // [*] Fastbuffer to which the table is printed
115 int last_printed_col; // Index of the last column which was set. -1 indicates start of row.
116 // Used for example for appending to the current column.
117 int row_printing_started; // Indicates that a row has been started. Duplicates last_printed_col, but harmlessly.
118 struct fbpool fb_col_out; // Per-cell fastbuf, see table_col_fbstart()
119 int col_out; // Index of the column that is currently printed using fb_col_out
121 // Back-end used for table formatting and its private data
122 struct table_formatter *formatter;
127 * In most cases, table descriptions are constructed using the following macros.
128 * See the examples above for more details.
130 * * `TBL_COLUMNS` indicates the start of definition of columns
131 * * `TBL_COL_xxx(name, width)` defines a column of type `xxx`
132 * * `TBL_COL_xxx_FMT(name, width, fmt)` defines a column with a custom format string
133 * * `TBL_COL_END` ends the column definitions
134 * * `TBL_COL_ORDER` specifies custom ordering of columns in the output
135 * * `TBL_COL_DELIMITER` and `TBL_APPEND_DELIMITER` override default delimiters
136 * * `TBL_OUTPUT_HUMAN_READABLE` requests human-readable formatting (this is the default)
137 * * `TBL_OUTPUT_MACHINE_READABLE` requests machine-readable TSV output
138 * * `TBL_OUTPUT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line)
142 #define TBL_COL_STR(_name, _width) { .name = _name, .width = _width, .fmt = "%s", .type = COL_TYPE_STR }
143 #define TBL_COL_INT(_name, _width) { .name = _name, .width = _width, .fmt = "%d", .type = COL_TYPE_INT }
144 #define TBL_COL_S64(_name, _width) { .name = _name, .width = _width, .fmt = "%lld", .type = COL_TYPE_S64 }
145 #define TBL_COL_UINT(_name, _width) { .name = _name, .width = _width, .fmt = "%u", .type = COL_TYPE_UINT }
146 #define TBL_COL_U64(_name, _width) { .name = _name, .width = _width, .fmt = "%llu", .type = COL_TYPE_U64 }
147 #define TBL_COL_INTMAX(_name, _width) { .name = _name, .width = _width, .fmt = "%jd", .type = COL_TYPE_INTMAX }
148 #define TBL_COL_UINTMAX(_name, _width) { .name = _name, .width = _width, .fmt = "%ju", .type = COL_TYPE_UINTMAX }
149 #define TBL_COL_HEXUINT(_name, _width) { .name = _name, .width = _width, .fmt = "0x%x", .type = COL_TYPE_UINT }
150 #define TBL_COL_DOUBLE(_name, _width, _prec) { .name = _name, .width = _width, .fmt = "%." #_prec "lf", .type = COL_TYPE_DOUBLE }
151 #define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = "%s", .type = COL_TYPE_BOOL }
152 #define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = 0, .type = COL_TYPE_ANY }
154 #define TBL_COL_STR_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_STR }
155 #define TBL_COL_INT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INT }
156 #define TBL_COL_S64_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_S64 }
157 #define TBL_COL_UINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
158 #define TBL_COL_U64_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_U64 }
159 #define TBL_COL_INTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INTMAX }
160 #define TBL_COL_UINTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINTMAX }
161 #define TBL_COL_HEXUINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
162 #define TBL_COL_BOOL_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_BOOL }
164 #define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type = COL_TYPE_LAST }
166 #define TBL_COLUMNS .columns = (struct table_column [])
167 #define TBL_COL_ORDER(order) .column_order = (int *) order, .cols_to_output = ARRAY_SIZE(order)
168 #define TBL_COL_DELIMITER(_delimiter_) .col_delimiter = _delimiter_
169 #define TBL_APPEND_DELIMITER(_delimiter_) .append_delimiter = _delimiter_
171 #define TBL_OUTPUT_HUMAN_READABLE .formatter = &table_fmt_human_readable
172 #define TBL_OUTPUT_BLOCKLINE .formatter = &table_fmt_blockline
173 #define TBL_OUTPUT_MACHINE_READABLE .formatter = &table_fmt_machine_readable
176 * @table_init serves for initialization of the table. The structure should
177 * already contain the definitions of columns.
179 void table_init(struct table *tbl);
181 /** Destroy a table definition, freeing all memory used by it. **/
182 void table_cleanup(struct table *tbl);
185 * Start printing of a table. This is a prerequisite to setting of column values.
186 * After table_start() is called, it is no longer possible to change parameters
187 * of the table by `table_set_xxx` nor by direct access to the table structure.
189 void table_start(struct table *tbl, struct fastbuf *out);
192 * This function must be called after all the rows of the current table are printed,
193 * making the table structure ready for the next table. You can call `table_set_*`
194 * between table_end() and table_start().
196 void table_end(struct table *tbl);
199 * Filling tables with data
200 * ------------------------
202 * For each column type, there are functions for filling of cells
203 * of the particular type:
205 * * `table_col_`'type'`(table, idx, value)` sets the cell in column `idx`
207 * * `table_col_`'type'`_fmt(table, idx, fmt, ...)` does the same with
208 * a custom printf-like format string
209 * * `table_col_`'type'`_name(table, name, value)` refers to the column
210 * by its name instead of its index.
211 * * `table_append_`'type'`(table, value)` appends a value to the most
212 * recently accessed cell.
215 #define TABLE_COL_PROTO(_name_, _type_) void table_col_##_name_(struct table *tbl, int col, _type_ val);\
216 void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val);\
217 void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val) FORMAT_CHECK(printf, 3, 0);
219 // table_col_<type>_fmt has one disadvantage: it is not possible to
220 // check whether fmt contains format that contains formatting that is
221 // compatible with _type_
223 TABLE_COL_PROTO(int, int);
224 TABLE_COL_PROTO(uint, uint);
225 TABLE_COL_PROTO(double, double);
226 TABLE_COL_PROTO(str, const char *);
227 TABLE_COL_PROTO(intmax, intmax_t);
228 TABLE_COL_PROTO(uintmax, uintmax_t);
229 TABLE_COL_PROTO(s64, s64);
230 TABLE_COL_PROTO(u64, u64);
232 void table_col_bool(struct table *tbl, int col, uint val);
233 void table_col_bool_name(struct table *tbl, const char *col_name, uint val);
234 void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val);
235 #undef TABLE_COL_PROTO
237 #define TABLE_APPEND_PROTO(_name_, _type_) void table_append_##_name_(struct table *tbl, _type_ val)
238 TABLE_APPEND_PROTO(int, int);
239 TABLE_APPEND_PROTO(uint, uint);
240 TABLE_APPEND_PROTO(double, double);
241 TABLE_APPEND_PROTO(str, const char *);
242 TABLE_APPEND_PROTO(intmax, intmax_t);
243 TABLE_APPEND_PROTO(uintmax, uintmax_t);
244 TABLE_APPEND_PROTO(s64, s64);
245 TABLE_APPEND_PROTO(u64, u64);
246 void table_append_bool(struct table *tbl, int val);
247 #undef TABLE_APPEND_PROTO
250 * Set a particular cell of the current row to a string formatted
251 * by sprintf(). This function can set a column of an arbitrary type.
253 void table_col_printf(struct table *tbl, int col, const char *fmt, ...) FORMAT_CHECK(printf, 3, 4);
256 * Appends a string formatted by sprintf() to the most recently filled cell.
257 * This function can work with columns of an arbitrary type.
259 void table_append_printf(struct table *tbl, const char *fmt, ...) FORMAT_CHECK(printf, 2, 3);
262 * Alternatively, a string cell can be constructed as a stream.
263 * This function creates a fastbuf stream connected to the contents
264 * of the particular cell. Before you close the stream by table_col_fbend(),
265 * no other operations with cells are allowed.
267 struct fastbuf *table_col_fbstart(struct table *tbl, int col);
270 * Closes the stream that is used for printing of the current column.
272 void table_col_fbend(struct table *tbl);
275 * Called when all cells of the current row have their values filled in.
276 * It sends the completed row to the output stream.
278 void table_end_row(struct table *tbl);
281 * Configuration functions
282 * -----------------------
286 * Find the index of a column with name @col_name. Returns -1 if there is no such column.
288 int table_get_col_idx(struct table *tbl, const char *col_name);
291 * Returns a comma-and-space-separated list of column names, allocated from table's internal
294 const char *table_get_col_list(struct table *tbl);
297 * Sets the order in which the columns are printed. The @col_order parameter is used until table_end() or
298 * table_cleanup() is called. The table stores only the pointer and the memory pointed to by @col_order is
299 * allocated and deallocated by the caller.
301 void table_set_col_order(struct table *tbl, int *col_order, int col_order_size);
304 * Sets the order in which the columns are printed. The specification is a string with comma-separated column
305 * names. Returns NULL for success and an error message otherwise. The string is not referenced after
306 * this function returns.
308 const char *table_set_col_order_by_name(struct table *tbl, const char *col_order);
311 * Sets table formatter for @tbl. See below for the list of formatters.
313 void table_set_formatter(struct table *tbl, struct table_formatter *fmt);
316 * Set a table option. All options have a key and a value. Currently,
317 * the following keys are defined (other keys can be accepted by formatters):
320 * |=======================
321 * | key | value | meaning
322 * | `header` | 0 or 1 | set whether a table header should be printed
323 * | `noheader` | 'none' | equivalent to `header`=0
324 * | `cols` | column list | set order of columns (accepts a comma-separated list of column names)
325 * | `fmt` | formatter | set table formatter (`human`, `machine`, `block`)
326 * | `col-delim`| string | set column delimiter
327 * |========================
329 const char *table_set_option_value(struct table *tbl, const char *key, const char *value);
332 * Sets a table option given as 'key'`:`'value' or `key` (with no value).
334 const char *table_set_option(struct table *tbl, const char *opt);
337 * Sets several table option in 'key'`:`'value' form, stored in a growing array.
338 * This is frequently used for options given on the command line.
340 const char *table_set_gary_options(struct table *tbl, char **gary_table_opts);
346 * Each formatter defines several call-back functions, which are called
347 * by the table printer at specific points. The formatter can keep its internal
348 * state in the `data` field of `struct table` and allocate temporary data
349 * from the table's memory pool.
352 /** Definition of a formatter back-end. **/
353 struct table_formatter {
354 void (*row_output)(struct table *tbl); // [*] Function that outputs one row
355 void (*table_start)(struct table *tbl); // [*] table_start callback (optional)
356 void (*table_end)(struct table *tbl); // [*] table_end callback (optional)
357 bool (*process_option)(struct table *tbl, const char *key, const char *value, const char **err);
358 // [*] Process table option and possibly return an error message (optional)
361 /** Standard formatter for human-readable output **/
362 extern struct table_formatter table_fmt_human_readable;
364 /** Standard formatter for machine-readable output (tab-separated values) **/
365 extern struct table_formatter table_fmt_machine_readable;
367 /** Standard formatter for block output (one cell per line) **/
368 extern struct table_formatter table_fmt_blockline;