From: Robert Kessl Date: Wed, 16 Jul 2014 07:40:19 +0000 (+0200) Subject: tableprinter: started fixing of setting cell values X-Git-Tag: v6.1~3^2~95 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=0be42d4cd68c9b1528a64ec3689d20b358f6c9a6;p=libucw.git tableprinter: started fixing of setting cell values --- diff --git a/ucw/table.c b/ucw/table.c index 51f50b99..6ff2ab2f 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -52,7 +52,7 @@ static struct table *table_make_instance(const struct table_template *tbl_templa new_inst->column_order[i].cell_content = NULL; int col_idx = new_inst->column_order[i].idx; new_inst->column_order[i].col_def = new_inst->columns + col_idx; - new_inst->column_order[i].output_type = tbl_template->column_order[i].output_type; + new_inst->column_order[i].output_type = tbl_template->columns[col_idx].fmt; } new_inst->cols_to_output = tbl_template->cols_to_output; @@ -193,7 +193,7 @@ void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output) tbl->column_order[i].idx = col_idx; tbl->column_order[i].col_def = tbl->columns + col_idx; tbl->column_order[i].cell_content = NULL; - tbl->column_order[i].output_type = XTYPE_FMT_DEFAULT; + tbl->column_order[i].output_type = tbl->columns[col_idx].fmt; } table_update_ll(tbl); } @@ -290,7 +290,7 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx; tbl->column_order[curr_col_idx].idx = col_idx; tbl->column_order[curr_col_idx].cell_content = NULL; - tbl->column_order[curr_col_idx].output_type = XTYPE_FMT_DEFAULT; + tbl->column_order[curr_col_idx].output_type = tbl->columns[col_idx].fmt; if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_instance_option) { char *err = NULL; tbl->columns[col_idx].set_col_instance_option(tbl, curr_col_idx, arg, &err); @@ -407,6 +407,14 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha return "Invalid argument to output-type option."; } return NULL; + } else if(strcmp(key, "cell-fmt") == 0) { + u32 fmt = 0; + const char *err = xtype_parse_fmt(NULL, value, &fmt, tbl->pool); + if(err) return mp_printf(tbl->pool, "??? Invalid cell format: '%s'.", err); + for(uint i = 0; i < tbl->cols_to_output; i++) { + tbl->column_order[i].output_type = fmt; + } + return NULL; } else if(strcmp(key, "col-delim") == 0) { char * d = mp_printf(tbl->pool, "%s", value); tbl->col_delimiter = d; @@ -655,12 +663,12 @@ enum test_any_table_cols { TEST_ANY_COL0_INT, TEST_ANY_COL1_ANY }; -static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL(TEST_ANY_COL1_ANY) }; +static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL_FMT(TEST_ANY_COL1_ANY, XTYPE_FMT_PRETTY) }; static struct table_template test_any_tbl = { TBL_COLUMNS { [TEST_ANY_COL0_INT] = TBL_COL_INT("col0_int", 8), - [TEST_ANY_COL1_ANY] = TBL_COL_ANY("col1_any", 9), + [TEST_ANY_COL1_ANY] = TBL_COL_ANY_FMT("col1_any", 9, XTYPE_FMT_PRETTY), TBL_COL_END }, TBL_COL_ORDER(test_any_column_order), diff --git a/ucw/table.h b/ucw/table.h index 16b234c8..840ebbfe 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -194,7 +194,7 @@ struct table { #define TBL_COL_INTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_INTMAX, TBL_COL_LIST_INIT } #define TBL_COL_UINTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINTMAX, TBL_COL_LIST_INIT } #define TBL_COL_HEXUINT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINT, TBL_COL_LIST_INIT } -#define TBL_COL_DOUBLE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT } +#define TBL_COL_DOUBLE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT } #define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_BOOL, TBL_COL_LIST_INIT } #define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_ANY, TBL_COL_LIST_INIT } #define TBL_COL_CUSTOM(_name, _width, _xtype) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = _xtype, TBL_COL_LIST_INIT } @@ -208,7 +208,8 @@ struct table { #define TBL_COL_UINTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINTMAX, TBL_COL_LIST_INIT } #define TBL_COL_HEXUINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINT, TBL_COL_LIST_INIT } #define TBL_COL_BOOL_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_BOOL, TBL_COL_LIST_INIT } -#define TBL_COL_DOUBLE_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT } +#define TBL_COL_ANY_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_ANY, TBL_COL_LIST_INIT } +#define TBL_COL_DOUBLE_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT } #define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type_def = NULL } @@ -216,7 +217,7 @@ struct table { #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 = XTYPE_FMT_DEFAULT, .next_column = -1, .fmt = _fmt } +#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_OUTPUT_HUMAN_READABLE .formatter = &table_fmt_human_readable @@ -287,11 +288,21 @@ TABLE_COL_PROTO(u64, u64); TABLE_COL_PROTO(bool, bool); /** macros that enables easy definitions of bodies of table_col_ functions **/ +// FIXME: these macros really do not do what they are supposed to do :) +// the problem is the default formatting #define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\ {\ - enum xtype_fmt fmt = tbl->columns[col].fmt;\ - table_col_##_name_##_fmt(tbl, col, fmt, val);\ + ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\ + ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type_def);\ + tbl->last_printed_col = col;\ + tbl->row_printing_started = 1;\ + const char *cell_content = NULL;\ + TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {\ + if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, curr_col->output_type, tbl->pool);\ + else cell_content = (_typeconst_)->format(&val, curr_col->output_type, tbl->pool);\ + curr_col->cell_content = cell_content;\ + } TBL_COL_ITER_END\ } #define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\ @@ -307,9 +318,11 @@ TABLE_COL_PROTO(bool, bool); tbl->last_printed_col = col;\ tbl->row_printing_started = 1;\ const char *cell_content = NULL;\ - if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, fmt, tbl->pool);\ - else cell_content = (_typeconst_)->format(&val, fmt, tbl->pool);\ - table_set_all_inst_content(tbl, col, cell_content);\ + if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, fmt, tbl->pool);\ + else cell_content = (_typeconst_)->format(&val, fmt, tbl->pool);\ + TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {\ + curr_col->cell_content = cell_content;\ + } TBL_COL_ITER_END\ } #define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_); \ diff --git a/ucw/xtypes.c b/ucw/xtypes.c index 18ee5b63..8e2f63ce 100644 --- a/ucw/xtypes.c +++ b/ucw/xtypes.c @@ -27,7 +27,7 @@ const char *xtype_parse_fmt(struct xtype *xt, const char *str, u32 *dest, struct return NULL; } - if (xt->parse_fmt) + if (xt && xt->parse_fmt) return (xt->parse_fmt)(str, dest, pool); else return "Unknown mode";