]> mj.ucw.cz Git - libucw.git/blob - ucw/table-types.c
tableprinter: update of linked list and usage of const
[libucw.git] / ucw / table-types.c
1 #include <ucw/lib.h>
2 #include <ucw/table-types.h>
3 #include <ucw/fastbuf.h>
4 #include <ucw/table.h>
5 #include <time.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 /** xt_size **/
10
11 static const char *unit_suffix[] = {
12   [SIZE_UNIT_BYTE] = "",
13   [SIZE_UNIT_KILOBYTE] = "KB",
14   [SIZE_UNIT_MEGABYTE] = "MB",
15   [SIZE_UNIT_GIGABYTE] = "GB",
16   [SIZE_UNIT_TERABYTE] = "TB"
17 };
18
19 static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool)
20 {
21   u64 curr_val = *(u64*) src;
22   uint out_type = 0;
23
24   static u64 unit_div[] = {
25     [SIZE_UNIT_BYTE] = (u64) 1,
26     [SIZE_UNIT_KILOBYTE] = (u64) 1024LLU,
27     [SIZE_UNIT_MEGABYTE] = (u64) (1024LLU * 1024LLU),
28     [SIZE_UNIT_GIGABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU),
29     [SIZE_UNIT_TERABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU * 1024LLU)
30   };
31
32   if(fmt == XTYPE_FMT_DEFAULT || fmt == XTYPE_FMT_RAW) {
33     curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
34     out_type = SIZE_UNIT_BYTE;
35   } else if(fmt == XTYPE_FMT_PRETTY) {
36     curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
37     out_type = SIZE_UNIT_BYTE;
38   } else if((fmt & SIZE_UNITS_FIXED) != 0) {
39     curr_val = curr_val / unit_div[fmt & ~SIZE_UNITS_FIXED];
40     out_type = fmt & ~SIZE_UNITS_FIXED;
41   }
42
43   return mp_printf(pool, "%lu%s", curr_val, unit_suffix[out_type]);
44 }
45
46 bool table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
47 {
48   const struct table_column *col_def = tbl->column_order[col_inst_idx].col_def;
49   if(col_def->type_def != COL_TYPE_SIZE) {
50     *err = NULL;
51     return false;
52   }
53
54   if(col_arg == NULL || strcasecmp(col_arg, "b") == 0 || strcasecmp(col_arg, "bytes") == 0) {
55     tbl->column_order[col_inst_idx].output_type = SIZE_UNIT_BYTE | SIZE_UNITS_FIXED;
56     *err = NULL;
57     return true;
58   }
59
60   tbl->column_order[col_inst_idx].output_type = XTYPE_FMT_DEFAULT; // CELL_OUT_UNINITIALIZED;
61   for(uint i = SIZE_UNIT_BYTE; i <= SIZE_UNIT_TERABYTE; i++) {
62     if(strcasecmp(col_arg, unit_suffix[i]) == 0) {
63       tbl->column_order[col_inst_idx].output_type = i | SIZE_UNITS_FIXED;
64     }
65   }
66
67   if(tbl->column_order[col_inst_idx].output_type == XTYPE_FMT_DEFAULT) {
68     *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d (counted from 0)", col_arg, col_inst_idx);
69     return true;
70   }
71
72   *err = NULL;
73   return true;
74 }
75
76 TABLE_COL_BODY(size, u64)
77
78 const struct xtype xt_size = {
79   .size = sizeof(u64),
80   .name = "size",
81   //.parse = xt_size_parse,
82   .format = xt_size_format,
83 };
84
85 /** xt_timestamp **/
86
87 #define FORMAT_TIME_SIZE 20     // Minimum buffer size
88
89 bool table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
90 {
91   int col_type_idx = tbl->column_order[col_inst_idx].idx;
92   if(tbl->columns[col_type_idx].type_def != COL_TYPE_TIMESTAMP) {
93     *err = NULL;
94     return false;
95   }
96
97   if(col_arg == NULL) {
98     *err = NULL;
99     return true;
100   }
101
102   if(strcasecmp(col_arg, "timestamp") == 0 || strcasecmp(col_arg, "epoch") == 0) {
103     tbl->column_order[col_inst_idx].output_type = TIMESTAMP_EPOCH;
104   } else if(strcasecmp(col_arg, "datetime") == 0) {
105     tbl->column_order[col_inst_idx].output_type = TIMESTAMP_DATETIME;
106   } else {
107     *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_inst_idx);
108     return true;
109   }
110
111   *err = NULL;
112   return true;
113 }
114
115 static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool)
116 {
117   char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
118
119   time_t tmp_time = *(time_t*)src;
120   struct tm t = *gmtime(&tmp_time);
121   switch (fmt) {
122   case XTYPE_FMT_DEFAULT:
123   case XTYPE_FMT_RAW:
124     sprintf(formatted_time_buf, "%lu", tmp_time);
125     break;
126   case XTYPE_FMT_PRETTY:
127     strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
128     break;
129   default:
130     abort();
131     break;
132   }
133
134   return mp_printf(pool, "%s", formatted_time_buf);
135 }
136
137 TABLE_COL_BODY(timestamp, u64)
138
139 const struct xtype xt_timestamp = {
140   .size = sizeof(u64),
141   .name = "timestamp",
142   //.parse = xt_timestamp_parse,
143   .format = xt_timestamp_format,
144 };