From: Robert Kessl Date: Mon, 21 Jul 2014 10:38:48 +0000 (+0200) Subject: tableprinter: code cleanup, added options X-Git-Tag: v6.1~3^2~76 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=b9f18336270ad857eeed9e114fb059fddda7765b;p=libucw.git tableprinter: code cleanup, added options - renamed TBL_OUTPUT_* -> TBL_FMT_* - added "raw" and "pretty" table option - added "cells" table option --- diff --git a/ucw/table-test-2.c b/ucw/table-test-2.c index b31295d1..88fec5e2 100644 --- a/ucw/table-test-2.c +++ b/ucw/table-test-2.c @@ -20,7 +20,7 @@ static struct table_template test_tbl = { [TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20), TBL_COL_END }, - TBL_OUTPUT_HUMAN_READABLE, + TBL_FMT_HUMAN_READABLE, }; static void do_test(void) @@ -72,7 +72,7 @@ static struct table_template test_tbl2 = { [TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20), TBL_COL_END }, - TBL_OUTPUT_HUMAN_READABLE, + TBL_FMT_HUMAN_READABLE, }; static void do_test2(void) diff --git a/ucw/table-test-align.c b/ucw/table-test-align.c index eb22f2e2..b1d16a97 100644 --- a/ucw/table-test-align.c +++ b/ucw/table-test-align.c @@ -23,7 +23,7 @@ static struct table_template test_tbl = { [test_col4_double] = TBL_COL_DOUBLE_FMT("col4_double", 11 | CELL_ALIGN_LEFT, XTYPE_FMT_DEFAULT), TBL_COL_END }, - TBL_OUTPUT_HUMAN_READABLE, + TBL_FMT_HUMAN_READABLE, TBL_COL_DELIMITER("\t"), }; diff --git a/ucw/table-test.c b/ucw/table-test.c index f23c176d..7b6e581d 100644 --- a/ucw/table-test.c +++ b/ucw/table-test.c @@ -28,7 +28,7 @@ static struct table_template test_tbl = { TBL_COL_END }, TBL_COL_ORDER(test_column_order), - TBL_OUTPUT_HUMAN_READABLE, + TBL_FMT_HUMAN_READABLE, TBL_COL_DELIMITER("\t"), }; @@ -43,7 +43,7 @@ static struct table_template test_default_order_tbl = { [TEST_DEFAULT_ORDER_COL2_INT] = TBL_COL_INT("col2_int", 9), TBL_COL_END }, - TBL_OUTPUT_HUMAN_READABLE, + TBL_FMT_HUMAN_READABLE, TBL_COL_DELIMITER("\t"), }; diff --git a/ucw/table.c b/ucw/table.c index f6d0cba0..4b41ae20 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -314,11 +314,9 @@ void table_col_generic_format(struct table *tbl, int col, void *value, const str 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; + curr_col->cell_content = expected_type->format(value, fmt, tbl->pool); } TBL_COL_ITER_END } @@ -342,10 +340,7 @@ TABLE_COL_BODY(uintmax, uintmax_t) TABLE_COL_BODY(s64, s64) TABLE_COL_BODY(u64, u64) TABLE_COL_BODY(bool, bool) - -void table_col_str(struct table *tbl, int col, const char *val) { - table_col_generic_format(tbl, col, (void*)val, &xt_str); -} +TABLE_COL_BODY(str, const char *) void table_reset_row(struct table *tbl) { @@ -414,7 +409,7 @@ 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) { + } else if(strcmp(key, "cells") == 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); @@ -422,6 +417,14 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha tbl->column_order[i].output_type = fmt; } return NULL; + } else if(strcmp(key, "raw") == 0 || strcmp(key, "pretty") == 0) { + u32 fmt = 0; + const char *err = xtype_parse_fmt(NULL, key, &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; @@ -588,7 +591,7 @@ static struct table_template test_tbl = { TBL_COL_END }, TBL_COL_ORDER(test_column_order), - TBL_OUTPUT_HUMAN_READABLE, + TBL_FMT_HUMAN_READABLE, TBL_COL_DELIMITER("\t"), }; @@ -679,7 +682,7 @@ static struct table_template test_any_tbl = { TBL_COL_END }, TBL_COL_ORDER(test_any_column_order), - TBL_OUTPUT_HUMAN_READABLE, + TBL_FMT_HUMAN_READABLE, TBL_COL_DELIMITER("\t"), }; diff --git a/ucw/table.h b/ucw/table.h index 9273138e..6b3f3765 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -176,9 +176,9 @@ struct table { * * `TBL_COL_END` ends the column definitions * * `TBL_COL_ORDER` specifies custom ordering of columns in the output * * `TBL_COL_DELIMITER` and `TBL_APPEND_DELIMITER` override default delimiters - * * `TBL_OUTPUT_HUMAN_READABLE` requests human-readable formatting (this is the default) - * * `TBL_OUTPUT_MACHINE_READABLE` requests machine-readable TSV output - * * `TBL_OUTPUT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line) + * * `TBL_FMT__HUMAN_READABLE` requests human-readable formatting (this is the default) + * * `TBL_FMT_MACHINE_READABLE` requests machine-readable TSV output + * * `TBL_FMT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line) * ***/ @@ -219,9 +219,9 @@ struct table { #define TBL_COL(_idx) { .idx = _idx, .output_type = XTYPE_FMT_DEFAULT, .next_column = -1 } #define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = _fmt, .next_column = -1 } -#define TBL_OUTPUT_HUMAN_READABLE .formatter = &table_fmt_human_readable -#define TBL_OUTPUT_BLOCKLINE .formatter = &table_fmt_blockline -#define TBL_OUTPUT_MACHINE_READABLE .formatter = &table_fmt_machine_readable +#define TBL_FMT_HUMAN_READABLE .formatter = &table_fmt_human_readable +#define TBL_FMT_BLOCKLINE .formatter = &table_fmt_blockline +#define TBL_FMT_MACHINE_READABLE .formatter = &table_fmt_machine_readable /** * The TBL_COL_ITER_START macro are used for iterating over all instances of a particular column in @@ -272,8 +272,6 @@ void table_end(struct table *tbl); * a custom printf-like format string * * `table_col_`'type'`_name(table, name, value)` refers to the column * by its name instead of its index. - * * `table_append_`'type'`(table, value)` appends a value to the most - * recently accessed cell. ***/ @@ -401,7 +399,7 @@ void table_set_formatter(struct table *tbl, const struct table_formatter *fmt); * | `cols` | comma-separated column list | set order of columns * | `fmt` | `human`/`machine`/`block` | set table formatter to one of the built-in formatters * | `col-delim`| string | set column delimiter -* | `cell-fmt` | string | set column format type +* | `cells` | string | set column format mode * |=================================================================================================== **/ const char *table_set_option_value(struct table *tbl, const char *key, const char *value); diff --git a/ucw/xtypes-basic.c b/ucw/xtypes-basic.c index 5cd74adb..2d365078 100644 --- a/ucw/xtypes-basic.c +++ b/ucw/xtypes-basic.c @@ -70,19 +70,14 @@ static const char *xt_double_parse(const char *str, void *dest, struct mempool * errno = 0; double result = strtod(str, &endptr); if(*endptr != 0 || endptr == str) return "Could not parse double."; - if(errno == ERANGE) return "Could not parse double: overflow happend during parsing."; + if(errno == ERANGE) return "Could not parse double."; *((double *) dest) = result; return NULL; } -const struct xtype xt_double = { - .size = sizeof(double), - .name = "double", - .parse = xt_double_parse, - .format = xt_double_format, -}; +XTYPE_NUM_STRUCT(double, double) /* bool */ @@ -127,18 +122,13 @@ static const char *xt_bool_parse(const char *str, void *dest, struct mempool *po return "Could not parse bool."; } -const struct xtype xt_bool = { - .size = sizeof(bool), - .name = "bool", - .parse = xt_bool_parse, - .format = xt_bool_format, -}; +XTYPE_NUM_STRUCT(bool, bool) /* str */ static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool) { - return mp_strdup(pool, (const char *) src); + return mp_strdup(pool, *((const char **) src)); } static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED) @@ -147,9 +137,4 @@ static const char *xt_str_parse(const char *str, void *dest, struct mempool *poo return NULL; } -const struct xtype xt_str = { - .size = sizeof(char *), - .name = "str", - .parse = xt_str_parse, - .format = xt_str_format, -}; +XTYPE_NUM_STRUCT(char *, str)