2 #include <ucw/config.h>
3 #include <ucw/table-types.h>
4 #include <ucw/fastbuf.h>
5 #include <ucw/config.h>
11 static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
13 int col_type_idx = tbl->column_order[col_copy_idx].idx;
14 if(tbl->columns[col_type_idx].type == COL_TYPE_SIZE) {
15 if(strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
16 tbl->column_order[col_copy_idx].output_type = UNIT_BYTE;
17 } else if(strcasecmp(col_arg, "kb") == 0) {
18 tbl->column_order[col_copy_idx].output_type = UNIT_KILOBYTE;
19 } else if(strcasecmp(col_arg, "mb") == 0) {
20 tbl->column_order[col_copy_idx].output_type = UNIT_MEGABYTE;
21 } else if(strcasecmp(col_arg, "gb") == 0) {
22 tbl->column_order[col_copy_idx].output_type = UNIT_GIGABYTE;
23 } else if(strcasecmp(col_arg, "tb") == 0) {
24 tbl->column_order[col_copy_idx].output_type = UNIT_TERABYTE;
26 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_copy_idx);
37 struct table_user_type table_type_size = {
38 .set_col_instance_option = table_set_col_opt_size,
39 .type = COL_TYPE_SIZE,
42 static bool table_set_col_opt_timestamp(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
44 int col_type_idx = tbl->column_order[col_copy_idx].idx;
45 if(tbl->columns[col_type_idx].type == COL_TYPE_TIMESTAMP) {
46 if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
47 tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH;
48 } else if(strcasecmp(col_arg, "datetime") == 0) {
49 tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME;
51 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
62 struct table_user_type table_type_timestamp = {
63 .set_col_instance_option = table_set_col_opt_timestamp,
64 .type = COL_TYPE_TIMESTAMP,
67 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
69 int col = table_get_col_idx(tbl, col_name);
70 table_col_size(tbl, col, val);
73 void table_col_size(struct table *tbl, int col, u64 val)
75 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
76 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_SIZE == tbl->columns[col].type);
78 tbl->last_printed_col = col;
79 tbl->row_printing_started = 1;
81 static u64 unit_div[] = {
82 [UNIT_BYTE] = (u64) 1,
83 [UNIT_KILOBYTE] = (u64) 1024LLU,
84 [UNIT_MEGABYTE] = (u64) (1024LLU * 1024LLU),
85 [UNIT_GIGABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU),
86 [UNIT_TERABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU * 1024LLU)
89 static const char *unit_suffix[] = {
91 [UNIT_KILOBYTE] = "KB",
92 [UNIT_MEGABYTE] = "MB",
93 [UNIT_GIGABYTE] = "GB",
94 [UNIT_TERABYTE] = "TB"
97 TBL_COL_ITER_START(tbl, col, curr_col) {
98 // FIXME: do some rounding?
100 if(tbl->column_order[curr_col].output_type == CELL_OUT_UNINITIALIZED) {
101 val = val / unit_div[UNIT_BYTE];
104 val = val / unit_div[tbl->column_order[curr_col].output_type];
105 out_type = tbl->column_order[curr_col].output_type;
108 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]);
109 } TBL_COL_ITER_END(tbl, curr_col)
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_START(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);
146 } TBL_COL_ITER_END(tbl, curr_col)