From 50a77a246c1505ebd135c699d973eef0e56194cf Mon Sep 17 00:00:00 2001 From: Robert Kessl Date: Wed, 2 Jul 2014 13:51:43 +0200 Subject: [PATCH] tableprinter: update of user-defined column types - added pointer to a definition of column to struct table_column - added definition of column with size and timestamp. --- ucw/table-types.c | 24 +++++++++++++++++++++--- ucw/table-types.h | 9 +++++---- ucw/table.c | 6 +++--- ucw/table.h | 12 ++++++++++-- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/ucw/table-types.c b/ucw/table-types.c index 75bf53bc..50d3079e 100644 --- a/ucw/table-types.c +++ b/ucw/table-types.c @@ -8,7 +8,7 @@ #include #include -bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char *col_arg, char **err) +static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err) { int col_type_idx = tbl->column_order[col_copy_idx].idx; if(tbl->columns[col_type_idx].type == COL_TYPE_SIZE) { @@ -23,13 +23,25 @@ bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char } else if(strcasecmp(col_arg, "tb") == 0) { tbl->column_order[col_copy_idx].output_type = UNIT_TERABYTE; } else { - *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx); + *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_copy_idx); return true; } *err = NULL; return true; } + *err = NULL; + return false; +} + +struct table_user_type table_type_size = { + .set_col_instance_option = table_set_col_opt_size, + .type = COL_TYPE_SIZE, +}; + +static bool table_set_col_opt_timestamp(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err) +{ + int col_type_idx = tbl->column_order[col_copy_idx].idx; if(tbl->columns[col_type_idx].type == COL_TYPE_TIMESTAMP) { if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) { tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH; @@ -43,9 +55,15 @@ bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char return true; } - return table_set_col_opt_default(tbl, col_copy_idx, col_arg, err); + *err = NULL; + return false; } +struct table_user_type table_type_timestamp = { + .set_col_instance_option = table_set_col_opt_timestamp, + .type = COL_TYPE_TIMESTAMP, +}; + void table_col_size_name(struct table *tbl, const char *col_name, u64 val) { int col = table_get_col_idx(tbl, col_name); diff --git a/ucw/table-types.h b/ucw/table-types.h index 5b264e11..7b8dec5a 100644 --- a/ucw/table-types.h +++ b/ucw/table-types.h @@ -21,8 +21,11 @@ enum timestamp_format { #define COL_TYPE_SIZE COL_TYPE_CUSTOM #define COL_TYPE_TIMESTAMP (COL_TYPE_CUSTOM+1) -#define TBL_COL_SIZE(_name, _width) { .name = _name, .width = _width, .fmt = "%llu", .type = COL_TYPE_SIZE } -#define TBL_COL_TIMESTAMP(_name, _width) { .name = _name, .width = _width, .fmt = "%lld", .type = COL_TYPE_TIMESTAMP } +extern struct table_user_type table_type_timestamp; +extern struct table_user_type table_type_size; + +#define TBL_COL_SIZE(_name, _width) { .name = _name, .width = _width, .fmt = "%llu", .type = COL_TYPE_SIZE, .type_def = &table_type_size } +#define TBL_COL_TIMESTAMP(_name, _width) { .name = _name, .width = _width, .fmt = "%lld", .type = COL_TYPE_TIMESTAMP, .type_def = &table_type_timestamp } #define TBL_COL_SIZE_FMT(_name, _width, _units) { .name = _name, .width = _width, .fmt = "%llu", .type = COL_TYPE_SIZE } #define TBL_COL_TIMESTAMP_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = "%lld", .type = COL_TYPE_TIMESTAMP } @@ -54,8 +57,6 @@ void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val) void table_col_size(struct table *tbl, int col, u64 val); void table_col_timestamp(struct table *tbl, int col, u64 val); -bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char *col_arg, char **err); - //TABLE_COL(size, u64, COL_TYPE_SIZE) //TABLE_COL_STR(size, u64, COL_TYPE_SIZE) diff --git a/ucw/table.c b/ucw/table.c index 048d4589..4ed97f30 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -249,10 +249,10 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde 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 = CELL_OUT_UNINITIALIZED; - if(tbl->formatter && tbl->formatter->set_col_instance_option) { + if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].type_def->set_col_instance_option) { char *err = NULL; - tbl->formatter->set_col_instance_option(tbl, curr_col_idx, arg, &err); - if(err) return err; + tbl->columns[col_idx].type_def->set_col_instance_option(tbl, curr_col_idx, arg, &err); + if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err); } name_start = next; diff --git a/ucw/table.h b/ucw/table.h index 14e84314..43d94f79 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -103,6 +103,15 @@ enum column_type { #define CELL_OUT_HUMAN_READABLE 0 #define CELL_OUT_MACHINE_READABLE 1 +struct table; + +struct table_user_type { + bool (*set_col_instance_option)(struct table *tbl, uint col, const char *value, char **err); + // [*] process table option for a column instance + uint type; // [*] type identifier, should be a number shifted by COL_TYPE_CUSTOM + const char *default_fmt; // [*] default format used for printing +}; + /** * Definition of a single table column. * Usually, this is generated using the `TABLE_COL_`'type' macros. @@ -115,6 +124,7 @@ struct table_column { enum column_type type; // [*] Type of the cells in the column int first_column; int last_column; + struct table_user_type *type_def; }; struct table_col_info { @@ -405,8 +415,6 @@ struct table_formatter { void (*table_end)(struct table *tbl); // [*] table_end callback (optional) bool (*process_option)(struct table *tbl, const char *key, const char *value, const char **err); // [*] Process table option and possibly return an error message (optional) - bool (*set_col_instance_option)(struct table *tbl, uint col, const char *value, char **err); - // [*] process table option for a column instance const char *formats[]; }; -- 2.39.2