From: Robert Kessl Date: Wed, 16 Jul 2014 12:50:11 +0000 (+0200) Subject: tableprinter: update of linked list and usage of const X-Git-Tag: v6.1~3^2~92 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=efd46e011a873451215540215153035b73d4dc8f;p=libucw.git tableprinter: update of linked list and usage of const --- diff --git a/ucw/table-types.c b/ucw/table-types.c index bea583a7..faa72f42 100644 --- a/ucw/table-types.c +++ b/ucw/table-types.c @@ -45,7 +45,7 @@ static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool) 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_inst_idx].col_def; + const struct table_column *col_def = tbl->column_order[col_inst_idx].col_def; if(col_def->type_def != COL_TYPE_SIZE) { *err = NULL; return false; diff --git a/ucw/table.c b/ucw/table.c index af805f99..16ae418d 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -28,7 +28,7 @@ static struct table *table_make_instance(const struct table_template *tbl_templa new_inst->pool = pool; // initialize column definitions - int col_count = 0; // count the number of columns in the struct table + uint 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].width == 0 && @@ -41,8 +41,11 @@ static struct table *table_make_instance(const struct table_template *tbl_templa } 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; + new_inst->ll_headers = mp_alloc(new_inst->pool, sizeof(int) * col_count); + for(uint i = 0; i < col_count; i++) { + new_inst->ll_headers[i] = -1; + } // initialize column_order if(tbl_template->column_order) { @@ -159,7 +162,7 @@ static void table_update_ll(struct table *tbl) int cols_to_output = tbl->cols_to_output; for(int i = 0; i < tbl->column_count; i++) { - tbl->columns[i].first_column = -1; + tbl->ll_headers[i] = -1; } for(int i = 0; i < cols_to_output; i++) { @@ -168,14 +171,10 @@ static void table_update_ll(struct table *tbl) } for(int i = 0; i < cols_to_output; i++) { - int first = tbl->column_order[i].col_def->first_column; - tbl->column_order[i].col_def->first_column = i; - - if(first != -1) { - tbl->column_order[i].next_column = first; - } else { - tbl->column_order[i].next_column = -1; - } + int col_def_idx = tbl->column_order[i].col_def - tbl->columns; + int first = tbl->ll_headers[col_def_idx]; + tbl->ll_headers[col_def_idx] = i; + tbl->column_order[i].next_column = first; } } @@ -199,7 +198,7 @@ void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output) bool table_col_is_printed(struct table *tbl, uint col_idx) { - if(tbl->columns[col_idx].first_column == -1) return 0; + if(tbl->ll_headers[col_idx] == -1) return 0; return 1; } @@ -221,7 +220,7 @@ static char * table_parse_col_arg(char *col_def) **/ bool table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char **err) { - struct table_column *col_def = tbl->column_order[col_idx].col_def; + const struct table_column *col_def = tbl->column_order[col_idx].col_def; if(col_def->type_def == COL_TYPE_DOUBLE) { uint precision = 0; @@ -473,7 +472,7 @@ const char *table_set_gary_options(struct table *tbl, char **gary_table_opts) static void table_row_human_readable(struct table *tbl) { for(uint i = 0; i < tbl->cols_to_output; i++) { - struct table_column *col_def = tbl->column_order[i].col_def; + const struct table_column *col_def = tbl->column_order[i].col_def; if(i) { bputs(tbl->out, tbl->col_delimiter); } @@ -487,7 +486,7 @@ static void table_row_human_readable(struct table *tbl) static void table_write_header(struct table *tbl) { for(uint i = 0; i < tbl->cols_to_output; i++) { - struct table_column *col_def = tbl->column_order[i].col_def; + const struct table_column *col_def = tbl->column_order[i].col_def; if(i) { bputs(tbl->out, tbl->col_delimiter); } @@ -554,7 +553,7 @@ struct table_formatter table_fmt_machine_readable = { static void table_row_blockline_output(struct table *tbl) { for(uint i = 0; i < tbl->cols_to_output; i++) { - struct table_column *col_def = tbl->column_order[i].col_def; + const struct table_column *col_def = tbl->column_order[i].col_def; bprintf(tbl->out, "%s: %s\n", col_def->name, tbl->column_order[i].cell_content); } bputc(tbl->out, '\n'); @@ -693,8 +692,6 @@ static void test_any_type(struct fastbuf *out) { struct table *tbl = table_init(&test_any_tbl); - tbl->columns[TEST_ANY_COL1_ANY].fmt = XTYPE_FMT_PRETTY; - table_start(tbl, out); table_col_int(tbl, TEST_ANY_COL0_INT, -10); diff --git a/ucw/table.h b/ucw/table.h index 61fe6d48..2b93bdb2 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -109,7 +109,6 @@ struct table_column { const char *name; // [*] Name of the column displayed in table header int width; // [*] Width of the column (in characters) OR'ed with column flags enum xtype_fmt fmt; - int first_column; // head of linked list of columns of this type const struct xtype *type_def; bool (*set_col_instance_option)(struct table *tbl, uint col, const char *value, char **err); @@ -120,7 +119,7 @@ struct table_column { // idx is used only for initialization and col_def is used in other cases 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 + const struct table_column *col_def; // this is pointer to the column definition, located in the array struct table::columns const 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 enum xtype_fmt output_type; // format of this column @@ -131,7 +130,7 @@ struct table_col_instance { * Please use only fields marked with `[*]`. **/ struct table_template { - struct table_column *columns; // [*] Definition of columns + const struct table_column *columns; // [*] Definition of columns 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 @@ -145,8 +144,9 @@ struct table_template { * fields marked with `[*]`. **/ struct table { - struct table_column *columns; // [*] Definition of columns + const struct table_column *columns; // [*] Definition of columns int column_count; // [*] Number of columns (calculated by table_init()) + int *ll_headers; // headers of linked lists that connects column instances struct mempool *pool; // Memory pool used for storing table data. Contains global state // and data of the current row. struct mempool_state pool_state; // State of the pool after the table is initialized, i.e., before @@ -185,31 +185,31 @@ struct table { * ***/ -#define TBL_COL_LIST_INIT .first_column = -1 -#define TBL_COL_STR(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_STR, TBL_COL_LIST_INIT } -#define TBL_COL_INT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_INT, TBL_COL_LIST_INIT } -#define TBL_COL_S64(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_S64, TBL_COL_LIST_INIT } -#define TBL_COL_UINT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINT, TBL_COL_LIST_INIT } -#define TBL_COL_U64(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_U64, TBL_COL_LIST_INIT } -#define TBL_COL_INTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_INTMAX, TBL_COL_LIST_INIT } -#define TBL_COL_UINTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINTMAX, TBL_COL_LIST_INIT } -#define TBL_COL_HEXUINT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINT, TBL_COL_LIST_INIT } -#define TBL_COL_DOUBLE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT } -#define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_BOOL, TBL_COL_LIST_INIT } -#define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_ANY, TBL_COL_LIST_INIT } -#define TBL_COL_CUSTOM(_name, _width, _xtype) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = _xtype, TBL_COL_LIST_INIT } - -#define TBL_COL_STR_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_STR, TBL_COL_LIST_INIT } -#define TBL_COL_INT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_INT, TBL_COL_LIST_INIT } -#define TBL_COL_S64_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_S64, TBL_COL_LIST_INIT } -#define TBL_COL_UINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINT, TBL_COL_LIST_INIT } -#define TBL_COL_U64_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_U64, TBL_COL_LIST_INIT } -#define TBL_COL_INTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_INTMAX, TBL_COL_LIST_INIT } -#define TBL_COL_UINTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINTMAX, TBL_COL_LIST_INIT } -#define TBL_COL_HEXUINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINT, TBL_COL_LIST_INIT } -#define TBL_COL_BOOL_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_BOOL, TBL_COL_LIST_INIT } -#define TBL_COL_ANY_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_ANY, TBL_COL_LIST_INIT } -#define TBL_COL_DOUBLE_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT } +//#define TBL_COL_LIST_INIT .first_column = -1 +#define TBL_COL_STR(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_STR } +#define TBL_COL_INT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_INT } +#define TBL_COL_S64(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_S64 } +#define TBL_COL_UINT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINT } +#define TBL_COL_U64(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_U64 } +#define TBL_COL_INTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_INTMAX } +#define TBL_COL_UINTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINTMAX } +#define TBL_COL_HEXUINT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINT } +#define TBL_COL_DOUBLE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_DOUBLE } +#define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_BOOL } +#define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_ANY } +#define TBL_COL_CUSTOM(_name, _width, _xtype) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = _xtype } + +#define TBL_COL_STR_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_STR } +#define TBL_COL_INT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_INT } +#define TBL_COL_S64_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_S64 } +#define TBL_COL_UINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINT } +#define TBL_COL_U64_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_U64 } +#define TBL_COL_INTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_INTMAX } +#define TBL_COL_UINTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINTMAX } +#define TBL_COL_HEXUINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINT } +#define TBL_COL_BOOL_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_BOOL } +#define TBL_COL_ANY_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_ANY } +#define TBL_COL_DOUBLE_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_DOUBLE } #define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type_def = NULL } @@ -224,8 +224,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_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_START(_tbl, _colidx, _var, _idxval) { struct table_col_instance *_var = NULL; int _idxval = _tbl->ll_headers[_colidx]; \ + for(_idxval = _tbl->ll_headers[_colidx], _var = _tbl->column_order + _idxval; _idxval != -1; _idxval = _tbl->column_order[_idxval].next_column, _var = _tbl->column_order + _idxval) #define TBL_COL_ITER_END }