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) 1024LU,
84 [UNIT_MEGABYTE] = (u64) (1024LU * 1024LU),
85 [UNIT_GIGABYTE] = (u64) (1024LU * 1024LU * 1024LU),
86 [UNIT_TERABYTE] = (u64) (1024LU * 1024LU * 1024LU * 1024LU)
89 static const char *unit_suffix[] = {
91 [UNIT_KILOBYTE] = "KB",
92 [UNIT_MEGABYTE] = "MB",
93 [UNIT_GIGABYTE] = "GB",
94 [UNIT_TERABYTE] = "TB"
97 int curr_col = tbl->columns[col].first_column;
98 while(curr_col != -1) {
100 // FIXME: do some rounding?
102 if(tbl->column_order[curr_col].output_type == CELL_OUT_UNINITIALIZED) {
103 val = val / unit_div[UNIT_BYTE];
106 val = val / unit_div[tbl->column_order[curr_col].output_type];
107 out_type = tbl->column_order[curr_col].output_type;
110 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]);
111 curr_col = tbl->column_order[curr_col].next_column;
116 #define FORMAT_TIME_SIZE 20 // Minimum buffer size
118 void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val)
120 int col = table_get_col_idx(tbl, col_name);
121 table_col_size(tbl, col, val);
124 void table_col_timestamp(struct table *tbl, int col, u64 val)
126 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
127 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_TIMESTAMP == tbl->columns[col].type);
129 char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
131 time_t tmp_time = (time_t)val;
132 struct tm t = *gmtime(&tmp_time);
134 int curr_col = tbl->columns[col].first_column;
135 while(curr_col != -1) {
136 switch (tbl->column_order[curr_col].output_type) {
137 case TIMESTAMP_EPOCH:
138 case CELL_OUT_UNINITIALIZED:
139 sprintf(formatted_time_buf, "%lu", val);
141 case TIMESTAMP_DATETIME:
142 strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
149 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf);
150 curr_col = tbl->column_order[curr_col].next_column;