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)
.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 };
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 = {
.name = "timestamp",
//.parse = xt_timestamp_parse,
.format = xt_timestamp_format,
+ .parse_fmt = xt_timestamp_fmt_parse
};
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)
/**
* 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);
}
/**
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);
}
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.
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
};
/**
* 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
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 */
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