]> mj.ucw.cz Git - libucw.git/blob - ucw/table-types.c
tableprinter: removed some obsolete FIXME, added some comments
[libucw.git] / ucw / table-types.c
1 /*
2  *      UCW Library -- Table printer types
3  *
4  *      (c) 2014 Robert Kessl <robert.kessl@economia.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/table-types.h>
9 #include <ucw/fastbuf.h>
10 #include <ucw/strtonum.h>
11 #include <ucw/table.h>
12 #include <time.h>
13 #include <stdio.h>
14 #include <inttypes.h>
15 #include <errno.h>
16
17 // FIXME: I seriously doubt there is any good reason for keeping
18 // these types separated from the generic xtype machinery. There
19 // is nothing special in them, which would be tightly connected
20 // to the table printer. Especially as they are already tested
21 // by xtypes-test.c.  --mj
22
23 /** xt_size **/
24
25 static const struct unit_definition xt_size_units[] = {
26   [XT_SIZE_UNIT_BYTE] = { "", 1LLU, 1 },
27   [XT_SIZE_UNIT_KILOBYTE] = { "KB", 1024LLU, 1 },
28   [XT_SIZE_UNIT_MEGABYTE] = { "MB", 1024LLU * 1024LLU, 1 },
29   [XT_SIZE_UNIT_GIGABYTE] = { "GB", 1024LLU * 1024LLU * 1024LLU, 1 },
30   [XT_SIZE_UNIT_TERABYTE] = { "TB", 1024LLU * 1024LLU * 1024LLU * 1024LLU, 1 },
31   { 0, 0, 0 }
32 };
33
34 static enum size_units xt_size_auto_units(u64 sz)
35 {
36   if(sz >= xt_size_units[XT_SIZE_UNIT_TERABYTE].num) {
37     return XT_SIZE_UNIT_TERABYTE;
38   } else if(sz >= xt_size_units[XT_SIZE_UNIT_GIGABYTE].num) {
39     return XT_SIZE_UNIT_GIGABYTE;
40   } else if(sz >= xt_size_units[XT_SIZE_UNIT_MEGABYTE].num) {
41     return XT_SIZE_UNIT_MEGABYTE;
42   } else if(sz >= xt_size_units[XT_SIZE_UNIT_KILOBYTE].num) {
43     return XT_SIZE_UNIT_KILOBYTE;
44   }
45
46   return XT_SIZE_UNIT_BYTE;
47 }
48
49 static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool)
50 {
51   u64 curr_val = *(u64*) src;
52   uint out_units;
53
54   if(fmt & XT_SIZE_FMT_FIXED_UNIT) {
55     out_units = fmt & ~XT_SIZE_FMT_FIXED_UNIT;
56   } else {
57     switch(fmt) {
58     case XTYPE_FMT_RAW:
59       return mp_printf(pool, "%"PRIu64, curr_val);
60     case XTYPE_FMT_PRETTY:
61       out_units = XT_SIZE_UNIT_AUTO;
62       break;
63     case XTYPE_FMT_DEFAULT:
64     default:
65       out_units = XT_SIZE_UNIT_BYTE;
66       break;
67     }
68   }
69
70   if(out_units == XT_SIZE_UNIT_AUTO) {
71     out_units = xt_size_auto_units(curr_val);
72   }
73   ASSERT(out_units < ARRAY_SIZE(xt_size_units));
74
75   curr_val = curr_val / xt_size_units[out_units].num;
76   return mp_printf(pool, "%"PRIu64"%s", curr_val, xt_size_units[out_units].unit);
77 }
78
79 static const char *xt_size_fmt_parse(const char *opt_str, u32 *dest, struct mempool *pool)
80 {
81   if(strlen(opt_str) == 0 || strcmp(opt_str, "B") == 0 || strcmp(opt_str, "Bytes") == 0) {
82     *dest = XT_SIZE_FMT_UNIT(XT_SIZE_UNIT_BYTE);
83     return NULL;
84   }
85
86   if(strcmp(opt_str, "auto") == 0) {
87     *dest = XT_SIZE_FMT_UNIT(XT_SIZE_UNIT_AUTO);
88     return NULL;
89   }
90
91   int unit_idx = xtype_unit_parser(opt_str, xt_size_units);
92   if(unit_idx == -1) {
93     return mp_printf(pool, "Unknown option '%s'", opt_str);
94   }
95
96   *dest = XT_SIZE_FMT_UNIT(unit_idx);
97   return NULL;
98 }
99
100 static const char *xt_size_parse(const char *str, void *dest, struct mempool *pool)
101 {
102   errno = 0;
103   const char *units_start = NULL;
104   u64 parsed;
105   const char *err = str_to_u64(&parsed, str, &units_start, 10 | STN_FLAGS);
106   if(err != NULL) {
107     return mp_printf(pool, "Invalid value of size: '%s'; number parser error: %s.", str, err);
108   }
109
110   if(*units_start == 0) {
111     *(u64*) dest = (u64) parsed;
112     return NULL;
113   }
114
115   int unit_idx = xtype_unit_parser(units_start, xt_size_units);
116   if(unit_idx == -1) {
117     return mp_printf(pool, "Invalid units: '%s'.", str);
118   }
119
120   // Overflow detection
121   u64 num = xt_size_units[unit_idx].num;
122   if(parsed && UINT64_MAX / parsed < num) {
123     return mp_printf(pool, "Size too large: '%s'.", str);
124   }
125
126   *(u64*) dest = parsed * xt_size_units[unit_idx].num;
127   return NULL;
128 }
129
130 TABLE_COL_BODY(size, u64)
131
132 const struct xtype xt_size = {
133   .size = sizeof(u64),
134   .name = "size",
135   .parse = xt_size_parse,
136   .format = xt_size_format,
137   .parse_fmt = xt_size_fmt_parse
138 };
139
140 /** xt_timestamp **/
141
142 #define FORMAT_TIME_SIZE 20     // Minimum buffer size
143
144 static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool)
145 {
146   char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
147
148   u64 tmp_time_u64 = *(u64*)src;
149   time_t tmp_time = (time_t) tmp_time_u64;
150   struct tm t = *gmtime(&tmp_time);
151   switch (fmt) {
152   case XTYPE_FMT_DEFAULT:
153   case XTYPE_FMT_RAW:
154     sprintf(formatted_time_buf, "%"PRIu64, tmp_time_u64);
155     break;
156   case XTYPE_FMT_PRETTY:
157     strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
158     break;
159   default:
160     ASSERT(0);
161     break;
162   }
163
164   return mp_strdup(pool, formatted_time_buf);
165 }
166
167 static const char *xt_timestamp_fmt_parse(const char *opt_str, u32 *dest, struct mempool *pool)
168 {
169   if(strcasecmp(opt_str, "timestamp") == 0 || strcasecmp(opt_str, "epoch") == 0) {
170     *dest = XT_TIMESTAMP_FMT_EPOCH;
171     return NULL;
172   } else if(strcasecmp(opt_str, "datetime") == 0) {
173     *dest = XT_TIMESTAMP_FMT_DATETIME;
174     return NULL;
175   }
176
177   return mp_printf(pool, "Invalid column format option: '%s'.", opt_str);
178 }
179
180 static const char *xt_timestamp_parse(const char *str, void *dest, struct mempool *pool)
181 {
182   errno = 0;
183   const char *parse_end = NULL;
184   u64 parsed;
185   const char *err = str_to_u64(&parsed, str, &parse_end, 10 | STN_FLAGS);
186   if(str == parse_end) {
187     return mp_printf(pool, "Invalid value of timestamp: '%s'; number parser error: %s.", str, err);
188   }
189
190   if(*parse_end == 0) {
191     *(u64*) dest = (u64) parsed;
192     return NULL;
193   }
194
195   struct tm parsed_time;
196   parse_end = strptime(str, "%F %T", &parsed_time);
197   if(parse_end == NULL) {
198     return mp_printf(pool, "Invalid value of timestamp: '%s'.", str);
199   }
200   if(*parse_end != 0) {
201     return mp_printf(pool, "Invalid value of timestamp: '%s'.", str);
202   }
203
204   time_t tmp_time = mktime(&parsed_time);
205   *(u64*)dest = (u64) tmp_time;
206
207   return NULL;
208 }
209
210 TABLE_COL_BODY(timestamp, u64)
211
212 const struct xtype xt_timestamp = {
213   .size = sizeof(u64),
214   .name = "timestamp",
215   .parse = xt_timestamp_parse,
216   .format = xt_timestamp_format,
217   .parse_fmt = xt_timestamp_fmt_parse
218 };