From d9656daa5fad2bd5e7ef0de655aa94ac0eea9035 Mon Sep 17 00:00:00 2001 From: Robert Kessl Date: Fri, 4 Jul 2014 15:02:24 +0200 Subject: [PATCH] tableprinter: update of iterator macro; bugfix in table_set_col_opt_size --- ucw/table-types.c | 51 ++++++++++++++++++++++++++++------------------- ucw/table.c | 1 - ucw/table.h | 3 ++- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/ucw/table-types.c b/ucw/table-types.c index 8204a936..ab2e5b69 100644 --- a/ucw/table-types.c +++ b/ucw/table-types.c @@ -24,16 +24,22 @@ static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const c return false; } + if(col_arg == NULL) { + *err = NULL; + return true; + } + if(strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) { tbl->column_order[col_copy_idx].output_type = UNIT_BYTE; } tbl->column_order[col_copy_idx].output_type = CELL_OUT_UNINITIALIZED; - for(int i = 0; i < ARRAY_SIZE(unit_suffix); i++) { + for(uint i = 0; i < ARRAY_SIZE(unit_suffix); i++) { if(strcasecmp(col_arg, unit_suffix[i]) == 0) { tbl->column_order[col_copy_idx].output_type = i; } } + if(tbl->column_order[col_copy_idx].output_type == CELL_OUT_UNINITIALIZED) { *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_copy_idx); return true; @@ -51,21 +57,27 @@ struct table_user_type table_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; - } else if(strcasecmp(col_arg, "datetime") == 0) { - tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME; - } else { - *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx); - return true; - } + if(tbl->columns[col_type_idx].type != COL_TYPE_TIMESTAMP) { + *err = NULL; + return false; + } + + if(col_arg == NULL) { *err = NULL; return true; } + if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) { + tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH; + } else if(strcasecmp(col_arg, "datetime") == 0) { + tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME; + } else { + *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx); + return true; + } + *err = NULL; - return false; + return true; } struct table_user_type table_type_timestamp = { @@ -95,18 +107,18 @@ void table_col_size(struct table *tbl, int col, u64 val) [UNIT_TERABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU * 1024LLU) }; - TBL_COL_ITER(tbl, col, curr_col) { + TBL_COL_ITER(tbl, col, curr_col, curr_col_idx) { // FIXME: do some rounding? uint out_type = 0; - if(tbl->column_order[curr_col].output_type == CELL_OUT_UNINITIALIZED) { + if(curr_col->output_type == CELL_OUT_UNINITIALIZED) { val = val / unit_div[UNIT_BYTE]; out_type = 0; } else { - val = val / unit_div[tbl->column_order[curr_col].output_type]; - out_type = tbl->column_order[curr_col].output_type; + val = val / unit_div[curr_col->output_type]; + out_type = curr_col->output_type; } - tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]); + curr_col->cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]); } } @@ -127,9 +139,8 @@ void table_col_timestamp(struct table *tbl, int col, u64 val) time_t tmp_time = (time_t)val; struct tm t = *gmtime(&tmp_time); - - TBL_COL_ITER(tbl, col, curr_col) { - switch (tbl->column_order[curr_col].output_type) { + TBL_COL_ITER(tbl, col, curr_col, curr_col_idx) { + switch (curr_col->output_type) { case TIMESTAMP_EPOCH: case CELL_OUT_UNINITIALIZED: sprintf(formatted_time_buf, "%lu", val); @@ -142,6 +153,6 @@ void table_col_timestamp(struct table *tbl, int col, u64 val) break; } - tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf); + curr_col->cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf); } } diff --git a/ucw/table.c b/ucw/table.c index 915f6b7b..1e141953 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -182,7 +182,6 @@ static char * table_parse_col_arg(char *col_def) left_br++; char *right_br = strchr(left_br, ']'); *right_br = 0; - //*left_br = 0; return left_br; } diff --git a/ucw/table.h b/ucw/table.h index 99368433..95e0e4b9 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -217,7 +217,8 @@ struct table { #define TBL_OUTPUT_BLOCKLINE .formatter = &table_fmt_blockline #define TBL_OUTPUT_MACHINE_READABLE .formatter = &table_fmt_machine_readable -#define TBL_COL_ITER(_tbl, _colidx, _var) for(int _var = _tbl->columns[_colidx].first_column; _var != -1; _var = _tbl->column_order[_var].next_column) +#define TBL_COL_ITER(_tbl, _colidx, _var, _idxval) struct table_col_info *_var = NULL; int _idxval = -1; \ + for(_idxval = _tbl->columns[_idxval].first_column, _var = _tbl->column_order + _idxval; _idxval != -1; _idxval = _tbl->column_order[_idxval].next_column) /** * Initialize a table definition. The structure should already contain -- 2.39.2