2 #include <ucw/config.h>
3 #include <ucw/table-types.h>
4 #include <ucw/fastbuf.h>
5 #include <ucw/config.h>
11 static const char *unit_suffix[] = {
13 [UNIT_KILOBYTE] = "KB",
14 [UNIT_MEGABYTE] = "MB",
15 [UNIT_GIGABYTE] = "GB",
16 [UNIT_TERABYTE] = "TB"
19 static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
21 int col_type_idx = tbl->column_order[col_copy_idx].idx;
22 if(tbl->columns[col_type_idx].type != COL_TYPE_SIZE) {
27 if(strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
28 tbl->column_order[col_copy_idx].output_type = UNIT_BYTE;
31 tbl->column_order[col_copy_idx].output_type = CELL_OUT_UNINITIALIZED;
32 for(int i = 0; i < ARRAY_SIZE(unit_suffix); i++) {
33 if(strcasecmp(col_arg, unit_suffix[i]) == 0) {
34 tbl->column_order[col_copy_idx].output_type = i;
37 if(tbl->column_order[col_copy_idx].output_type == CELL_OUT_UNINITIALIZED) {
38 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_copy_idx);
46 struct table_user_type table_type_size = {
47 .set_col_instance_option = table_set_col_opt_size,
48 .type = COL_TYPE_SIZE,
51 static bool table_set_col_opt_timestamp(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
53 int col_type_idx = tbl->column_order[col_copy_idx].idx;
54 if(tbl->columns[col_type_idx].type == COL_TYPE_TIMESTAMP) {
55 if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
56 tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH;
57 } else if(strcasecmp(col_arg, "datetime") == 0) {
58 tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME;
60 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
71 struct table_user_type table_type_timestamp = {
72 .set_col_instance_option = table_set_col_opt_timestamp,
73 .type = COL_TYPE_TIMESTAMP,
76 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
78 int col = table_get_col_idx(tbl, col_name);
79 table_col_size(tbl, col, val);
82 void table_col_size(struct table *tbl, int col, u64 val)
84 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
85 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_SIZE == tbl->columns[col].type);
87 tbl->last_printed_col = col;
88 tbl->row_printing_started = 1;
90 static u64 unit_div[] = {
91 [UNIT_BYTE] = (u64) 1,
92 [UNIT_KILOBYTE] = (u64) 1024LLU,
93 [UNIT_MEGABYTE] = (u64) (1024LLU * 1024LLU),
94 [UNIT_GIGABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU),
95 [UNIT_TERABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU * 1024LLU)
98 TBL_COL_ITER(tbl, col, curr_col) {
99 // FIXME: do some rounding?
101 if(tbl->column_order[curr_col].output_type == CELL_OUT_UNINITIALIZED) {
102 val = val / unit_div[UNIT_BYTE];
105 val = val / unit_div[tbl->column_order[curr_col].output_type];
106 out_type = tbl->column_order[curr_col].output_type;
109 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]);
113 #define FORMAT_TIME_SIZE 20 // Minimum buffer size
115 void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val)
117 int col = table_get_col_idx(tbl, col_name);
118 table_col_size(tbl, col, val);
121 void table_col_timestamp(struct table *tbl, int col, u64 val)
123 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
124 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_TIMESTAMP == tbl->columns[col].type);
126 char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
128 time_t tmp_time = (time_t)val;
129 struct tm t = *gmtime(&tmp_time);
131 TBL_COL_ITER(tbl, col, curr_col) {
132 switch (tbl->column_order[curr_col].output_type) {
133 case TIMESTAMP_EPOCH:
134 case CELL_OUT_UNINITIALIZED:
135 sprintf(formatted_time_buf, "%lu", val);
137 case TIMESTAMP_DATETIME:
138 strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
145 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf);