]> mj.ucw.cz Git - libucw.git/blob - ucw/table-types.c
tableprinter: code cleanup
[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
10 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
11 {
12   int col = table_get_col_idx(tbl, col_name);
13   table_col_size(tbl, col, val);
14 }
15
16 void table_col_size(struct table *tbl, int col, u64 val)
17 {
18   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
19   ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_SIZE == tbl->columns[col].type);
20
21   tbl->last_printed_col = col;
22   tbl->row_printing_started = 1;
23
24   static u64 unit_div[] = {
25     [UNIT_BYTE] = (u64) 1,
26     [UNIT_KILOBYTE] = (u64) 1024LU,
27     [UNIT_MEGABYTE] = (u64) (1024LU * 1024LU),
28     [UNIT_GIGABYTE] = (u64) (1024LU * 1024LU * 1024LU),
29     [UNIT_TERABYTE] = (u64) (1024LU * 1024LU * 1024LU * 1024LU)
30   };
31
32   static const char *unit_suffix[] = {
33     [UNIT_BYTE] = "",
34     [UNIT_KILOBYTE] = "KB",
35     [UNIT_MEGABYTE] = "MB",
36     [UNIT_GIGABYTE] = "GB",
37     [UNIT_TERABYTE] = "TB"
38   };
39
40   // FIXME: do some rounding?
41   val = val / unit_div[tbl->column_order[col].output_type];
42
43   //tbl->col_str_ptrs[col] = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[tbl->column_order[col].output_type]);
44   table_col_printf(tbl, col, "%lu%s", val, unit_suffix[tbl->column_order[col].output_type]);
45 }
46
47 #define FORMAT_TIME_SIZE 20     // Minimum buffer size
48
49 void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val)
50 {
51   int col = table_get_col_idx(tbl, col_name);
52   table_col_size(tbl, col, val);
53 }
54
55 void table_col_timestamp(struct table *tbl, int col, u64 val)
56 {
57   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
58   ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_TIMESTAMP == tbl->columns[col].type);
59   //ASSERT(fmt != NULL);
60
61   char formatted_time_buf[FORMAT_TIME_SIZE];
62
63   time_t tmp_time = (time_t)val;
64   struct tm t = *gmtime(&tmp_time);
65
66   switch (tbl->column_order[col].output_type) {
67   case TIMESTAMP_EPOCH:
68     sprintf(formatted_time_buf, "%u", (uint) val);
69     break;
70   case TIMESTAMP_DATETIME:
71     strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
72   default:
73     break;
74   }
75
76   //tbl->col_str_ptrs[col] = mp_printf(tbl->pool, "%s", formatted_time_buf);
77   table_col_printf(tbl, col, "%s", formatted_time_buf);
78 }
79