From 29db807e9be029beb9eaad2f1d3f8f9cb6aed36c Mon Sep 17 00:00:00 2001 From: Robert Kessl Date: Wed, 9 Jul 2014 13:42:08 +0200 Subject: [PATCH] tableprinter: code cleanup contd. - renamed table_col_info -> table_col_instance - table_init now uses const for struct table_template - some initialization moved from table_init to table_make_instance --- ucw/table-test.c | 2 +- ucw/table.c | 51 ++++++++++++++++++++++++------------------------ ucw/table.h | 17 +++++++--------- 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/ucw/table-test.c b/ucw/table-test.c index e0f18d08..55614f56 100644 --- a/ucw/table-test.c +++ b/ucw/table-test.c @@ -14,7 +14,7 @@ enum test_table_cols { test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double, test_col5_size, test_col6_time }; -static struct table_col_info test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) }; +static struct table_col_instance test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) }; static struct table_template test_tbl = { TBL_COLUMNS { diff --git a/ucw/table.c b/ucw/table.c index d6850352..8d742f97 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -20,15 +20,14 @@ static void table_update_ll(struct table *tbl); /*** Management of tables ***/ -static struct table *table_make_instance(struct table_template *tbl_template) +static struct table *table_make_instance(const struct table_template *tbl_template) { struct table *new_inst = xmalloc_zero(sizeof(struct table)); - new_inst->column_count = tbl_template->column_count; new_inst->pool = mp_new(4096); if(tbl_template->column_order) { - new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_info) * tbl_template->cols_to_output); - memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_info) * tbl_template->cols_to_output); + new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_instance) * tbl_template->cols_to_output); + memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_instance) * tbl_template->cols_to_output); for(uint i = 0; i < new_inst->cols_to_output; i++) { new_inst->column_order[i].cell_content = NULL; new_inst->column_order[i].col_def = NULL; @@ -38,6 +37,23 @@ static struct table *table_make_instance(struct table_template *tbl_template) new_inst->cols_to_output = tbl_template->cols_to_output; } + int col_count = 0; // count the number of columns in the struct table + for(;;) { + if(tbl_template->columns[col_count].name == NULL && + tbl_template->columns[col_count].fmt == NULL && + tbl_template->columns[col_count].width == 0 && + tbl_template->columns[col_count].type == COL_TYPE_LAST) + break; + ASSERT(tbl_template->columns[col_count].name != NULL); + ASSERT(tbl_template->columns[col_count].type == COL_TYPE_ANY || tbl_template->columns[col_count].fmt != NULL); + ASSERT(tbl_template->columns[col_count].width != 0); + + col_count++; + } + new_inst->column_count = col_count; + + 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; @@ -51,27 +67,10 @@ static struct table *table_make_instance(struct table_template *tbl_template) return new_inst; } -struct table *table_init(struct table_template *tbl_template) +struct table *table_init(const struct table_template *tbl_template) { - int col_count = 0; // count the number of columns in the struct table - struct table *tbl = table_make_instance(tbl_template); - for(;;) { - if(tbl->columns[col_count].name == NULL && - tbl->columns[col_count].fmt == NULL && - tbl->columns[col_count].width == 0 && - tbl->columns[col_count].type == COL_TYPE_LAST) - break; - ASSERT(tbl->columns[col_count].name != NULL); - ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl_template->columns[col_count].fmt != NULL); - ASSERT(tbl->columns[col_count].width != 0); - - col_count++; - } - - tbl->column_count = col_count; - if(!tbl->formatter) { tbl->formatter = &table_fmt_human_readable; } @@ -187,7 +186,7 @@ void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output) } tbl->cols_to_output = cols_to_output; - tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * cols_to_output); + tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * cols_to_output); for(int i = 0; i < cols_to_output; i++) { int col_idx = col_order[i]; tbl->column_order[i].idx = col_idx; @@ -271,7 +270,7 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde } tbl->cols_to_output = col_count; - tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * col_count); + tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * col_count); int curr_col_idx = 0; char *name_start = tmp_col_order; @@ -669,7 +668,7 @@ enum test_table_cols { test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double }; -static struct table_col_info test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) }; +static struct table_col_instance test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) }; static struct table_template test_tbl = { TBL_COLUMNS { @@ -763,7 +762,7 @@ enum test_any_table_cols { test_any_col0_int, test_any_col1_any }; -static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) }; +static struct table_col_instance test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) }; static struct table_template test_any_tbl = { TBL_COLUMNS { diff --git a/ucw/table.h b/ucw/table.h index f4163d30..bb523f26 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -125,14 +125,13 @@ struct table_column { const char *fmt; // [*] Default format of each cell in the column enum column_type type; // [*] Type of the cells in the column int first_column; // head of linked list of columns of this type - //int last_column; // tail of linked list of columns of this type struct table_user_type *type_def; }; // FIXME: is it correct to have idx and col_def? idx is sufficient and in fact a duplicity of idx // idx is used only for initialization and col_def is used in other cases -struct table_col_info { - uint idx; // idx is a pointer to struct table::columns +struct table_col_instance { + uint idx; // idx is a index into struct table::columns struct table_column *col_def; // this is pointer to the column definition, located in the array struct table::columns char *cell_content; // content of the cell of the current row int next_column; // index of next column in linked list of columns of the same type @@ -145,8 +144,7 @@ struct table_col_info { **/ struct table_template { struct table_column *columns; // [*] Definition of columns - int column_count; // [*] Number of columns (calculated by table_init()) - struct table_col_info *column_order; // [*] Order of the columns in the print-out of the table + struct table_col_instance *column_order; // [*] Order of the columns in the print-out of the table uint cols_to_output; // [*] Number of columns that are printed const char *col_delimiter; // [*] Delimiter that is placed between columns // Back-end used for table formatting and its private data @@ -166,7 +164,7 @@ struct table { struct mempool_state pool_state; // State of the pool after the table is initialized, i.e., before // per-row data have been allocated. - struct table_col_info *column_order; // [*] Order of the columns in the print-out of the table + struct table_col_instance *column_order; // [*] Order of the columns in the print-out of the table uint cols_to_output; // [*] Number of columns that are printed const char *col_delimiter; // [*] Delimiter that is placed between columns uint print_header; // [*] 0 indicates that table header should not be printed @@ -225,7 +223,7 @@ struct table { #define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type = COL_TYPE_LAST } #define TBL_COLUMNS .columns = (struct table_column []) -#define TBL_COL_ORDER(order) .column_order = (struct table_col_info *) order, .cols_to_output = ARRAY_SIZE(order) +#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 } @@ -235,7 +233,7 @@ struct table { #define TBL_OUTPUT_BLOCKLINE .formatter = &table_fmt_blockline #define TBL_OUTPUT_MACHINE_READABLE .formatter = &table_fmt_machine_readable -#define TBL_COL_ITER_START(_tbl, _colidx, _var, _idxval) { struct table_col_info *_var = NULL; int _idxval = _tbl->columns[_colidx].first_column; \ +#define TBL_COL_ITER_START(_tbl, _colidx, _var, _idxval) { struct table_col_instance *_var = NULL; int _idxval = _tbl->columns[_colidx].first_column; \ for(_idxval = _tbl->columns[_colidx].first_column, _var = _tbl->column_order + _idxval; _idxval != -1; _idxval = _tbl->column_order[_idxval].next_column, _var = _tbl->column_order + _idxval) #define TBL_COL_ITER_END } @@ -244,7 +242,7 @@ struct table { * Creates a new table from a table template. The template should already contain * the definitions of columns. **/ -struct table *table_init(struct table_template *tbl_template); +struct table *table_init(const struct table_template *tbl_template); /** Destroy a table definition, freeing all memory used by it. **/ void table_cleanup(struct table *tbl); @@ -440,7 +438,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) - const char *formats[]; }; /** Standard formatter for human-readable output. **/ -- 2.39.5