]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: update of user-defined column types
authorRobert Kessl <kesslr@centrum.cz>
Wed, 2 Jul 2014 11:51:43 +0000 (13:51 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Wed, 2 Jul 2014 11:51:51 +0000 (13:51 +0200)
 - added pointer to a definition of column to struct table_column
 - added definition of column with size and timestamp.

ucw/table-types.c
ucw/table-types.h
ucw/table.c
ucw/table.h

index 75bf53bc0cf246eb793137e0c4c5ea34b3832dc3..50d3079e98a37b6c643258d17a63bedada410016 100644 (file)
@@ -8,7 +8,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char *col_arg, char **err)
+static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
 {
   int col_type_idx = tbl->column_order[col_copy_idx].idx;
   if(tbl->columns[col_type_idx].type == COL_TYPE_SIZE) {
@@ -23,13 +23,25 @@ bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char
     } else if(strcasecmp(col_arg, "tb") == 0) {
       tbl->column_order[col_copy_idx].output_type = UNIT_TERABYTE;
     } else {
-      *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
+      *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_copy_idx);
       return true;
     }
     *err = NULL;
     return true;
   }
 
+  *err = NULL;
+  return false;
+}
+
+struct table_user_type table_type_size = {
+  .set_col_instance_option = table_set_col_opt_size,
+  .type = COL_TYPE_SIZE,
+};
+
+static bool table_set_col_opt_timestamp(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
+{
+  int col_type_idx = tbl->column_order[col_copy_idx].idx;
   if(tbl->columns[col_type_idx].type == COL_TYPE_TIMESTAMP) {
     if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
       tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH;
@@ -43,9 +55,15 @@ bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char
     return true;
   }
 
-  return table_set_col_opt_default(tbl, col_copy_idx, col_arg, err);
+  *err = NULL;
+  return false;
 }
 
+struct table_user_type table_type_timestamp = {
+  .set_col_instance_option = table_set_col_opt_timestamp,
+  .type = COL_TYPE_TIMESTAMP,
+};
+
 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
 {
   int col = table_get_col_idx(tbl, col_name);
index 5b264e11ced5f70f5bca32966f08f7393951b412..7b8dec5ac0d28677da8de7251cac96f0b0a265f6 100644 (file)
@@ -21,8 +21,11 @@ enum timestamp_format {
 #define COL_TYPE_SIZE       COL_TYPE_CUSTOM
 #define COL_TYPE_TIMESTAMP  (COL_TYPE_CUSTOM+1)
 
-#define TBL_COL_SIZE(_name, _width)           { .name = _name, .width = _width, .fmt = "%llu", .type = COL_TYPE_SIZE }
-#define TBL_COL_TIMESTAMP(_name, _width)      { .name = _name, .width = _width, .fmt = "%lld", .type = COL_TYPE_TIMESTAMP }
+extern struct table_user_type table_type_timestamp;
+extern struct table_user_type table_type_size;
+
+#define TBL_COL_SIZE(_name, _width)           { .name = _name, .width = _width, .fmt = "%llu", .type = COL_TYPE_SIZE, .type_def = &table_type_size }
+#define TBL_COL_TIMESTAMP(_name, _width)      { .name = _name, .width = _width, .fmt = "%lld", .type = COL_TYPE_TIMESTAMP, .type_def = &table_type_timestamp }
 
 #define TBL_COL_SIZE_FMT(_name, _width, _units)         { .name = _name, .width = _width, .fmt = "%llu", .type = COL_TYPE_SIZE }
 #define TBL_COL_TIMESTAMP_FMT(_name, _width, _fmt)      { .name = _name, .width = _width, .fmt = "%lld", .type = COL_TYPE_TIMESTAMP }
@@ -54,8 +57,6 @@ void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val)
 void table_col_size(struct table *tbl, int col, u64 val);
 void table_col_timestamp(struct table *tbl, int col, u64 val);
 
-bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char *col_arg, char **err);
-
 //TABLE_COL(size, u64, COL_TYPE_SIZE)
 //TABLE_COL_STR(size, u64, COL_TYPE_SIZE)
 
index 048d4589222e1eeb2af0a63e67357344e88264b8..4ed97f300df4f34c71bf1e04ab549d3e447d894b 100644 (file)
@@ -249,10 +249,10 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde
     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;
-    if(tbl->formatter && tbl->formatter->set_col_instance_option) {
+    if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].type_def->set_col_instance_option) {
       char *err = NULL;
-      tbl->formatter->set_col_instance_option(tbl, curr_col_idx, arg, &err);
-      if(err) return err;
+      tbl->columns[col_idx].type_def->set_col_instance_option(tbl, curr_col_idx, arg, &err);
+      if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
     }
 
     name_start = next;
index 14e843143718467349d0e5c42f59087b85652a84..43d94f79b960181873660b3a4c84a10ca201e1d4 100644 (file)
@@ -103,6 +103,15 @@ enum column_type {
 #define CELL_OUT_HUMAN_READABLE     0
 #define CELL_OUT_MACHINE_READABLE   1
 
+struct table;
+
+struct table_user_type {
+  bool (*set_col_instance_option)(struct table *tbl, uint col, const char *value, char **err);
+       // [*] process table option for a column instance
+  uint type;               // [*] type identifier, should be a number shifted by COL_TYPE_CUSTOM
+  const char *default_fmt; // [*] default format used for printing
+};
+
 /**
  * Definition of a single table column.
  * Usually, this is generated using the `TABLE_COL_`'type' macros.
@@ -115,6 +124,7 @@ struct table_column {
   enum column_type type;       // [*] Type of the cells in the column
   int first_column;
   int last_column;
+  struct table_user_type *type_def;
 };
 
 struct table_col_info {
@@ -405,8 +415,6 @@ struct table_formatter {
   void (*table_end)(struct table *tbl);                // [*] table_end callback (optional)
   bool (*process_option)(struct table *tbl, const char *key, const char *value, const char **err);
        // [*] Process table option and possibly return an error message (optional)
-  bool (*set_col_instance_option)(struct table *tbl, uint col, const char *value, char **err);
-        // [*] process table option for a column instance
   const char *formats[];
 };