]> mj.ucw.cz Git - libucw.git/blob - ucw/table-types.c
tableprinter: update of column iterator and table_set_col_opt_size
[libucw.git] / ucw / table-types.c
1 #include <ucw/lib.h>
2 #include <ucw/config.h>
3 #include <ucw/table-types.h>
4 #include <ucw/fastbuf.h>
5 #include <ucw/config.h>
6 #include <ucw/table.h>
7 #include <time.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 static const char *unit_suffix[] = {
12   [UNIT_BYTE] = "",
13   [UNIT_KILOBYTE] = "KB",
14   [UNIT_MEGABYTE] = "MB",
15   [UNIT_GIGABYTE] = "GB",
16   [UNIT_TERABYTE] = "TB"
17 };
18
19 static bool table_set_col_opt_size(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
20 {
21   int col_type_idx = tbl->column_order[col_copy_idx].idx;
22   if(tbl->columns[col_type_idx].type != COL_TYPE_SIZE) {
23     *err = NULL;
24     return false;
25   }
26
27   if(strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
28     tbl->column_order[col_copy_idx].output_type = UNIT_BYTE;
29   }
30
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;
35     }
36   }
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);
39     return true;
40   }
41
42   *err = NULL;
43   return true;
44 }
45
46 struct table_user_type table_type_size = {
47   .set_col_instance_option = table_set_col_opt_size,
48   .type = COL_TYPE_SIZE,
49 };
50
51 static bool table_set_col_opt_timestamp(struct table *tbl, uint col_copy_idx, const char *col_arg, char **err)
52 {
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;
59     } else {
60       *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
61       return true;
62     }
63     *err = NULL;
64     return true;
65   }
66
67   *err = NULL;
68   return false;
69 }
70
71 struct table_user_type table_type_timestamp = {
72   .set_col_instance_option = table_set_col_opt_timestamp,
73   .type = COL_TYPE_TIMESTAMP,
74 };
75
76 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
77 {
78   int col = table_get_col_idx(tbl, col_name);
79   table_col_size(tbl, col, val);
80 }
81
82 void table_col_size(struct table *tbl, int col, u64 val)
83 {
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);
86
87   tbl->last_printed_col = col;
88   tbl->row_printing_started = 1;
89
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)
96   };
97
98   TBL_COL_ITER(tbl, col, curr_col) {
99     // FIXME: do some rounding?
100     uint out_type = 0;
101     if(tbl->column_order[curr_col].output_type == CELL_OUT_UNINITIALIZED) {
102       val = val / unit_div[UNIT_BYTE];
103       out_type = 0;
104     } else {
105       val = val / unit_div[tbl->column_order[curr_col].output_type];
106       out_type = tbl->column_order[curr_col].output_type;
107     }
108
109     tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[out_type]);
110   }
111 }
112
113 #define FORMAT_TIME_SIZE 20     // Minimum buffer size
114
115 void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val)
116 {
117   int col = table_get_col_idx(tbl, col_name);
118   table_col_size(tbl, col, val);
119 }
120
121 void table_col_timestamp(struct table *tbl, int col, u64 val)
122 {
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);
125
126   char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
127
128   time_t tmp_time = (time_t)val;
129   struct tm t = *gmtime(&tmp_time);
130
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);
136       break;
137     case TIMESTAMP_DATETIME:
138       strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
139     break;
140     default:
141       abort();
142       break;
143     }
144
145     tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf);
146   }
147 }