From: Robert Kessl Date: Tue, 22 Jul 2014 08:38:03 +0000 (+0200) Subject: tableprinter: update of parsing of column options X-Git-Tag: v6.1~3^2~72 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=c5d58cb79212938d4fb4f2e9f9d34fa8179e569a;p=libucw.git tableprinter: update of parsing of column options --- diff --git a/ucw/table-types.c b/ucw/table-types.c index f516f8f4..b5027817 100644 --- a/ucw/table-types.c +++ b/ucw/table-types.c @@ -55,39 +55,25 @@ static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool) return mp_printf(pool, "%"PRIu64"%s", curr_val, unit_suffix[out_type]); } -int table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err) +static const char * xt_size_fmt_parse(const char *opt_str, u32 *dest, struct mempool *pool UNUSED) { - if(col_arg == NULL) { - *err = "NULL is not supported as a column argument."; - return TABLE_OPT_ERR; + if(opt_str == NULL) { + return "NULL is not supported as a column argument."; } - const struct table_column *col_def = tbl->column_order[col_inst_idx].col_def; - if(col_def->type_def != &xt_size) { - *err = NULL; - return TABLE_OPT_UNKNOWN; + if(strlen(opt_str) == 0 || strcasecmp(opt_str, "b") == 0 || strcasecmp(opt_str, "bytes") == 0) { + *dest = SIZE_UNIT_BYTE | SIZE_UNITS_FIXED; + return NULL; } - if(strlen(col_arg) == 0 || strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) { - tbl->column_order[col_inst_idx].fmt = SIZE_UNIT_BYTE | SIZE_UNITS_FIXED; - *err = NULL; - return TABLE_OPT_PROCESSED; - } - - tbl->column_order[col_inst_idx].fmt = XTYPE_FMT_DEFAULT; // CELL_OUT_UNINITIALIZED; for(uint i = SIZE_UNIT_BYTE; i <= SIZE_UNIT_TERABYTE; i++) { - if(strcasecmp(col_arg, unit_suffix[i]) == 0) { - tbl->column_order[col_inst_idx].fmt = i | SIZE_UNITS_FIXED; + if(strcasecmp(opt_str, unit_suffix[i]) == 0) { + *dest = i | SIZE_UNITS_FIXED; + return NULL; } } - if(tbl->column_order[col_inst_idx].fmt == XTYPE_FMT_DEFAULT) { - *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_inst_idx); - return TABLE_OPT_ERR; - } - - *err = NULL; - return TABLE_OPT_PROCESSED; + return "Unknown option."; } TABLE_COL_BODY(size, u64) @@ -97,38 +83,13 @@ const struct xtype xt_size = { .name = "size", //.parse = xt_size_parse, .format = xt_size_format, + .parse_fmt = xt_size_fmt_parse }; /** xt_timestamp **/ #define FORMAT_TIME_SIZE 20 // Minimum buffer size -int table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err) -{ - int col_type_idx = tbl->column_order[col_inst_idx].idx; - if(tbl->columns[col_type_idx].type_def != &xt_timestamp) { - *err = NULL; - return TABLE_OPT_UNKNOWN; - } - - if(col_arg == NULL) { - *err = "NULL is not supported as a column argument."; - return TABLE_OPT_ERR; - } - - if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) { - tbl->column_order[col_inst_idx].fmt = TIMESTAMP_EPOCH; - } else if(strcasecmp(col_arg, "datetime") == 0) { - tbl->column_order[col_inst_idx].fmt = TIMESTAMP_DATETIME; - } else { - *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_inst_idx); - return TABLE_OPT_ERR; - } - - *err = NULL; - return TABLE_OPT_PROCESSED; -} - static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool) { char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 }; @@ -152,6 +113,23 @@ static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool) return mp_printf(pool, "%s", formatted_time_buf); } +static const char * xt_timestamp_fmt_parse(const char *opt_str, u32 *dest, struct mempool *pool) +{ + if(opt_str == NULL) { + return "NULL is not supported as a column argument."; + } + + if(strcasecmp(opt_str, "timestamp") == 0 || strcasecmp(opt_str, "epoch") == 0) { + *dest = TIMESTAMP_EPOCH; + return NULL; + } else if(strcasecmp(opt_str, "datetime") == 0) { + *dest = TIMESTAMP_DATETIME; + return NULL; + } + + return mp_printf(pool, "Invalid column format option: '%s'.", opt_str); +} + TABLE_COL_BODY(timestamp, u64) const struct xtype xt_timestamp = { @@ -159,4 +137,5 @@ const struct xtype xt_timestamp = { .name = "timestamp", //.parse = xt_timestamp_parse, .format = xt_timestamp_format, + .parse_fmt = xt_timestamp_fmt_parse }; diff --git a/ucw/table-types.h b/ucw/table-types.h index e7b4366b..79b9e19e 100644 --- a/ucw/table-types.h +++ b/ucw/table-types.h @@ -26,14 +26,11 @@ enum size_units { extern const struct xtype xt_size; extern const struct xtype xt_timestamp; -int table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err); -int table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err); +#define TBL_COL_SIZE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_size, .set_col_instance_option = table_set_col_opt_default } +#define TBL_COL_TIMESTAMP(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_timestamp, .set_col_instance_option = table_set_col_opt_default } -#define TBL_COL_SIZE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_size, .set_col_instance_option = table_set_col_opt_size } -#define TBL_COL_TIMESTAMP(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_timestamp, .set_col_instance_option = table_set_col_opt_timestamp } - -#define TBL_COL_SIZE_FMT(_name, _width, _units) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_size, .set_col_instance_option = table_set_col_opt_size } -#define TBL_COL_TIMESTAMP_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_timestamp, .set_col_instance_option = table_set_col_opt_timestamp } +#define TBL_COL_SIZE_FMT(_name, _width, _units) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_size, .set_col_instance_option = table_set_col_opt_default } +#define TBL_COL_TIMESTAMP_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_timestamp, .set_col_instance_option = table_set_col_opt_default} TABLE_COL_PROTO(size, u64) diff --git a/ucw/table.c b/ucw/table.c index cf994177..90988d21 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -218,23 +218,19 @@ static char * table_parse_col_arg(char *col_def) /** * Setting options for basic table types (as defined in table.h) **/ -int table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char **err) +const char *table_set_col_opt_default(struct table *tbl, uint col_idx, const char *col_opt) { const struct table_column *col_def = tbl->column_order[col_idx].col_def; - if(col_def->type_def == &xt_double) { - uint precision = 0; - const char *tmp_err = str_to_uint(&precision, col_arg, NULL, 0); - if(tmp_err) { - *err = mp_printf(tbl->pool, "An error occured while parsing precision: %s.", tmp_err); - return false; - } - tbl->column_order[col_idx].fmt = precision; // FIXME: shift the value of precision - return true; + if(col_def && col_def->type_def && col_def->type_def->parse_fmt) { + uint fmt = 0; + const char *tmp_err = col_def->type_def->parse_fmt(col_opt, &fmt, tbl->pool); + if(tmp_err) return tmp_err; + tbl->column_order[col_idx].fmt = fmt; + return NULL; } - *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_idx); - return false; + return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_idx); } /** @@ -284,8 +280,8 @@ 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].fmt = 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); + const char *err = NULL; + err = tbl->columns[col_idx].set_col_instance_option(tbl, curr_col_idx, arg); if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err); } diff --git a/ucw/table.h b/ucw/table.h index 57640477..4f63e827 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -93,12 +93,6 @@ struct table; -enum table_option_state { - TABLE_OPT_PROCESSED, - TABLE_OPT_UNKNOWN, - TABLE_OPT_ERR -}; - /** * Definition of a single table column. * Usually, this is generated using the `TABLE_COL_`'type' macros. @@ -110,7 +104,7 @@ struct table_column { uint fmt; // [*] default format of the column const struct xtype *type_def; // [*] pointer to xtype of this column - int (*set_col_instance_option)(struct table *tbl, uint col, const char *value, char **err); + const char * (*set_col_instance_option)(struct table *tbl, uint col, const char *value); // [*] process table option for a column instance }; @@ -346,7 +340,7 @@ int table_get_col_idx(struct table *tbl, const char *col_name); /** * Sets a string argument to a column instance **/ -int table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char ** err); +const char *table_set_col_opt_default(struct table *tbl, uint col_idx, const char *col_opt); /** * Returns a comma-and-space-separated list of column names, allocated from table's internal diff --git a/ucw/xtypes-basic.c b/ucw/xtypes-basic.c index 9bca1436..4ee14cfa 100644 --- a/ucw/xtypes-basic.c +++ b/ucw/xtypes-basic.c @@ -77,7 +77,26 @@ static const char *xt_double_parse(const char *str, void *dest, struct mempool * return NULL; } -XTYPE_NUM_STRUCT(double, double) +static const char * xt_double_fmt_parse(const char *str, u32 *dest, struct mempool *pool) +{ + uint precision = 0; + const char *tmp_err = str_to_uint(&precision, str, NULL, 0); + if(tmp_err) { + return mp_printf(pool, "An error occured while parsing precision: %s.", tmp_err); + } + + *dest = XTYPE_FMT_DBL_FIXED_PREC(precision); + + return NULL; +} + +const struct xtype xt_double = { + .size = sizeof(double), + .name = "double", + .parse = xt_double_parse, + .format = xt_double_format, + .parse_fmt = xt_double_fmt_parse +}; /* bool */ diff --git a/ucw/xtypes.h b/ucw/xtypes.h index 840713be..5a42e465 100644 --- a/ucw/xtypes.h +++ b/ucw/xtypes.h @@ -99,7 +99,7 @@ extern const struct xtype xt_bool; extern const struct xtype xt_double; -#define XTYPE_FMT_DBL_PREC 0x40000000 -#define XTYPE_FMT_DBL_FIXED_PREC(_prec) (_prec | XTYPE_FMT_DBL_PREC) +#define XTYPE_FMT_DBL_PREC XTYPE_FMT_CUSTOM +#define XTYPE_FMT_DBL_FIXED_PREC(_prec) (_prec | XTYPE_FMT_CUSTOM) #endif