X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Ftable.h;h=c1bf73e314184d175b8c597de5f3089f8d728f5f;hb=14569005ceedc6c2ce34526b8a6ac6778938aa47;hp=6c20ba832ee591b07aa9e326e4b11f4a658330ec;hpb=8f29ef125a903b92ea6c098567615e28322c9b20;p=libucw.git diff --git a/ucw/table.h b/ucw/table.h index 6c20ba83..c1bf73e3 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -7,52 +7,29 @@ #ifndef _UCW_TABLE_H #define _UCW_TABLE_H +#include + #include #include #include -// FIXME: update these macros #ifdef CONFIG_UCW_CLEAN_ABI -#define table_append_bool ucw_table_append_bool -#define table_append_double ucw_table_append_double -#define table_append_int ucw_table_append_int -#define table_append_intmax ucw_table_append_intmax -#define table_append_printf ucw_table_append_printf -#define table_append_str ucw_table_append_str -#define table_append_u64 ucw_table_append_u64 -#define table_append_uint ucw_table_append_uint -#define table_append_uintmax ucw_table_append_uintmax #define table_cleanup ucw_table_cleanup #define table_col_bool ucw_table_col_bool -#define table_col_bool_fmt ucw_table_col_bool_fmt -#define table_col_bool_name ucw_table_col_bool_name #define table_col_double ucw_table_col_double -#define table_col_double_fmt ucw_table_col_double_fmt -#define table_col_double_name ucw_table_col_double_name #define table_col_fbend ucw_table_col_fbend #define table_col_fbstart ucw_table_col_fbstart +#define table_col_generic_format ucw_table_col_generic_format #define table_col_int ucw_table_col_int -#define table_col_int_fmt ucw_table_col_int_fmt -#define table_col_int_name ucw_table_col_int_name #define table_col_intmax ucw_table_col_intmax -#define table_col_intmax_fmt ucw_table_col_intmax_fmt -#define table_col_intmax_name ucw_table_col_intmax_name +#define table_col_is_printed ucw_table_col_is_printed #define table_col_printf ucw_table_col_printf #define table_col_s64 ucw_table_col_s64 -#define table_col_s64_fmt ucw_table_col_s64_fmt -#define table_col_s64_name ucw_table_col_s64_name #define table_col_str ucw_table_col_str -#define table_col_str_fmt ucw_table_col_str_fmt -#define table_col_str_name ucw_table_col_str_name +#define table_col_str ucw_table_col_str #define table_col_u64 ucw_table_col_u64 -#define table_col_u64_fmt ucw_table_col_u64_fmt -#define table_col_u64_name ucw_table_col_u64_name #define table_col_uint ucw_table_col_uint -#define table_col_uint_fmt ucw_table_col_uint_fmt -#define table_col_uint_name ucw_table_col_uint_name #define table_col_uintmax ucw_table_col_uintmax -#define table_col_uintmax_fmt ucw_table_col_uintmax_fmt -#define table_col_uintmax_name ucw_table_col_uintmax_name #define table_end ucw_table_end #define table_end_row ucw_table_end_row #define table_fmt_blockline ucw_table_fmt_blockline @@ -61,6 +38,8 @@ #define table_get_col_idx ucw_table_get_col_idx #define table_get_col_list ucw_table_get_col_list #define table_init ucw_table_init +#define table_reset_row ucw_table_reset_row +#define table_set_col_opt ucw_table_set_col_opt #define table_set_col_order ucw_table_set_col_order #define table_set_col_order_by_name ucw_table_set_col_order_by_name #define table_set_formatter ucw_table_set_formatter @@ -77,7 +56,7 @@ // FIXME: update documentation according to the changes made in recent commits! -/** Types of columns. These are seldom used explicitly, using a column definition macro is preferred. **/ +/** The COL_TYPE_ANY macro specifies a column type which can be filled with arbitrary type. **/ #define COL_TYPE_ANY NULL @@ -98,12 +77,13 @@ struct table; **/ struct table_column { const char *name; // [*] Name of the column displayed in table header - int width; // [*] Width of the column (in characters) OR'ed with column flags - enum xtype_fmt fmt; - const struct xtype *type_def; + uint width; // [*] Width of the column (in characters) OR'ed with column flags + uint fmt; // [*] default format of the column + const struct xtype *type_def; // [*] pointer to xtype of this column - bool (*set_col_instance_option)(struct table *tbl, uint col, const char *value, char **err); - // [*] process table option for a column instance + const char * (*set_col_opt)(struct table *tbl, uint col_inst_idx, const char *col_opt); + // [*] process table option for a column instance. @col_inst_idx is the index of the column to which the @col_opt is set. + // FIXME: Comment on arguments and return value }; // FIXME: is it correct to have idx and col_def? idx is sufficient and in fact a duplicity of idx @@ -113,7 +93,7 @@ struct table_col_instance { const struct table_column *col_def; // this is pointer to the column definition, located in the array struct table::columns const char *cell_content; // content of the cell of the current row int next_column; // index of next column in linked list of columns of the same type - enum xtype_fmt output_type; // format of this column + uint fmt; // format of this column }; /** @@ -126,7 +106,7 @@ struct table_template { uint cols_to_output; // [*] Number of columns that are printed const char *col_delimiter; // [*] Delimiter that is placed between columns // Back-end used for table formatting and its private data - struct table_formatter *formatter; + const struct table_formatter *formatter; }; /** @@ -146,18 +126,16 @@ struct table { struct table_col_instance *column_order; // [*] Order of the columns in the print-out of the table uint cols_to_output; // [*] Number of columns that are printed const char *col_delimiter; // [*] Delimiter that is placed between columns - uint print_header; // [*] 0 indicates that table header should not be printed + bool print_header; // [*] false indicates that table header should not be printed struct fastbuf *out; // [*] Fastbuffer to which the table is printed - int last_printed_col; // Index of the last column which was set. -1 indicates start of row. - // Used for example for appending to the current column. - int row_printing_started; // Indicates that a row has been started. Duplicates last_printed_col, but harmlessly. + bool row_printing_started; // Indicates that a row has been started. struct fbpool fb_col_out; // Per-cell fastbuf, see table_col_fbstart() int col_out; // Index of the column that is currently printed using fb_col_out // Back-end used for table formatting and its private data - struct table_formatter *formatter; - void *data; + const struct table_formatter *formatter; + void *formatter_data; }; /**** @@ -170,9 +148,9 @@ struct table { * * `TBL_COL_END` ends the column definitions * * `TBL_COL_ORDER` specifies custom ordering of columns in the output * * `TBL_COL_DELIMITER` and `TBL_APPEND_DELIMITER` override default delimiters - * * `TBL_OUTPUT_HUMAN_READABLE` requests human-readable formatting (this is the default) - * * `TBL_OUTPUT_MACHINE_READABLE` requests machine-readable TSV output - * * `TBL_OUTPUT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line) + * * `TBL_FMT_HUMAN_READABLE` requests human-readable formatting (this is the default) + * * `TBL_FMT_MACHINE_READABLE` requests machine-readable TSV output + * * `TBL_FMT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line) * ***/ @@ -187,7 +165,7 @@ struct table { #define TBL_COL_DOUBLE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_double } #define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_bool } #define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_ANY } -#define TBL_COL_CUSTOM(_name, _width, _xtype) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = _xtype } +#define TBL_COL_XTYPE(_name, _width, _xtype) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = _xtype } #define TBL_COL_STR_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_str } #define TBL_COL_INT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = &xt_int } @@ -205,17 +183,35 @@ struct table { #define TBL_COLUMNS .columns = (struct table_column []) #define TBL_COL_ORDER(order) .column_order = (struct table_col_instance *) order, .cols_to_output = ARRAY_SIZE(order) -#define TBL_COL_DELIMITER(_delimiter_) .col_delimiter = _delimiter_ -#define TBL_COL(_idx) { .idx = _idx, .output_type = XTYPE_FMT_DEFAULT, .next_column = -1 } -#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = _fmt, .next_column = -1 } -#define TBL_COL_TYPE(_idx, _type) { .idx = _idx, .output_type = _type, .next_column = -1 } +#define TBL_COL_DELIMITER(_delimiter) .col_delimiter = _delimiter -#define TBL_OUTPUT_HUMAN_READABLE .formatter = &table_fmt_human_readable -#define TBL_OUTPUT_BLOCKLINE .formatter = &table_fmt_blockline -#define TBL_OUTPUT_MACHINE_READABLE .formatter = &table_fmt_machine_readable +/** + * These macros are used for definition of column order + **/ +#define TBL_COL(_idx) { .idx = _idx, .fmt = XTYPE_FMT_DEFAULT, .next_column = -1 } +#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .fmt = _fmt, .next_column = -1 } + +/** + * These macros are aliases to various kinds of table formats. + **/ +#define TBL_FMT_HUMAN_READABLE .formatter = &table_fmt_human_readable +#define TBL_FMT_BLOCKLINE .formatter = &table_fmt_blockline +#define TBL_FMT_MACHINE_READABLE .formatter = &table_fmt_machine_readable +#define TBL_FMT(_fmt) .formatter = _fmt -#define TBL_COL_ITER_START(_tbl, _colidx, _var, _idxval) { struct table_col_instance *_var = NULL; int _idxval = _tbl->ll_headers[_colidx]; \ - for(_idxval = _tbl->ll_headers[_colidx], _var = _tbl->column_order + _idxval; _idxval != -1; _idxval = _tbl->column_order[_idxval].next_column, _var = _tbl->column_order + _idxval) +/** + * The TBL_COL_ITER_START macro are used for iterating over all instances of a particular column in + * table _tbl. _colidx is the column index in _tbl, _instptr is the pointer to the column instance + * (struct table_col_instance *), _idxval is the index of current column index. The variables are + * enclosed in a block, so they do not introduce variable name collisions. + * + * The TBL_COL_ITER_END macro must close the block started with TBL_COL_ITER_START. + * + * These macros are usually used to hide the implementation details of the column instances linked + * list. This is usefull for definition of new types. + **/ +#define TBL_COL_ITER_START(_tbl, _colidx, _instptr, _idxval) { struct table_col_instance *_instptr = NULL; int _idxval = _tbl->ll_headers[_colidx]; \ + for(_idxval = _tbl->ll_headers[_colidx], _instptr = _tbl->column_order + _idxval; _idxval != -1; _idxval = _tbl->column_order[_idxval].next_column, _instptr = _tbl->column_order + _idxval) #define TBL_COL_ITER_END } @@ -225,7 +221,7 @@ struct table { **/ struct table *table_init(const struct table_template *tbl_template); -/** Destroy a table definition, freeing all memory used by it. **/ +/** Destroy a table instance, freeing all memory used by it. **/ void table_cleanup(struct table *tbl); /** @@ -249,22 +245,12 @@ void table_end(struct table *tbl); * For each column type, there are functions for filling of cells * of the particular type: * - * * `table_col_`'type'`(table, idx, value)` sets the cell in column `idx` + * * `table_col_`'type'`(table, col_def_idx, value)` sets the cell in column `col_def_idx` * to the `value` - * * `table_col_`'type'`_fmt(table, idx, fmt, ...)` does the same with - * a custom printf-like format string - * * `table_col_`'type'`_name(table, name, value)` refers to the column - * by its name instead of its index. - * * `table_append_`'type'`(table, value)` appends a value to the most - * recently accessed cell. ***/ -#define TABLE_COL_PROTO(_name_, _type_) void table_col_##_name_(struct table *tbl, int col, _type_ val); - -// table_col__fmt has one disadvantage: it is not possible to -// check whether fmt contains format that contains formatting that is -// compatible with _type_ +#define TABLE_COL_PROTO(_name, _type) void table_col_##_name(struct table *tbl, int col, _type val); TABLE_COL_PROTO(int, int) TABLE_COL_PROTO(uint, uint) @@ -277,12 +263,16 @@ TABLE_COL_PROTO(bool, bool) void table_col_str(struct table *tbl, int col, const char * val); -/** macros that enables easy definitions of bodies of table_col_ functions **/ - -#define TABLE_COL_BODY(_name_, _type_) void table_col_##_name_(struct table *tbl, int col, _type_ val) {\ - table_col_generic_format(tbl, col, (void*)&val, &xt_##_name_);\ +/** TABLE_COL_BODY macro enables easy definitions of bodies of table_col_ functions **/ +#define TABLE_COL_BODY(_name, _type) void table_col_##_name(struct table *tbl, int col, _type val) {\ + table_col_generic_format(tbl, col, (void*)&val, &xt_##_name);\ } +/** + * The table_col_generic_format performs all the checks necessary while filling cell with value, + * calls the format function from expected_type and stores its result as a cell value. The function + * guarantees that each column instance is printed with its format. + **/ void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type); /** @@ -311,7 +301,7 @@ void table_col_fbend(struct table *tbl); void table_end_row(struct table *tbl); /** - * Resets data in current row. + * Resets data in the current row. **/ void table_reset_row(struct table *tbl); @@ -321,15 +311,26 @@ void table_reset_row(struct table *tbl); ***/ /** - * Find the index of a column with name @col_name. Returns -1 if there is no such column. + * Find the index of a column definition with name @col_name. Returns -1 if there is no such column. **/ int table_get_col_idx(struct table *tbl, const char *col_name); - /** - * Sets a string argument to a column instance + * Sets a string option to an instance of a column type. This is the default version that checks + * whether the xtype::parse_fmt can be called and calls it. However, there are situation in which + * the xtype::parse_fmt is not sufficient, e.g., column decoration, post-processing, etc. + * + * Each struct table_column has a pointer to a customized version of table_set_col_opt which is + * called instead of this (default) version of table_set_col_opt + * + * FIXME: Make table_set_col_opt() a front-end function used by everybody, + * which checks if the set_col_opt hook is defined and either calls it or + * processes the options in the generic way. Nobody else should call the + * hook directly. + * RK: that is the current solution the only confusion can be that + * the hook and this function has the same prototype. **/ -bool table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char ** err); +const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *col_opt); /** * Returns a comma-and-space-separated list of column names, allocated from table's internal @@ -338,16 +339,16 @@ bool table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_a const char *table_get_col_list(struct table *tbl); /** - * Sets the order in which the columns are printed. The @col_order parameter is used until @table_end() or - * @table_cleanup() is called. The table stores only the pointer and the memory pointed to by @col_order is - * allocated and deallocated by the caller. - **/ -void table_set_col_order(struct table *tbl, int *col_order, int col_order_size); - -/** - * Returns 1 if col_idx will be printed, 0 otherwise. + * Sets the order in which the columns are printed. The columns are specified by struct + * + * Sets the order in which the columns are printed. + * The table converts the integers in @col_order into an internal representation stored + * in `column_order`. Options to column instances can be set using @table_set_col_opt(). + * + * @table_col_instance. This allows specification of format. The user should make an array of struct + * @table_col_instance and fill the array using the TBL_COL and TBL_COL_FMT. **/ -bool table_col_is_printed(struct table *tbl, uint col_idx); +void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order, uint cols_to_output); /** * Sets the order in which the columns are printed. The specification is a string with comma-separated column @@ -361,16 +362,24 @@ bool table_col_is_printed(struct table *tbl, uint col_idx); * * is a string that does not contain comma ',' or '[',']' brackets * - * is currently only one string. + * is currently only one string without commas. In the future the format can be ,,... . * * FIXME In the future, we should allow to be a comma(,) separated list of identifiers **/ const char *table_set_col_order_by_name(struct table *tbl, const char *col_order); +/** + * Returns true if col_idx will be printed, false otherwise. + * + * FIXME: Naming of arguments is confusing. @col_idx sometimes indexes + * columns, but sometimes their instances. + **/ +bool table_col_is_printed(struct table *tbl, uint col_def_idx); + /** * Sets table formatter. See below for the list of formatters. **/ -void table_set_formatter(struct table *tbl, struct table_formatter *fmt); +void table_set_formatter(struct table *tbl, const struct table_formatter *fmt); /** * Set a table option. All options have a key and a value. Currently, @@ -384,6 +393,9 @@ void table_set_formatter(struct table *tbl, struct table_formatter *fmt); * | `cols` | comma-separated column list | set order of columns * | `fmt` | `human`/`machine`/`block` | set table formatter to one of the built-in formatters * | `col-delim`| string | set column delimiter + * | `cells` | string | set column format mode + * | `raw` | 'none' | set column format to raw data + * | `pretty` | 'none' | set column format to pretty-printing * |=================================================================================================== **/ const char *table_set_option_value(struct table *tbl, const char *key, const char *value); @@ -428,15 +440,15 @@ struct table_formatter { }; /** Standard formatter for human-readable output. **/ -extern struct table_formatter table_fmt_human_readable; +extern const struct table_formatter table_fmt_human_readable; /** Standard formatter for machine-readable output (tab-separated values). **/ -extern struct table_formatter table_fmt_machine_readable; +extern const struct table_formatter table_fmt_machine_readable; /** * Standard formatter for block output. Each cell is output on its own line * of the form `column_name: value`. Rows are separated by blank lines. **/ -extern struct table_formatter table_fmt_blockline; +extern const struct table_formatter table_fmt_blockline; #endif