From: Robert Kessl Date: Wed, 16 Jul 2014 08:58:06 +0000 (+0200) Subject: tableprinter: updated table_col_ funcs, update of mem allocation X-Git-Tag: v6.1~3^2~94 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=f46cd54513203eb5ceb36831b379691fbca9189a;p=libucw.git tableprinter: updated table_col_ funcs, update of mem allocation --- diff --git a/ucw/table.c b/ucw/table.c index 6ff2ab2f..af805f99 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -23,9 +23,9 @@ static void table_update_ll(struct table *tbl); static struct table *table_make_instance(const struct table_template *tbl_template) { struct mempool *pool = mp_new(4096); - struct table *new_inst = mp_alloc_zero(pool, sizeof(struct table)); // FIXME: update allocation to the weird schema made by pchar and mj? + struct table *new_inst = mp_alloc_zero(pool, sizeof(struct table)); - new_inst->pool = mp_new(4096); + new_inst->pool = pool; // initialize column definitions int col_count = 0; // count the number of columns in the struct table @@ -84,7 +84,6 @@ struct table *table_init(const struct table_template *tbl_template) void table_cleanup(struct table *tbl) { mp_delete(tbl->pool); - memset(tbl, 0, sizeof(struct table)); } // TODO: test default column order @@ -315,6 +314,20 @@ static void table_set_all_inst_content(struct table *tbl, int col_templ, const c } TBL_COL_ITER_END } +void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type) +{ + ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col); + ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == 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) { + enum xtype_fmt fmt = curr_col->output_type; + cell_content = expected_type->format(value, fmt, tbl->pool); + curr_col->cell_content = cell_content; + } TBL_COL_ITER_END +} + void table_col_printf(struct table *tbl, int col, const char *fmt, ...) { ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col); @@ -327,18 +340,18 @@ void table_col_printf(struct table *tbl, int col, const char *fmt, ...) va_end(args); } -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_BODY(int, int) +TABLE_COL_BODY(uint, uint) +TABLE_COL_BODY(double, double) +TABLE_COL_BODY(intmax, intmax_t) +TABLE_COL_BODY(uintmax, uintmax_t) +TABLE_COL_BODY(s64, s64) +TABLE_COL_BODY(u64, u64) +TABLE_COL_BODY(bool, bool) -TABLE_COL(bool, bool, COL_TYPE_BOOL) -TABLE_COL_STR(bool, bool, COL_TYPE_BOOL) -TABLE_COL_FMT(bool, bool, COL_TYPE_BOOL) +void table_col_str(struct table *tbl, int col, const char *val) { + table_col_generic_format(tbl, col, (void*)val, &xt_str); +} void table_reset_row(struct table *tbl) { @@ -595,7 +608,7 @@ static void do_print1(struct table *test_tbl) table_col_int(test_tbl, test_col1_int, 10000); table_col_uint(test_tbl, test_col2_uint, 10); table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222); - table_col_bool(test_tbl, test_col3_bool, 1); + table_col_bool(test_tbl, test_col3_bool, true); table_col_double(test_tbl, test_col4_double, 1.5); table_col_printf(test_tbl, test_col4_double, "AAA"); table_end_row(test_tbl); @@ -603,7 +616,7 @@ static void do_print1(struct table *test_tbl) table_col_str(test_tbl, test_col0_str, "test"); table_col_int(test_tbl, test_col1_int, -100); table_col_uint(test_tbl, test_col2_uint, 100); - table_col_bool(test_tbl, test_col3_bool, 0); + table_col_bool(test_tbl, test_col3_bool, false); table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5); table_end_row(test_tbl); } diff --git a/ucw/table.h b/ucw/table.h index 840ebbfe..61fe6d48 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -269,69 +269,31 @@ void table_end(struct table *tbl); * recently accessed cell. ***/ -#define TABLE_COL_PROTO(_name_, _type_) void table_col_##_name_(struct table *tbl, int col, _type_ val);\ - void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val);\ - void table_col_##_name_##_fmt(struct table *tbl, int col, enum xtype_fmt fmt, _type_ val); + +#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_ -TABLE_COL_PROTO(int, int); -TABLE_COL_PROTO(uint, uint); -TABLE_COL_PROTO(double, double); -TABLE_COL_PROTO(str, const char *); -TABLE_COL_PROTO(intmax, intmax_t); -TABLE_COL_PROTO(uintmax, uintmax_t); -TABLE_COL_PROTO(s64, s64); -TABLE_COL_PROTO(u64, u64); -TABLE_COL_PROTO(bool, bool); +TABLE_COL_PROTO(int, int) +TABLE_COL_PROTO(uint, uint) +TABLE_COL_PROTO(double, double) +TABLE_COL_PROTO(intmax, intmax_t) +TABLE_COL_PROTO(uintmax, uintmax_t) +TABLE_COL_PROTO(s64, s64) +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)\ - {\ - 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\ - } +void table_col_str(struct table *tbl, int col, const char * 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);\ - } +/** macros that enables easy definitions of bodies of table_col_ functions **/ -#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);\ - TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {\ - curr_col->cell_content = cell_content;\ - } TBL_COL_ITER_END\ +#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_);\ } -#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 +void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type); /** * 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 abbc6700..ac8727d0 100644 --- a/ucw/xtypes-basic.c +++ b/ucw/xtypes-basic.c @@ -132,7 +132,7 @@ const struct xtype xt_bool = { static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool) { - return mp_printf(pool, "%s", *((char **) src)); + return mp_printf(pool, "%s", (const char *) src); } static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED)