From: Robert Kessl Date: Tue, 15 Jul 2014 11:06:24 +0000 (+0200) Subject: tableprinter: update of TABLE_COL_BODIES macro, code cleanup X-Git-Tag: v6.1~3^2~97 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=19dcf6467986a5cc7c6b20ee07541260ad1da60f;p=libucw.git tableprinter: update of TABLE_COL_BODIES macro, code cleanup - TABLE_COL_BODIES macros moved to table.h - removed undef of TABLE_COL_PROTO in table.h - code cleanup - added xtypes.h to header list in Makefile --- diff --git a/ucw/Makefile b/ucw/Makefile index 6bc4f26f..9a716946 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -69,7 +69,8 @@ LIBUCW_MAIN_INCLUDES= \ signames.h \ sighandler.h \ opt.h \ - table.h table-types.h + table.h table-types.h \ + xtypes.h ifdef CONFIG_UCW_THREADS # Some modules require threading diff --git a/ucw/table.c b/ucw/table.c index c5d59a58..4896b6f3 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -311,9 +311,6 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde static void table_set_all_inst_content(struct table *tbl, int col_templ, const char *col_content) { TBL_COL_ITER_START(tbl, col_templ, curr_col_ptr, curr_col) { - //if( override == 0 ) { - //die("Error while setting content of all cells of a single type column, cell format should not be overriden."); - //} curr_col_ptr->cell_content = col_content; } TBL_COL_ITER_END } @@ -330,58 +327,24 @@ void table_col_printf(struct table *tbl, int col, const char *fmt, ...) va_end(args); } -#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);\ - } - -#define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\ - {\ - int col = table_get_col_idx(tbl, col_name);\ - table_col_##_name_(tbl, col, val);\ - } - -#define TABLE_COL_FMT(_name_, _type_, _typeconst_, _override) void table_col_##_name_##_fmt(struct table *tbl, int col, enum xtype_fmt fmt, _type_ 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;\ - 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);\ - } - -#define TABLE_COL_BODIES(_name_, _type_, _typeconst_, _override) TABLE_COL(_name_, _type_, _typeconst_); \ - TABLE_COL_STR(_name_, _type_, _typeconst_);\ - TABLE_COL_FMT(_name_, _type_, _typeconst_, _override); - -TABLE_COL_BODIES(int, int, COL_TYPE_INT, 0) -TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT, 0) -TABLE_COL_BODIES(str, const char *, COL_TYPE_STR, 1) -TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX, 0) -TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX, 0) -TABLE_COL_BODIES(s64, s64, COL_TYPE_S64, 0) -TABLE_COL_BODIES(u64, u64, COL_TYPE_U64, 0) -TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE, 0) -//TABLE_COL_BODIES(bool, bool, COL_TYPE_BOOL, 0) - -// column type double is a special case -//TABLE_COL(double, double, COL_TYPE_DOUBLE); -//TABLE_COL_STR(double, double, COL_TYPE_DOUBLE); +TABLE_COL_BODIES(int, int, COL_TYPE_INT) +TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT) +TABLE_COL_BODIES(str, const char *, COL_TYPE_STR) +TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX) +TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX) +TABLE_COL_BODIES(s64, s64, COL_TYPE_S64) +TABLE_COL_BODIES(u64, u64, COL_TYPE_U64) +TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE) TABLE_COL(bool, bool, COL_TYPE_BOOL) TABLE_COL_STR(bool, bool, COL_TYPE_BOOL) -TABLE_COL_FMT(bool, bool, COL_TYPE_BOOL, 0) +TABLE_COL_FMT(bool, bool, COL_TYPE_BOOL) #undef TABLE_COL #undef TABLE_COL_FMT #undef TABLE_COL_STR #undef TABLE_COL_BODIES - void table_reset_row(struct table *tbl) { for(uint i = 0; i < tbl->cols_to_output; i++) { diff --git a/ucw/table.h b/ucw/table.h index 89684c75..49b7fbb3 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -197,6 +197,7 @@ struct table { #define TBL_COL_DOUBLE(_name, _width, _prec) { .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 } #define TBL_COL_STR_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_STR, TBL_COL_LIST_INIT } #define TBL_COL_INT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_INT, TBL_COL_LIST_INIT } @@ -284,7 +285,39 @@ TABLE_COL_PROTO(s64, s64); TABLE_COL_PROTO(u64, u64); TABLE_COL_PROTO(bool, bool); -#undef TABLE_COL_PROTO +/** macros that enables easy definitions of bodies of table_col_ functions **/ + +#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);\ + } + +#define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\ + {\ + int col = table_get_col_idx(tbl, col_name);\ + table_col_##_name_(tbl, col, val);\ + } + +#define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, enum xtype_fmt fmt, _type_ 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;\ + 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);\ + } + +#define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_); \ + TABLE_COL_STR(_name_, _type_, _typeconst_);\ + TABLE_COL_FMT(_name_, _type_, _typeconst_); + +// FIXME: the next line was removed, the question is whether it should +// be there currently it is impossible to undef the macro. +// #undef TABLE_COL_PROTO /** * Set a particular cell of the current row to a string formatted diff --git a/ucw/xtypes-basic.c b/ucw/xtypes-basic.c index 04380457..cf3010b5 100644 --- a/ucw/xtypes-basic.c +++ b/ucw/xtypes-basic.c @@ -13,6 +13,7 @@ #include #include #include +#include #define XTYPE_NUM_FORMAT(_type, _fmt, _typename) static const char *xt_##_typename##_format(void *src, u32 fmt UNUSED, struct mempool *pool) \ {\ @@ -36,10 +37,10 @@ XTYPE_NUM_STRUCT(_type, _typename) XTYPE_NUM_DEF(int, "%d", int) -XTYPE_NUM_DEF(s64, PRId64, s64) +XTYPE_NUM_DEF(s64, "%" PRId64, s64) XTYPE_NUM_DEF(intmax_t, "%jd", intmax) XTYPE_NUM_DEF(uint, "%u", uint) -XTYPE_NUM_DEF(u64, PRIu64, u64) +XTYPE_NUM_DEF(u64, "%" PRIu64, u64) XTYPE_NUM_DEF(uintmax_t, "%ju", uintmax) /* double */ @@ -127,9 +128,9 @@ const struct xtype xt_bool = { .format = xt_bool_format, }; - /* str */ -static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool) // (struct table *tbl, int col, enum xtype_fmt fmt, bool val) + +static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool) { return mp_printf(pool, "%s", *((char **) src)); }