]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: bugfix in initialization macros
authorRobert Kessl <kesslr@centrum.cz>
Fri, 4 Jul 2014 10:30:30 +0000 (12:30 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Fri, 4 Jul 2014 10:30:30 +0000 (12:30 +0200)
 - added forgotten type definition to TBL_COL_SIZE_FMT, TBL_COL_TIMESTAMP_FMT

ucw/table.c
ucw/table.h

index 538e82349571e352898c8867952696046344d980..915f6b7bf75db207177ce549740b10de0ee54027 100644 (file)
@@ -131,14 +131,18 @@ static void table_update_ll(struct table *tbl)
   }
 
   for(int i = 0; i < cols_to_output; i++) {
-    int col_idx = tbl->column_order[i].idx;
-    int last = tbl->columns[col_idx].last_column;
+    int idx = tbl->column_order[i].idx;
+    tbl->column_order[i].col_def = tbl->columns + idx;
+  }
+
+  for(int i = 0; i < cols_to_output; i++) {
+    int last = tbl->column_order[i].col_def->last_column;
     if(last != -1) {
-      tbl->columns[col_idx].last_column = i;
+      tbl->column_order[i].col_def->last_column = i;
       tbl->column_order[last].next_column = i;
     } else {
-      tbl->columns[col_idx].last_column = i;
-      tbl->columns[col_idx].first_column = i;
+      tbl->column_order[i].col_def->last_column = i;
+      tbl->column_order[i].col_def->first_column = i;
     }
     tbl->column_order[i].next_column = -1;
   }
@@ -155,6 +159,7 @@ void table_set_col_order(struct table *tbl, int *col_order, int 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;
+    tbl->column_order[i].col_def = tbl->columns + col_idx;
     tbl->column_order[i].cell_content = NULL;
     tbl->column_order[i].output_type = CELL_OUT_UNINITIALIZED;
   }
@@ -186,9 +191,10 @@ static char * table_parse_col_arg(char *col_def)
  **/
 bool table_set_col_opt_default(struct table *tbl, int col_copy_idx, const char *col_arg, char **err)
 {
+  struct table_column *tmp_col = tbl->column_order[col_copy_idx].col_def;
   int col_type_idx = tbl->column_order[col_copy_idx].idx;
 
-  if(tbl->columns[col_type_idx].type == COL_TYPE_DOUBLE) {
+  if(tmp_col->type == COL_TYPE_DOUBLE) {
     uint precision = 0;
     str_to_uint(&precision, col_arg, NULL, 0);
     tbl->column_order[col_type_idx].output_type = precision;
@@ -247,6 +253,7 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde
     if(col_idx == -1) {
       return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
     }
+    tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
     tbl->column_order[curr_col_idx].idx = col_idx;
     tbl->column_order[curr_col_idx].cell_content = NULL;
     tbl->column_order[curr_col_idx].output_type = CELL_OUT_UNINITIALIZED;
@@ -536,7 +543,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++) {
-    int col_idx = tbl->column_order[i].idx;
+    int col_idx = tbl->column_order[i].col_def - tbl->columns;
     if(i) {
       bputs(tbl->out, tbl->col_delimiter);
     }
@@ -583,10 +590,10 @@ static void table_start_machine_readable(struct table *tbl)
   }
 
   if(tbl->print_header != 0) {
-    uint col_idx = tbl->column_order[0].idx;
+    uint col_idx = tbl->column_order[0].col_def - tbl->columns;
     bputs(tbl->out, tbl->columns[col_idx].name);
     for(uint i = 1; i < tbl->cols_to_output; i++) {
-      col_idx = tbl->column_order[i].idx;
+      col_idx = tbl->column_order[i].col_def - tbl->columns;
       bputs(tbl->out, tbl->col_delimiter);
       bputs(tbl->out, tbl->columns[col_idx].name);
     }
index afe7b7d34b29080d18b8a673613a813fc54770fe..4672b6d39d0a6e992d90a788c4feb21774104e3b 100644 (file)
@@ -122,16 +122,18 @@ struct table_column {
   int width;                   // [*] Width of the column (in characters) OR'ed with column flags
   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;
-  int last_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
 struct table_col_info {
-  uint idx;
-  char *cell_content;
-  int next_column;
-  int output_type;
+  uint idx;                      // idx is a pointer to 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
+  int output_type;               // format of this column
 };
 
 /**