]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: update of parsing of column options
authorRobert Kessl <kesslr@centrum.cz>
Tue, 22 Jul 2014 08:38:03 +0000 (10:38 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Tue, 22 Jul 2014 08:38:03 +0000 (10:38 +0200)
ucw/table-types.c
ucw/table-types.h
ucw/table.c
ucw/table.h
ucw/xtypes-basic.c
ucw/xtypes.h

index f516f8f47ff617dceb2ca63300b2298f4ea158a3..b5027817c8cee071759dcffab6ed4b016774b479 100644 (file)
@@ -55,39 +55,25 @@ static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool)
   return mp_printf(pool, "%"PRIu64"%s", curr_val, unit_suffix[out_type]);
 }
 
-int table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
+static const char * xt_size_fmt_parse(const char *opt_str, u32 *dest, struct mempool *pool UNUSED)
 {
-  if(col_arg == NULL) {
-    *err = "NULL is not supported as a column argument.";
-    return TABLE_OPT_ERR;
+  if(opt_str == NULL) {
+    return "NULL is not supported as a column argument.";
   }
 
-  const struct table_column *col_def = tbl->column_order[col_inst_idx].col_def;
-  if(col_def->type_def != &xt_size) {
-    *err = NULL;
-    return TABLE_OPT_UNKNOWN;
+  if(strlen(opt_str) == 0 || strcasecmp(opt_str, "b") == 0 || strcasecmp(opt_str, "bytes") == 0) {
+    *dest = SIZE_UNIT_BYTE | SIZE_UNITS_FIXED;
+    return NULL;
   }
 
-  if(strlen(col_arg) == 0 || strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
-    tbl->column_order[col_inst_idx].fmt = SIZE_UNIT_BYTE | SIZE_UNITS_FIXED;
-    *err = NULL;
-    return TABLE_OPT_PROCESSED;
-  }
-
-  tbl->column_order[col_inst_idx].fmt = XTYPE_FMT_DEFAULT; // CELL_OUT_UNINITIALIZED;
   for(uint i = SIZE_UNIT_BYTE; i <= SIZE_UNIT_TERABYTE; i++) {
-    if(strcasecmp(col_arg, unit_suffix[i]) == 0) {
-      tbl->column_order[col_inst_idx].fmt = i | SIZE_UNITS_FIXED;
+    if(strcasecmp(opt_str, unit_suffix[i]) == 0) {
+      *dest = i | SIZE_UNITS_FIXED;
+      return NULL;
     }
   }
 
-  if(tbl->column_order[col_inst_idx].fmt == XTYPE_FMT_DEFAULT) {
-    *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_inst_idx);
-    return TABLE_OPT_ERR;
-  }
-
-  *err = NULL;
-  return TABLE_OPT_PROCESSED;
+  return "Unknown option.";
 }
 
 TABLE_COL_BODY(size, u64)
@@ -97,38 +83,13 @@ const struct xtype xt_size = {
   .name = "size",
   //.parse = xt_size_parse,
   .format = xt_size_format,
+  .parse_fmt = xt_size_fmt_parse
 };
 
 /** xt_timestamp **/
 
 #define FORMAT_TIME_SIZE 20    // Minimum buffer size
 
-int table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
-{
-  int col_type_idx = tbl->column_order[col_inst_idx].idx;
-  if(tbl->columns[col_type_idx].type_def != &xt_timestamp) {
-    *err = NULL;
-    return TABLE_OPT_UNKNOWN;
-  }
-
-  if(col_arg == NULL) {
-    *err = "NULL is not supported as a column argument.";
-    return TABLE_OPT_ERR;
-  }
-
-  if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
-    tbl->column_order[col_inst_idx].fmt = TIMESTAMP_EPOCH;
-  } else if(strcasecmp(col_arg, "datetime") == 0) {
-    tbl->column_order[col_inst_idx].fmt = TIMESTAMP_DATETIME;
-  } else {
-    *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_inst_idx);
-    return TABLE_OPT_ERR;
-  }
-
-  *err = NULL;
-  return TABLE_OPT_PROCESSED;
-}
-
 static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool)
 {
   char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
@@ -152,6 +113,23 @@ static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool)
   return mp_printf(pool, "%s", formatted_time_buf);
 }
 
+static const char * xt_timestamp_fmt_parse(const char *opt_str, u32 *dest, struct mempool *pool)
+{
+  if(opt_str == NULL) {
+    return "NULL is not supported as a column argument.";
+  }
+
+  if(strcasecmp(opt_str, "timestamp") == 0 || strcasecmp(opt_str, "epoch") == 0) {
+    *dest = TIMESTAMP_EPOCH;
+    return NULL;
+  } else if(strcasecmp(opt_str, "datetime") == 0) {
+    *dest = TIMESTAMP_DATETIME;
+    return NULL;
+  }
+
+  return mp_printf(pool, "Invalid column format option: '%s'.", opt_str);
+}
+
 TABLE_COL_BODY(timestamp, u64)
 
 const struct xtype xt_timestamp = {
@@ -159,4 +137,5 @@ const struct xtype xt_timestamp = {
   .name = "timestamp",
   //.parse = xt_timestamp_parse,
   .format = xt_timestamp_format,
+  .parse_fmt = xt_timestamp_fmt_parse
 };
index e7b4366b22c9aaeffe1aaa79a29e3b641dc37eda..79b9e19e8261582a5ff34d7ea0f4da61d0f7d330 100644 (file)
@@ -26,14 +26,11 @@ enum size_units {
 extern const struct xtype xt_size;
 extern const struct xtype xt_timestamp;
 
-int table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err);
-int table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err);
+#define TBL_COL_SIZE(_name, _width)       { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_size, .set_col_instance_option = table_set_col_opt_default }
+#define TBL_COL_TIMESTAMP(_name, _width)  { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_timestamp, .set_col_instance_option = table_set_col_opt_default }
 
-#define TBL_COL_SIZE(_name, _width)       { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_size, .set_col_instance_option = table_set_col_opt_size }
-#define TBL_COL_TIMESTAMP(_name, _width)  { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_timestamp, .set_col_instance_option = table_set_col_opt_timestamp }
-
-#define TBL_COL_SIZE_FMT(_name, _width, _units)    { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_size, .set_col_instance_option = table_set_col_opt_size }
-#define TBL_COL_TIMESTAMP_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_timestamp, .set_col_instance_option = table_set_col_opt_timestamp }
+#define TBL_COL_SIZE_FMT(_name, _width, _units)    { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_size, .set_col_instance_option = table_set_col_opt_default }
+#define TBL_COL_TIMESTAMP_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = &xt_timestamp, .set_col_instance_option = table_set_col_opt_default}
 
 
 TABLE_COL_PROTO(size, u64)
index cf9941775256a48d0d3ff57e7017f7e782e946b2..90988d21e4f2684c17f6b4595c6a02015115a809 100644 (file)
@@ -218,23 +218,19 @@ static char * table_parse_col_arg(char *col_def)
 /**
  * Setting options for basic table types (as defined in table.h)
  **/
-int table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char **err)
+const char *table_set_col_opt_default(struct table *tbl, uint col_idx, const char *col_opt)
 {
   const struct table_column *col_def = tbl->column_order[col_idx].col_def;
 
-  if(col_def->type_def == &xt_double) {
-    uint precision = 0;
-    const char *tmp_err = str_to_uint(&precision, col_arg, NULL, 0);
-    if(tmp_err) {
-      *err = mp_printf(tbl->pool, "An error occured while parsing precision: %s.", tmp_err);
-      return false;
-    }
-    tbl->column_order[col_idx].fmt = precision; // FIXME: shift the value of precision
-    return true;
+  if(col_def && col_def->type_def && col_def->type_def->parse_fmt) {
+    uint fmt = 0;
+    const char *tmp_err = col_def->type_def->parse_fmt(col_opt, &fmt, tbl->pool);
+    if(tmp_err) return tmp_err;
+    tbl->column_order[col_idx].fmt = fmt;
+    return NULL;
   }
 
-  *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_idx);
-  return false;
+  return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_idx);
 }
 
 /**
@@ -284,8 +280,8 @@ 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].fmt = tbl->columns[col_idx].fmt;
     if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_instance_option) {
-      char *err = NULL;
-      tbl->columns[col_idx].set_col_instance_option(tbl, curr_col_idx, arg, &err);
+      const char *err = NULL;
+      err = tbl->columns[col_idx].set_col_instance_option(tbl, curr_col_idx, arg);
       if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
     }
 
index 576404775bb4ce3457dfc595bdef548af20a6c52..4f63e827f226b0537092a97de7946f0838891147 100644 (file)
 
 struct table;
 
-enum table_option_state {
-  TABLE_OPT_PROCESSED,
-  TABLE_OPT_UNKNOWN,
-  TABLE_OPT_ERR
-};
-
 /**
  * Definition of a single table column.
  * Usually, this is generated using the `TABLE_COL_`'type' macros.
@@ -110,7 +104,7 @@ struct table_column {
   uint fmt;                     // [*] default format of the column
   const struct xtype *type_def; // [*] pointer to xtype of this column
 
-  int (*set_col_instance_option)(struct table *tbl, uint col, const char *value, char **err);
+  const char * (*set_col_instance_option)(struct table *tbl, uint col, const char *value);
        // [*] process table option for a column instance
 };
 
@@ -346,7 +340,7 @@ int table_get_col_idx(struct table *tbl, const char *col_name);
 /**
  * Sets a string argument to a column instance
  **/
-int table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char ** err);
+const char *table_set_col_opt_default(struct table *tbl, uint col_idx, const char *col_opt);
 
 /**
  * Returns a comma-and-space-separated list of column names, allocated from table's internal
index 9bca14361799148aee874eec6179b8932ef151a1..4ee14cfa111a17da40c62eb7cdba7fdcb274cd7b 100644 (file)
@@ -77,7 +77,26 @@ static const char *xt_double_parse(const char *str, void *dest, struct mempool *
   return NULL;
 }
 
-XTYPE_NUM_STRUCT(double, double)
+static const char * xt_double_fmt_parse(const char *str, u32 *dest, struct mempool *pool)
+{
+  uint precision = 0;
+  const char *tmp_err = str_to_uint(&precision, str, NULL, 0);
+  if(tmp_err) {
+    return mp_printf(pool, "An error occured while parsing precision: %s.", tmp_err);
+  }
+
+  *dest = XTYPE_FMT_DBL_FIXED_PREC(precision);
+
+  return NULL;
+}
+
+const struct xtype xt_double = {
+  .size = sizeof(double),
+  .name = "double",
+  .parse = xt_double_parse,
+  .format = xt_double_format,
+  .parse_fmt = xt_double_fmt_parse
+};
 
 /* bool */
 
index 840713bea53505cb9ebe59e3966b47d4ca6c2e18..5a42e46570fcb71b053205de48a60b961787e556 100644 (file)
@@ -99,7 +99,7 @@ extern const struct xtype xt_bool;
 extern const struct xtype xt_double;
 
 
-#define XTYPE_FMT_DBL_PREC 0x40000000
-#define XTYPE_FMT_DBL_FIXED_PREC(_prec) (_prec | XTYPE_FMT_DBL_PREC)
+#define XTYPE_FMT_DBL_PREC   XTYPE_FMT_CUSTOM
+#define XTYPE_FMT_DBL_FIXED_PREC(_prec) (_prec | XTYPE_FMT_CUSTOM)
 
 #endif