From a7992185caf3d46b152653937063fe72582214df Mon Sep 17 00:00:00 2001 From: Robert Kessl Date: Tue, 8 Jul 2014 10:03:53 +0200 Subject: [PATCH] tableprinter: definition of the table separated from handle --- ucw/table-test-2.c | 6 ++---- ucw/table-test-align.c | 3 +-- ucw/table-test.c | 4 ++-- ucw/table.c | 21 ++++++++------------- ucw/table.h | 23 ++++++++++++++++++++--- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/ucw/table-test-2.c b/ucw/table-test-2.c index 78116d84..41f68823 100644 --- a/ucw/table-test-2.c +++ b/ucw/table-test-2.c @@ -14,7 +14,7 @@ enum test_table_cols { TEST_COL0_SIZE, TEST_COL1_TS }; -static struct table test_tbl = { +static struct table_template test_tbl = { TBL_COLUMNS { [TEST_COL0_SIZE] = TBL_COL_SIZE_FMT("size", 15, UNIT_BYTE), [TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20), @@ -66,9 +66,7 @@ static void do_test(void) bclose(out); } - - -static struct table test_tbl2 = { +static struct table_template test_tbl2 = { TBL_COLUMNS { [TEST_COL0_SIZE] = TBL_COL_SIZE_FMT("size", 15, UNIT_BYTE), [TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20), diff --git a/ucw/table-test-align.c b/ucw/table-test-align.c index ad480ad7..1de8041e 100644 --- a/ucw/table-test-align.c +++ b/ucw/table-test-align.c @@ -14,7 +14,7 @@ enum test_table_cols { test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double }; -static struct table test_tbl = { +static struct table_template test_tbl = { TBL_COLUMNS { [test_col0_str] = TBL_COL_STR("col0_str", 30 | CELL_ALIGN_LEFT), [test_col1_int] = TBL_COL_INT("col1_int", 8), @@ -27,7 +27,6 @@ static struct table test_tbl = { TBL_COL_DELIMITER("\t"), }; -static int test_to_perform = -1; static char **cli_table_opts; static struct opt_section table_printer_opts = { diff --git a/ucw/table-test.c b/ucw/table-test.c index 4e59e7e2..e0f18d08 100644 --- a/ucw/table-test.c +++ b/ucw/table-test.c @@ -16,7 +16,7 @@ enum test_table_cols { 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 test_tbl = { +static struct table_template test_tbl = { TBL_COLUMNS { [test_col0_str] = TBL_COL_STR("col0_str", 20), [test_col1_int] = TBL_COL_INT("col1_int", 8), @@ -36,7 +36,7 @@ enum test_default_order_cols { test_default_order_col0_int, test_default_order_col1_int, test_default_order_col2_int }; -static struct table test_default_order_tbl = { +static struct table_template test_default_order_tbl = { TBL_COLUMNS { [test_default_order_col0_int] = TBL_COL_INT("col0_int", 8), [test_default_order_col1_int] = TBL_COL_INT("col1_int", 9), diff --git a/ucw/table.c b/ucw/table.c index cb14a89c..0f1490cb 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -20,14 +20,7 @@ static void table_update_ll(struct table *tbl); /*** Management of tables ***/ -static void table_template_init(struct table *tbl_template) -{ - if(!tbl_template->pool) { - tbl_template->pool = mp_new(4096); - } -} - -static struct table *table_template_copy(struct table *tbl_template) +static struct table *table_template_copy(struct table_template *tbl_template) { struct table *copy = mp_alloc_zero(tbl_template->pool, sizeof(struct table)); @@ -48,7 +41,7 @@ static struct table *table_template_copy(struct table *tbl_template) copy->columns = tbl_template->columns; copy->col_delimiter = tbl_template->col_delimiter; - copy->print_header = tbl_template->print_header; + copy->print_header = 1; copy->out = 0; copy->last_printed_col = -1; copy->row_printing_started = 0; @@ -58,11 +51,13 @@ static struct table *table_template_copy(struct table *tbl_template) return copy; } -struct table *table_init(struct table *tbl_template) +struct table *table_init(struct table_template *tbl_template) { int col_count = 0; // count the number of columns in the struct table - table_template_init(tbl_template); + if(!tbl_template->pool) { + tbl_template->pool = mp_new(4096); + } struct table *tbl = table_template_copy(tbl_template); @@ -683,7 +678,7 @@ enum test_table_cols { 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 test_tbl = { +static struct table_template test_tbl = { TBL_COLUMNS { [test_col0_str] = TBL_COL_STR("col0_str", 20), [test_col1_int] = TBL_COL_INT("col1_int", 8), @@ -777,7 +772,7 @@ enum test_any_table_cols { static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) }; -static struct table test_any_tbl = { +static struct table_template test_any_tbl = { TBL_COLUMNS { [test_any_col0_int] = TBL_COL_INT("col0_int", 8), [test_any_col1_any] = TBL_COL_ANY("col1_any", 9), diff --git a/ucw/table.h b/ucw/table.h index ef7ecdc1..96a5b051 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -139,8 +139,25 @@ struct table_col_info { }; /** - * Definition of a table. Contains column definitions, per-table settings - * and internal data. Please use only fields marked with `[*]`. + * Definition of a table. Contains column definitions, and some per-table settings. + * Please use only fields marked with `[*]`. + **/ +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 + 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 + struct table_formatter *formatter; + + struct mempool *pool; // Memory pool used for storing table handles. +}; + +/** + * Handle of a table. Contains column definitions, per-table settings + * and internal data. To change the table definition, please use only + * fields marked with `[*]`. **/ struct table { struct table_column *columns; // [*] Definition of columns @@ -226,7 +243,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 *tbl_template); +struct table *table_init(struct table_template *tbl_template); /** Destroy a table definition, freeing all memory used by it. **/ void table_cleanup(struct table *tbl); -- 2.39.2