]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: definition of the table separated from handle
authorRobert Kessl <kesslr@centrum.cz>
Tue, 8 Jul 2014 08:03:53 +0000 (10:03 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Tue, 8 Jul 2014 08:03:53 +0000 (10:03 +0200)
ucw/table-test-2.c
ucw/table-test-align.c
ucw/table-test.c
ucw/table.c
ucw/table.h

index 78116d84cfde0912308800a88f8ce4fd4d2b2120..41f68823cf27249d36731cd721f7921e60a8d4d1 100644 (file)
@@ -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),
index ad480ad788a318a7d483f920a5d3204968a30e4d..1de8041ec1fb55981b9526aad73f543001f4790b 100644 (file)
@@ -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 = {
index 4e59e7e28d3d6af2e28ffe86bd3ef98dcad37d27..e0f18d086f9c879c672aac8d3ade7740063269d4 100644 (file)
@@ -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),
index cb14a89c741754d66973aa7ec2c4d985b547793c..0f1490cbd5cbd33a853f80fac7ad36e60a9aa422 100644 (file)
@@ -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),
index ef7ecdc130a3709fa05b2881a7cb99cb144ce4f8..96a5b05171eaba29ceb6a3016a2a02d87e027482 100644 (file)
@@ -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);