#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) {
} 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;
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);
#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 }
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)
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;
#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.
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 {
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[];
};