2 #include <ucw/table-types.h>
3 #include <ucw/fastbuf.h>
11 static const char *unit_suffix[] = {
12 [SIZE_UNIT_BYTE] = "",
13 [SIZE_UNIT_KILOBYTE] = "KB",
14 [SIZE_UNIT_MEGABYTE] = "MB",
15 [SIZE_UNIT_GIGABYTE] = "GB",
16 [SIZE_UNIT_TERABYTE] = "TB"
19 static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool)
21 u64 curr_val = *(u64*) src;
24 static u64 unit_div[] = {
25 [SIZE_UNIT_BYTE] = (u64) 1,
26 [SIZE_UNIT_KILOBYTE] = (u64) 1024LLU,
27 [SIZE_UNIT_MEGABYTE] = (u64) (1024LLU * 1024LLU),
28 [SIZE_UNIT_GIGABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU),
29 [SIZE_UNIT_TERABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU * 1024LLU)
32 if(fmt == XTYPE_FMT_DEFAULT || fmt == XTYPE_FMT_RAW) {
33 curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
34 out_type = SIZE_UNIT_BYTE;
35 } else if(fmt == XTYPE_FMT_PRETTY) {
36 curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
37 out_type = SIZE_UNIT_BYTE;
38 } else if((fmt & SIZE_UNITS_FIXED) != 0) {
39 curr_val = curr_val / unit_div[fmt & ~SIZE_UNITS_FIXED];
40 out_type = fmt & ~SIZE_UNITS_FIXED;
43 return mp_printf(pool, "%lu%s", curr_val, unit_suffix[out_type]);
46 bool table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
48 struct table_column *col_def = tbl->column_order[col_inst_idx].col_def;
49 if(col_def->type_def != COL_TYPE_SIZE) {
54 if(col_arg == NULL || strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
55 tbl->column_order[col_inst_idx].output_type = SIZE_UNIT_BYTE | SIZE_UNITS_FIXED;
60 tbl->column_order[col_inst_idx].output_type = XTYPE_FMT_DEFAULT; // CELL_OUT_UNINITIALIZED;
61 for(uint i = SIZE_UNIT_BYTE; i <= SIZE_UNIT_TERABYTE; i++) {
62 if(strcasecmp(col_arg, unit_suffix[i]) == 0) {
63 tbl->column_order[col_inst_idx].output_type = i | SIZE_UNITS_FIXED;
67 if(tbl->column_order[col_inst_idx].output_type == XTYPE_FMT_DEFAULT) {
68 *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_inst_idx);
76 TABLE_COL_BODY(size, u64)
78 const struct xtype xt_size = {
81 //.parse = xt_size_parse,
82 .format = xt_size_format,
87 #define FORMAT_TIME_SIZE 20 // Minimum buffer size
89 bool table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
91 int col_type_idx = tbl->column_order[col_inst_idx].idx;
92 if(tbl->columns[col_type_idx].type_def != COL_TYPE_TIMESTAMP) {
102 if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
103 tbl->column_order[col_inst_idx].output_type = TIMESTAMP_EPOCH;
104 } else if(strcasecmp(col_arg, "datetime") == 0) {
105 tbl->column_order[col_inst_idx].output_type = TIMESTAMP_DATETIME;
107 *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_inst_idx);
115 static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool)
117 char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
119 time_t tmp_time = *(time_t*)src;
120 struct tm t = *gmtime(&tmp_time);
122 case XTYPE_FMT_DEFAULT:
124 sprintf(formatted_time_buf, "%lu", tmp_time);
126 case XTYPE_FMT_PRETTY:
127 strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
134 return mp_printf(pool, "%s", formatted_time_buf);
137 TABLE_COL_BODY(timestamp, u64)
139 const struct xtype xt_timestamp = {
142 //.parse = xt_timestamp_parse,
143 .format = xt_timestamp_format,