From f6e91541c3d3818541f506d57d253f7d9afbeab3 Mon Sep 17 00:00:00 2001 From: Robert Kessl Date: Wed, 9 Jul 2014 16:12:23 +0200 Subject: [PATCH] tableprinter: code cleanup contd. - update of cell output types: CELL_OUT_UNINITIALIZED is -1, CELL_OUT_HUMAN_READABLE is now -2 and CELL_OUT_MACHINE_READABLE -3 - table_col_bool* now uses bool instead of uint - other code cleanup --- ucw/table-types.c | 28 ++++++++++++++-------------- ucw/table-types.h | 2 +- ucw/table.c | 36 +++++++++++++++++------------------- ucw/table.h | 15 ++++++++------- 4 files changed, 40 insertions(+), 41 deletions(-) diff --git a/ucw/table-types.c b/ucw/table-types.c index e44403ab..bdcf63b1 100644 --- a/ucw/table-types.c +++ b/ucw/table-types.c @@ -14,29 +14,29 @@ static const char *unit_suffix[] = { [UNIT_SIZE_TERABYTE] = "TB" }; -static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err) +static bool table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err) { - struct table_column *col_def = tbl->column_order[col_copy_idx].col_def; + struct table_column *col_def = tbl->column_order[col_inst_idx].col_def; if(col_def->type != COL_TYPE_SIZE) { *err = NULL; return false; } if(col_arg == NULL || strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) { - tbl->column_order[col_copy_idx].output_type = UNIT_SIZE_BYTE; + tbl->column_order[col_inst_idx].output_type = UNIT_SIZE_BYTE; *err = NULL; return true; } - tbl->column_order[col_copy_idx].output_type = CELL_OUT_UNINITIALIZED; - for(uint i = 0; i < ARRAY_SIZE(unit_suffix); i++) { + tbl->column_order[col_inst_idx].output_type = CELL_OUT_UNINITIALIZED; + for(uint i = UNIT_SIZE_BYTE; i <= UNIT_SIZE_TERABYTE; i++) { if(strcasecmp(col_arg, unit_suffix[i]) == 0) { - tbl->column_order[col_copy_idx].output_type = i; + tbl->column_order[col_inst_idx].output_type = i; } } - if(tbl->column_order[col_copy_idx].output_type == CELL_OUT_UNINITIALIZED) { - *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_copy_idx); + if(tbl->column_order[col_inst_idx].output_type == CELL_OUT_UNINITIALIZED) { + *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_inst_idx); return true; } @@ -49,9 +49,9 @@ struct table_user_type table_type_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) +static bool 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_copy_idx].idx; + int col_type_idx = tbl->column_order[col_inst_idx].idx; if(tbl->columns[col_type_idx].type != COL_TYPE_TIMESTAMP) { *err = NULL; return false; @@ -63,11 +63,11 @@ static bool table_set_col_opt_timestamp(struct table *tbl, uint col_copy_idx, co } if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) { - tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH; + tbl->column_order[col_inst_idx].output_type = TIMESTAMP_EPOCH; } else if(strcasecmp(col_arg, "datetime") == 0) { - tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME; + tbl->column_order[col_inst_idx].output_type = TIMESTAMP_DATETIME; } else { - *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_copy_idx); + *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_inst_idx); return true; } @@ -108,7 +108,7 @@ void table_col_size(struct table *tbl, int col, u64 val) u64 curr_val = val; if(curr_col->output_type == CELL_OUT_UNINITIALIZED) { curr_val = curr_val / unit_div[UNIT_SIZE_BYTE]; - out_type = 0; + out_type = UNIT_SIZE_BYTE; } else { curr_val = curr_val / unit_div[curr_col->output_type]; out_type = curr_col->output_type; diff --git a/ucw/table-types.h b/ucw/table-types.h index 6e53c8ae..a5bfd61e 100644 --- a/ucw/table-types.h +++ b/ucw/table-types.h @@ -10,7 +10,7 @@ #include enum size_units { - UNIT_SIZE_BYTE, + UNIT_SIZE_BYTE = CELL_OUT_USER_DEF_START, UNIT_SIZE_KILOBYTE, UNIT_SIZE_MEGABYTE, UNIT_SIZE_GIGABYTE, diff --git a/ucw/table.c b/ucw/table.c index 8d742f97..a8749a51 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -54,7 +54,6 @@ static struct table *table_make_instance(const struct table_template *tbl_templa new_inst->columns = mp_alloc_zero(new_inst->pool, sizeof(struct table_column) * new_inst->column_count); memcpy(new_inst->columns, tbl_template->columns, sizeof(struct table_column) * new_inst->column_count); - new_inst->columns = tbl_template->columns; // FIXME: copy also columns, if there will be two instances of table, then there will be clash between the linked lists! new_inst->col_delimiter = tbl_template->col_delimiter; new_inst->print_header = 1; @@ -384,6 +383,10 @@ TABLE_COL_BODIES(u64, u64, COL_TYPE_U64, 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(bool, bool, COL_TYPE_BOOL); +TABLE_COL_STR(bool, bool, COL_TYPE_BOOL); + #undef TABLE_COL #undef TABLE_COL_FMT #undef TABLE_COL_STR @@ -399,27 +402,24 @@ void table_col_double_fmt(struct table *tbl, int col, const char *fmt, double va char *cell_content = mp_printf(tbl->pool, fmt, val); int curr_col = tbl->columns[col].first_column; while(curr_col != -1) { - if(tbl->column_order[curr_col].output_type < 0) tbl->column_order[curr_col].cell_content = cell_content; - else { - char *cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val); - tbl->column_order[curr_col].cell_content = cell_content_tmp; + char *cell_content_tmp = NULL; + switch(tbl->column_order[curr_col].output_type) { + case CELL_OUT_UNINITIALIZED: + cell_content_tmp = cell_content; + break; + case CELL_OUT_MACHINE_READABLE: + cell_content_tmp = mp_printf(tbl->pool, "%4lf", val); + break; + default: + cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val); + break; } + tbl->column_order[curr_col].cell_content = cell_content_tmp; curr_col = tbl->column_order[curr_col].next_column; } } -void table_col_bool(struct table *tbl, int col, uint val) -{ - table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val); -} - -void table_col_bool_name(struct table *tbl, const char *col_name, uint val) -{ - int col = table_get_col_idx(tbl, col_name); - table_col_bool(tbl, col, val); -} - -void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val) +void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, bool val) { ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col); ASSERT(COL_TYPE_BOOL == tbl->columns[col].type); @@ -656,8 +656,6 @@ struct table_formatter table_fmt_blockline = { .table_start = table_start_blockline }; - - /*** Tests ***/ #ifdef TEST diff --git a/ucw/table.h b/ucw/table.h index bb523f26..cb295bad 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -102,8 +102,9 @@ enum column_type { #define CELL_WIDTH_MASK (~CELL_FLAG_MASK) #define CELL_OUT_UNINITIALIZED -1 -#define CELL_OUT_HUMAN_READABLE 0 -#define CELL_OUT_MACHINE_READABLE 1 +#define CELL_OUT_HUMAN_READABLE -2 +#define CELL_OUT_MACHINE_READABLE -3 +#define CELL_OUT_USER_DEF_START 5 struct table; @@ -225,8 +226,8 @@ struct table { #define TBL_COLUMNS .columns = (struct table_column []) #define TBL_COL_ORDER(order) .column_order = (struct table_col_instance *) order, .cols_to_output = ARRAY_SIZE(order) #define TBL_COL_DELIMITER(_delimiter_) .col_delimiter = _delimiter_ -#define TBL_COL(_idx) { .idx = _idx, .output_type = -1, .next_column = -1 } -#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = -1, .next_column = -1, .fmt = _fmt } +#define TBL_COL(_idx) { .idx = _idx, .output_type = CELL_OUT_UNINITIALIZED, .next_column = -1 } +#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = CELL_OUT_UNINITIALIZED, .next_column = -1, .fmt = _fmt } #define TBL_COL_TYPE(_idx, _type) { .idx = _idx, .output_type = _type, .next_column = -1 } #define TBL_OUTPUT_HUMAN_READABLE .formatter = &table_fmt_human_readable @@ -295,9 +296,9 @@ TABLE_COL_PROTO(uintmax, uintmax_t); TABLE_COL_PROTO(s64, s64); TABLE_COL_PROTO(u64, u64); -void table_col_bool(struct table *tbl, int col, uint val); -void table_col_bool_name(struct table *tbl, const char *col_name, uint val); -void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val); +void table_col_bool(struct table *tbl, int col, bool val); +void table_col_bool_name(struct table *tbl, const char *col_name, bool val); +void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, bool val); #undef TABLE_COL_PROTO /** -- 2.39.2