]> mj.ucw.cz Git - libucw.git/blob - ucw/table-types.c
tableprinter: added parsing of column arg; bugfixes
[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 bool table_set_col_opt_ucw_types(struct table *tbl, int col_copy_idx, const char *col_arg, char **err)
12 {
13   fprintf(stdout, "col_copy_idx: %d, col_arg: %s\n", col_copy_idx, col_arg);
14   fflush(stdout);
15
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;
28     } else {
29       *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
30       return true;
31     }
32     *err = NULL;
33     return true;
34   }
35
36   if(tbl->columns[col_type_idx].type == COL_TYPE_TIMESTAMP) {
37     fprintf(stdout, "setting timestamp format, col_arg: '%s'\n", col_arg);
38     fflush(stdout);
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;
43     } else {
44       *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
45       return true;
46     }
47     *err = NULL;
48     return true;
49   }
50
51   *err = mp_printf(tbl->pool, "Tableprinter: invalid column format option: '%s' for column %d.", col_arg, col_copy_idx);
52   return false;
53 }
54
55
56 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
57 {
58   int col = table_get_col_idx(tbl, col_name);
59   table_col_size(tbl, col, val);
60 }
61
62 void table_col_size(struct table *tbl, int col, u64 val)
63 {
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);
66
67   tbl->last_printed_col = col;
68   tbl->row_printing_started = 1;
69
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)
76   };
77
78   static const char *unit_suffix[] = {
79     [UNIT_BYTE] = "",
80     [UNIT_KILOBYTE] = "KB",
81     [UNIT_MEGABYTE] = "MB",
82     [UNIT_GIGABYTE] = "GB",
83     [UNIT_TERABYTE] = "TB"
84   };
85
86   int curr_col = tbl->columns[col].first_column;
87   while(curr_col != -1) {
88
89     // FIXME: do some rounding?
90     uint out_type = 0;
91     if(tbl->column_order[curr_col].output_type == CELL_OUT_UNINITIALIZED) {
92       val = val / unit_div[UNIT_BYTE];
93       out_type = 0;
94     } else {
95       val = val / unit_div[tbl->column_order[curr_col].output_type];
96       out_type = tbl->column_order[curr_col].output_type;
97     }
98
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;
101   }
102
103 }
104
105 #define FORMAT_TIME_SIZE 20     // Minimum buffer size
106
107 void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val)
108 {
109   int col = table_get_col_idx(tbl, col_name);
110   table_col_size(tbl, col, val);
111 }
112
113 void table_col_timestamp(struct table *tbl, int col, u64 val)
114 {
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);
117
118   char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
119
120   time_t tmp_time = (time_t)val;
121   struct tm t = *gmtime(&tmp_time);
122
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);
129       break;
130     case TIMESTAMP_DATETIME:
131       strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
132     break;
133     default:
134       abort();
135       break;
136     }
137
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;
141   }
142 }
143