2 #include <ucw/config.h>
3 #include <ucw/table-types.h>
4 #include <ucw/fastbuf.h>
5 #include <ucw/config.h>
11 bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char *col_arg, char **err)
13 fprintf(stdout, "col_copy_idx: %d, col_arg: %s\n", col_copy_idx, col_arg);
16 int col_type_idx = tbl->column_order[col_copy_idx].idx;
17 if(tbl->columns[col_type_idx].type == COL_TYPE_SIZE) {
18 if(strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
19 tbl->column_order[col_copy_idx].output_type = UNIT_BYTE;
20 } else if(strcasecmp(col_arg, "kb") == 0) {
21 tbl->column_order[col_copy_idx].output_type = UNIT_KILOBYTE;
22 } else if(strcasecmp(col_arg, "mb") == 0) {
23 tbl->column_order[col_copy_idx].output_type = UNIT_MEGABYTE;
24 } else if(strcasecmp(col_arg, "gb") == 0) {
25 tbl->column_order[col_copy_idx].output_type = UNIT_GIGABYTE;
26 } else if(strcasecmp(col_arg, "tb") == 0) {
27 tbl->column_order[col_copy_idx].output_type = UNIT_TERABYTE;
29 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
36 if(tbl->columns[col_type_idx].type == COL_TYPE_TIMESTAMP) {
37 fprintf(stdout, "setting timestamp format, col_arg: '%s'\n", col_arg);
39 if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
40 tbl->column_order[col_copy_idx].output_type = TIMESTAMP_EPOCH;
41 } else if(strcasecmp(col_arg, "datetime") == 0) {
42 tbl->column_order[col_copy_idx].output_type = TIMESTAMP_DATETIME;
44 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
51 *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
56 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
58 int col = table_get_col_idx(tbl, col_name);
59 table_col_size(tbl, col, val);
62 void table_col_size(struct table *tbl, int col, u64 val)
64 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
65 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_SIZE == tbl->columns[col].type);
67 tbl->last_printed_col = col;
68 tbl->row_printing_started = 1;
70 static u64 unit_div[] = {
71 [UNIT_BYTE] = (u64) 1,
72 [UNIT_KILOBYTE] = (u64) 1024LU,
73 [UNIT_MEGABYTE] = (u64) (1024LU * 1024LU),
74 [UNIT_GIGABYTE] = (u64) (1024LU * 1024LU * 1024LU),
75 [UNIT_TERABYTE] = (u64) (1024LU * 1024LU * 1024LU * 1024LU)
78 static const char *unit_suffix[] = {
80 [UNIT_KILOBYTE] = "KB",
81 [UNIT_MEGABYTE] = "MB",
82 [UNIT_GIGABYTE] = "GB",
83 [UNIT_TERABYTE] = "TB"
86 int curr_col = tbl->columns[col].first_column;
87 while(curr_col != -1) {
89 // FIXME: do some rounding?
91 if(tbl->column_order[curr_col].output_type == CELL_OUT_UNINITIALIZED) {
92 val = val / unit_div[UNIT_BYTE];
95 val = val / unit_div[tbl->column_order[curr_col].output_type];
96 out_type = tbl->column_order[curr_col].output_type;
99 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]);
100 curr_col = tbl->column_order[curr_col].next_column;
105 #define FORMAT_TIME_SIZE 20 // Minimum buffer size
107 void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val)
109 int col = table_get_col_idx(tbl, col_name);
110 table_col_size(tbl, col, val);
113 void table_col_timestamp(struct table *tbl, int col, u64 val)
115 ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
116 ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_TIMESTAMP == tbl->columns[col].type);
118 char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
120 time_t tmp_time = (time_t)val;
121 struct tm t = *gmtime(&tmp_time);
123 int curr_col = tbl->columns[col].first_column;
124 while(curr_col != -1) {
125 switch (tbl->column_order[curr_col].output_type) {
126 case TIMESTAMP_EPOCH:
127 case CELL_OUT_UNINITIALIZED:
128 sprintf(formatted_time_buf, "%lu", val);
130 case TIMESTAMP_DATETIME:
131 strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
138 //table_col_printf(tbl, col, "%s", formatted_time_buf);
139 tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf);
140 curr_col = tbl->column_order[curr_col].next_column;