- update of cell output types: CELL_OUT_UNINITIALIZED is -1, CELL_OUT_HUMAN_READABLE is now -2 and CELL_OUT_MACHINE_READABLE -3
- table_col_bool* now uses bool instead of uint
- other code cleanup
[UNIT_SIZE_TERABYTE] = "TB"
};
-static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
+static bool table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
{
- struct table_column *col_def = tbl->column_order[col_copy_idx].col_def;
+ struct table_column *col_def = tbl->column_order[col_inst_idx].col_def;
if(col_def->type != COL_TYPE_SIZE) {
*err = NULL;
return false;
}
if(col_arg == NULL || strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
- tbl->column_order[col_copy_idx].output_type = UNIT_SIZE_BYTE;
+ tbl->column_order[col_inst_idx].output_type = UNIT_SIZE_BYTE;
*err = NULL;
return true;
}
- tbl->column_order[col_copy_idx].output_type = CELL_OUT_UNINITIALIZED;
- for(uint i = 0; i < ARRAY_SIZE(unit_suffix); i++) {
+ tbl->column_order[col_inst_idx].output_type = CELL_OUT_UNINITIALIZED;
+ for(uint i = UNIT_SIZE_BYTE; i <= UNIT_SIZE_TERABYTE; i++) {
if(strcasecmp(col_arg, unit_suffix[i]) == 0) {
- tbl->column_order[col_copy_idx].output_type = i;
+ tbl->column_order[col_inst_idx].output_type = i;
}
}
- if(tbl->column_order[col_copy_idx].output_type == CELL_OUT_UNINITIALIZED) {
- *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_copy_idx);
+ if(tbl->column_order[col_inst_idx].output_type == CELL_OUT_UNINITIALIZED) {
+ *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_inst_idx);
return true;
}
.type = COL_TYPE_SIZE,
};
-static bool table_set_col_opt_timestamp(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
+static bool 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_copy_idx].idx;
+ int col_type_idx = tbl->column_order[col_inst_idx].idx;
if(tbl->columns[col_type_idx].type != COL_TYPE_TIMESTAMP) {
*err = NULL;
return false;
}
if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
- tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH;
+ tbl->column_order[col_inst_idx].output_type = TIMESTAMP_EPOCH;
} else if(strcasecmp(col_arg, "datetime") == 0) {
- tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME;
+ tbl->column_order[col_inst_idx].output_type = TIMESTAMP_DATETIME;
} else {
- *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
+ *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_inst_idx);
return true;
}
u64 curr_val = val;
if(curr_col->output_type == CELL_OUT_UNINITIALIZED) {
curr_val = curr_val / unit_div[UNIT_SIZE_BYTE];
- out_type = 0;
+ out_type = UNIT_SIZE_BYTE;
} else {
curr_val = curr_val / unit_div[curr_col->output_type];
out_type = curr_col->output_type;
#include <ucw/table.h>
enum size_units {
- UNIT_SIZE_BYTE,
+ UNIT_SIZE_BYTE = CELL_OUT_USER_DEF_START,
UNIT_SIZE_KILOBYTE,
UNIT_SIZE_MEGABYTE,
UNIT_SIZE_GIGABYTE,
new_inst->columns = mp_alloc_zero(new_inst->pool, sizeof(struct table_column) * new_inst->column_count);
memcpy(new_inst->columns, tbl_template->columns, sizeof(struct table_column) * new_inst->column_count);
- new_inst->columns = tbl_template->columns; // FIXME: copy also columns, if there will be two instances of table, then there will be clash between the linked lists!
new_inst->col_delimiter = tbl_template->col_delimiter;
new_inst->print_header = 1;
// column type double is a special case
TABLE_COL(double, double, COL_TYPE_DOUBLE);
TABLE_COL_STR(double, double, COL_TYPE_DOUBLE);
+
+TABLE_COL(bool, bool, COL_TYPE_BOOL);
+TABLE_COL_STR(bool, bool, COL_TYPE_BOOL);
+
#undef TABLE_COL
#undef TABLE_COL_FMT
#undef TABLE_COL_STR
char *cell_content = mp_printf(tbl->pool, fmt, val);
int curr_col = tbl->columns[col].first_column;
while(curr_col != -1) {
- if(tbl->column_order[curr_col].output_type < 0) tbl->column_order[curr_col].cell_content = cell_content;
- else {
- char *cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val);
- tbl->column_order[curr_col].cell_content = cell_content_tmp;
+ char *cell_content_tmp = NULL;
+ switch(tbl->column_order[curr_col].output_type) {
+ case CELL_OUT_UNINITIALIZED:
+ cell_content_tmp = cell_content;
+ break;
+ case CELL_OUT_MACHINE_READABLE:
+ cell_content_tmp = mp_printf(tbl->pool, "%4lf", val);
+ break;
+ default:
+ cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val);
+ break;
}
+ tbl->column_order[curr_col].cell_content = cell_content_tmp;
curr_col = tbl->column_order[curr_col].next_column;
}
}
-void table_col_bool(struct table *tbl, int col, uint val)
-{
- table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
-}
-
-void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
-{
- int col = table_get_col_idx(tbl, col_name);
- table_col_bool(tbl, col, val);
-}
-
-void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
+void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, bool val)
{
ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
.table_start = table_start_blockline
};
-
-
/*** Tests ***/
#ifdef TEST
#define CELL_WIDTH_MASK (~CELL_FLAG_MASK)
#define CELL_OUT_UNINITIALIZED -1
-#define CELL_OUT_HUMAN_READABLE 0
-#define CELL_OUT_MACHINE_READABLE 1
+#define CELL_OUT_HUMAN_READABLE -2
+#define CELL_OUT_MACHINE_READABLE -3
+#define CELL_OUT_USER_DEF_START 5
struct table;
#define TBL_COLUMNS .columns = (struct table_column [])
#define TBL_COL_ORDER(order) .column_order = (struct table_col_instance *) order, .cols_to_output = ARRAY_SIZE(order)
#define TBL_COL_DELIMITER(_delimiter_) .col_delimiter = _delimiter_
-#define TBL_COL(_idx) { .idx = _idx, .output_type = -1, .next_column = -1 }
-#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = -1, .next_column = -1, .fmt = _fmt }
+#define TBL_COL(_idx) { .idx = _idx, .output_type = CELL_OUT_UNINITIALIZED, .next_column = -1 }
+#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = CELL_OUT_UNINITIALIZED, .next_column = -1, .fmt = _fmt }
#define TBL_COL_TYPE(_idx, _type) { .idx = _idx, .output_type = _type, .next_column = -1 }
#define TBL_OUTPUT_HUMAN_READABLE .formatter = &table_fmt_human_readable
TABLE_COL_PROTO(s64, s64);
TABLE_COL_PROTO(u64, u64);
-void table_col_bool(struct table *tbl, int col, uint val);
-void table_col_bool_name(struct table *tbl, const char *col_name, uint val);
-void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val);
+void table_col_bool(struct table *tbl, int col, bool val);
+void table_col_bool_name(struct table *tbl, const char *col_name, bool val);
+void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, bool val);
#undef TABLE_COL_PROTO
/**