From: Robert Kessl Date: Mon, 28 Jul 2014 10:40:25 +0000 (+0200) Subject: tableprinter: update of table_set_col_order(and tests) X-Git-Tag: v6.1~3^2~37 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=ecb2433fe8157ec124f95ad0bbeb6a9bbf354328;p=libucw.git tableprinter: update of table_set_col_order(and tests) --- diff --git a/ucw/table.c b/ucw/table.c index eeffcfb7..e2e61696 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -80,14 +80,18 @@ void table_cleanup(struct table *tbl) } // TODO: test default column order +// FIXME: should we copy the default format from table definition or use TBL_FMT_HUMAN_READABLE? static void table_make_default_column_order(struct table *tbl) { - int *col_order_int = alloca(sizeof(int) * tbl->column_count); + struct table_col_instance *col_order = alloca(sizeof(struct table_col_instance) * tbl->column_count); + bzero(col_order, sizeof(struct table_col_instance) * tbl->column_count); for(int i = 0; i < tbl->column_count; i++) { - col_order_int[i] = i; + col_order[i].idx = (uint) i; + col_order[i].fmt = tbl->columns[i].fmt; } - table_set_col_order(tbl, col_order_int, tbl->column_count); + + table_set_col_order(tbl, col_order, tbl->column_count); } void table_start(struct table *tbl, struct fastbuf *out) @@ -167,21 +171,21 @@ static void table_update_ll(struct table *tbl) } } -void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output) +void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order, uint cols_to_output) { - for(int i = 0; i < cols_to_output; i++) { - ASSERT_MSG(col_order[i] >= 0 && col_order[i] < tbl->column_count, "Column %d does not exist (column number should be between 0 and %d).", col_order[i], tbl->column_count - 1); + for(uint i = 0; i < cols_to_output; i++) { + ASSERT_MSG(col_order[i].idx < (uint) tbl->column_count, "Column %d does not exist; column number should be between 0 and %d(including).", col_order[i].idx, tbl->column_count - 1); } tbl->cols_to_output = 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_def_idx = col_order[i]; - tbl->column_order[i].idx = col_def_idx; + memcpy(tbl->column_order, col_order, sizeof(struct table_col_instance) * cols_to_output); + for(uint i = 0; i < cols_to_output; i++) { + int col_def_idx = tbl->column_order[i].idx; // this is given in col_order tbl->column_order[i].col_def = tbl->columns + col_def_idx; - tbl->column_order[i].cell_content = NULL; // it is in fact initialized by mp_alloc_zero, but for completeness ... + tbl->column_order[i].cell_content = NULL; // cell_content is copied from @col_order, so make sure that it is NULL tbl->column_order[i].next_column = -1; - tbl->column_order[i].fmt = tbl->columns[col_def_idx].fmt; + // tbl->column_order[i].fmt should be untouched (copied from col_order) } table_update_ll(tbl); } @@ -655,6 +659,22 @@ static void test_simple1(struct fastbuf *out) do_print1(tbl); table_end(tbl); + + // test table_col_order_fmt + struct table_col_instance col_order[] = { TBL_COL(TEST_COL0_STR), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_PRETTY), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_RAW) }; + table_set_col_order(tbl, col_order, ARRAY_SIZE(col_order)); + table_start(tbl, out); + + table_col_str(tbl, TEST_COL0_STR, "test"); + table_col_double(tbl, TEST_COL4_DOUBLE, 1.23456789); + table_end_row(tbl); + + table_col_str(tbl, TEST_COL0_STR, "test"); + table_col_double(tbl, TEST_COL4_DOUBLE, 1.23456789); + table_end_row(tbl); + + table_end(tbl); + table_cleanup(tbl); } diff --git a/ucw/table.h b/ucw/table.h index c2dd948d..c1bf73e3 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -197,6 +197,7 @@ struct table { #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 +#define TBL_FMT(_fmt) .formatter = _fmt /** * The TBL_COL_ITER_START macro are used for iterating over all instances of a particular column in @@ -314,7 +315,6 @@ void table_reset_row(struct table *tbl); **/ int table_get_col_idx(struct table *tbl, const char *col_name); - /** * Sets a string option to an instance of a column type. This is the default version that checks * whether the xtype::parse_fmt can be called and calls it. However, there are situation in which @@ -339,22 +339,16 @@ const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char * const char *table_get_col_list(struct table *tbl); /** + * Sets the order in which the columns are printed. The columns are specified by struct + * * Sets the order in which the columns are printed. * The table converts the integers in @col_order into an internal representation stored * in `column_order`. Options to column instances can be set using @table_set_col_opt(). * - * FIXME: add a function to the interface that accepts a pointer to an - * array of table_col_instance. - **/ -void table_set_col_order(struct table *tbl, int *col_order, int col_order_size); - -/** - * Returns true if col_idx will be printed, false otherwise. - * - * FIXME: Naming of arguments is confusing. @col_idx sometimes indexes - * columns, but sometimes their instances. + * @table_col_instance. This allows specification of format. The user should make an array of struct + * @table_col_instance and fill the array using the TBL_COL and TBL_COL_FMT. **/ -bool table_col_is_printed(struct table *tbl, uint col_def_idx); +void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order, uint cols_to_output); /** * Sets the order in which the columns are printed. The specification is a string with comma-separated column @@ -374,6 +368,14 @@ bool table_col_is_printed(struct table *tbl, uint col_def_idx); **/ const char *table_set_col_order_by_name(struct table *tbl, const char *col_order); +/** + * Returns true if col_idx will be printed, false otherwise. + * + * FIXME: Naming of arguments is confusing. @col_idx sometimes indexes + * columns, but sometimes their instances. + **/ +bool table_col_is_printed(struct table *tbl, uint col_def_idx); + /** * Sets table formatter. See below for the list of formatters. **/ diff --git a/ucw/table.t b/ucw/table.t index 02383fd0..a04c5e42 100644 --- a/ucw/table.t +++ b/ucw/table.t @@ -23,6 +23,9 @@ col3_bool col0_str col0_str col1_int col2_uint col3_bool col4_double sdsdf 10000 XXX-22222 true AAA test -100 100 false 1.50 + col0_str col4_double col4_double + test 1.23 1.23456789 + test 1.23 1.23456789 col0_int col1_any -10 10000 -10 1.40