2 * UCW Library -- Table printer types
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
8 #include <ucw/table-types.h>
9 #include <ucw/fastbuf.h>
10 #include <ucw/table.h>
18 static const char *unit_suffix[] = {
19 [SIZE_UNIT_BYTE] = "",
20 [SIZE_UNIT_KILOBYTE] = "KB",
21 [SIZE_UNIT_MEGABYTE] = "MB",
22 [SIZE_UNIT_GIGABYTE] = "GB",
23 [SIZE_UNIT_TERABYTE] = "TB"
26 static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool)
28 u64 curr_val = *(u64*) src;
30 if(fmt == XTYPE_FMT_RAW) {
31 return mp_printf(pool, "%"PRIu64, curr_val);
34 uint out_type = SIZE_UNIT_BYTE;
36 static u64 unit_div[] = {
37 [SIZE_UNIT_BYTE] = 1LLU,
38 [SIZE_UNIT_KILOBYTE] = 1024LLU,
39 [SIZE_UNIT_MEGABYTE] = 1024LLU * 1024LLU,
40 [SIZE_UNIT_GIGABYTE] = 1024LLU * 1024LLU * 1024LLU,
41 [SIZE_UNIT_TERABYTE] = 1024LLU * 1024LLU * 1024LLU * 1024LLU
44 if(fmt == XTYPE_FMT_DEFAULT) {
45 curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
46 out_type = SIZE_UNIT_BYTE;
47 } else if(fmt == XTYPE_FMT_PRETTY) {
48 curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
49 out_type = SIZE_UNIT_BYTE;
50 } else if((fmt & SIZE_UNITS_FIXED) != 0) {
51 curr_val = curr_val / unit_div[fmt & ~SIZE_UNITS_FIXED];
52 out_type = fmt & ~SIZE_UNITS_FIXED;
55 return mp_printf(pool, "%"PRIu64"%s", curr_val, unit_suffix[out_type]);
58 static const char * xt_size_fmt_parse(const char *opt_str, u32 *dest, struct mempool *pool UNUSED)
61 return "NULL is not supported as a column argument.";
64 if(strlen(opt_str) == 0 || strcasecmp(opt_str, "b") == 0 || strcasecmp(opt_str, "bytes") == 0) {
65 *dest = SIZE_UNIT_BYTE | SIZE_UNITS_FIXED;
69 for(uint i = SIZE_UNIT_BYTE; i <= SIZE_UNIT_TERABYTE; i++) {
70 if(strcasecmp(opt_str, unit_suffix[i]) == 0) {
71 *dest = i | SIZE_UNITS_FIXED;
76 return "Unknown option.";
79 TABLE_COL_BODY(size, u64)
81 const struct xtype xt_size = {
84 //.parse = xt_size_parse,
85 .format = xt_size_format,
86 .parse_fmt = xt_size_fmt_parse
91 #define FORMAT_TIME_SIZE 20 // Minimum buffer size
93 static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool)
95 char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
97 u64 tmp_time_u64 = *(u64*)src;
98 time_t tmp_time = (time_t) tmp_time_u64;
99 struct tm t = *gmtime(&tmp_time);
101 case XTYPE_FMT_DEFAULT:
103 sprintf(formatted_time_buf, "%"PRIu64, tmp_time_u64);
105 case XTYPE_FMT_PRETTY:
106 strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
113 return mp_printf(pool, "%s", formatted_time_buf);
116 static const char * xt_timestamp_fmt_parse(const char *opt_str, u32 *dest, struct mempool *pool)
118 if(opt_str == NULL) {
119 return "NULL is not supported as a column argument.";
122 if(strcasecmp(opt_str, "timestamp") == 0 || strcasecmp(opt_str, "epoch") == 0) {
123 *dest = TIMESTAMP_EPOCH;
125 } else if(strcasecmp(opt_str, "datetime") == 0) {
126 *dest = TIMESTAMP_DATETIME;
130 return mp_printf(pool, "Invalid column format option: '%s'.", opt_str);
133 TABLE_COL_BODY(timestamp, u64)
135 const struct xtype xt_timestamp = {
138 //.parse = xt_timestamp_parse,
139 .format = xt_timestamp_format,
140 .parse_fmt = xt_timestamp_fmt_parse