]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: fix of size/timestamp printing
authorRobert Kessl <kesslr@centrum.cz>
Tue, 1 Jul 2014 06:18:17 +0000 (08:18 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Tue, 1 Jul 2014 06:18:17 +0000 (08:18 +0200)
ucw/table-test-2.c
ucw/table-test-2.t
ucw/table-types.c
ucw/table.c
ucw/table.h

index 49fce1db58270e41bfe7545c072ef6fd84688ee0..e4f45bb21bdc24d921ada78d9ba7d4339d901719 100644 (file)
@@ -53,6 +53,13 @@ static void do_test(void)
   table_col_timestamp(&test_tbl, TEST_COL1_TS, test_time);
   table_end_row(&test_tbl);
 
+  test_size = test_size * 1024LU;
+  test_tbl.column_order[TEST_COL0_SIZE].output_type = UNIT_TERABYTE;
+  test_tbl.column_order[TEST_COL1_TS].output_type = TIMESTAMP_DATETIME;
+  table_col_size(&test_tbl, TEST_COL0_SIZE, test_size);
+  table_col_timestamp(&test_tbl, TEST_COL1_TS, test_time);
+  table_end_row(&test_tbl);
+
   table_end(&test_tbl);
   table_cleanup(&test_tbl);
 
index 1b933c9798919db8fb43afa0b321f15fc6f506d2..92874dc91b2f1732a7ddc751b770db58132644e0 100644 (file)
@@ -5,4 +5,5 @@ Out <<EOF
  4194304KB           1403685533
     4096MB           1403685533
        4GB  2014-06-25 08:38:53
+       4TB  2014-06-25 08:38:53
 EOF
index c689f3b7b07d3bf7b896530a3092a629a0a42717..902e2829b75c057165e0e019eb6771b5ac27d259 100644 (file)
@@ -6,6 +6,7 @@
 #include <ucw/table.h>
 #include <time.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
 {
@@ -38,10 +39,16 @@ void table_col_size(struct table *tbl, int col, u64 val)
   };
 
   // FIXME: do some rounding?
-  val = val / unit_div[tbl->column_order[col].output_type];
+  uint out_type = 0;
+  if(tbl->column_order[col].output_type == CELL_OUT_UNINITIALIZED) {
+    val = val / unit_div[UNIT_BYTE];
+    out_type = 0;
+  } else {
+    val = val / unit_div[tbl->column_order[col].output_type];
+    out_type = tbl->column_order[col].output_type;
+  }
 
-  //tbl->col_str_ptrs[col] = mp_printf(tbl->pool, "%lu%s", val, unit_suffix[tbl->column_order[col].output_type]);
-  table_col_printf(tbl, col, "%lu%s", val, unit_suffix[tbl->column_order[col].output_type]);
+  table_col_printf(tbl, col, "%lu%s", val, unit_suffix[out_type]);
 }
 
 #define FORMAT_TIME_SIZE 20    // Minimum buffer size
@@ -56,24 +63,25 @@ void table_col_timestamp(struct table *tbl, int col, u64 val)
 {
   ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
   ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_TIMESTAMP == tbl->columns[col].type);
-  //ASSERT(fmt != NULL);
 
-  char formatted_time_buf[FORMAT_TIME_SIZE];
+  char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
 
   time_t tmp_time = (time_t)val;
   struct tm t = *gmtime(&tmp_time);
 
   switch (tbl->column_order[col].output_type) {
   case TIMESTAMP_EPOCH:
-    sprintf(formatted_time_buf, "%u", (uint) val);
+  case CELL_OUT_UNINITIALIZED:
+    sprintf(formatted_time_buf, "%lu", val);
     break;
   case TIMESTAMP_DATETIME:
     strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
+    break;
   default:
+    abort();
     break;
   }
 
-  //tbl->col_str_ptrs[col] = mp_printf(tbl->pool, "%s", formatted_time_buf);
   table_col_printf(tbl, col, "%s", formatted_time_buf);
 }
 
index f93afbb9ef29a74290b5b3ccef72bcc685e8191a..34590de62af9626781cff79e33e42ec2a463e5c2 100644 (file)
@@ -156,6 +156,7 @@ void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
     int col_idx = col_order[i];
     tbl->column_order[i].idx = col_idx;
     tbl->column_order[i].cell_content = NULL;
+    tbl->column_order[i].output_type = CELL_OUT_UNINITIALIZED;
   }
   table_update_ll(tbl);
 }
@@ -226,7 +227,9 @@ static void table_set_all_cols_content(struct table *tbl, int col, char *col_con
 {
   int curr_col = tbl->columns[col].first_column;
   while(curr_col != -1) {
-    if(override == 0 && tbl->column_order[curr_col].output_type != 0) {
+    if(override == 0 && tbl->column_order[curr_col].output_type != CELL_OUT_UNINITIALIZED ) {
+      fprintf(stdout, "curr_col: %d\n", curr_col);
+      fflush(stdout);
       die("Error while setting content of all cells of a single type column, cell format should not be overriden.");
     }
     tbl->column_order[curr_col].cell_content = col_content;
@@ -275,7 +278,7 @@ static const char *table_col_default_fmts[] = {
     table_col_##_name_(tbl, col, val);\
   }
 
-#define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val UNUSED)\
+#define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
   {\
      ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
      ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
@@ -292,17 +295,39 @@ static const char *table_col_default_fmts[] = {
 
 TABLE_COL_BODIES(int, int, COL_TYPE_INT)
 TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
-TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
 TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
 TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
 TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
 TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
 TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
+
+// column type double is a special case
+TABLE_COL(double, double, COL_TYPE_DOUBLE);
+TABLE_COL_STR(double, double, COL_TYPE_DOUBLE);
 #undef TABLE_COL
 #undef TABLE_COL_FMT
 #undef TABLE_COL_STR
 #undef TABLE_COL_BODIES
 
+void table_col_double_fmt(struct table *tbl, int col, const char *fmt, double val)
+{
+  ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
+  ASSERT(tbl->columns[col].type == COL_TYPE_ANY || COL_TYPE_DOUBLE == tbl->columns[col].type);
+  ASSERT(fmt != NULL);
+  tbl->last_printed_col = col;
+  tbl->row_printing_started = 1;
+  char *cell_content = mp_printf(tbl->pool, fmt, val);
+  int curr_col = tbl->columns[col].first_column;
+  while(curr_col != -1) {
+    if(tbl->column_order[curr_col].output_type < 0) tbl->column_order[curr_col].cell_content = cell_content;
+    else {
+      char *cell_content_tmp = mp_printf(tbl->pool, "%.*lf", tbl->column_order[curr_col].output_type, val);
+      tbl->column_order[curr_col].cell_content = cell_content_tmp;
+    }
+    curr_col = tbl->column_order[curr_col].next_column;
+  }
+}
+
 void table_col_bool(struct table *tbl, int col, uint val)
 {
   table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
@@ -326,6 +351,7 @@ void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
   while(curr_col != -1) {
     switch(tbl->column_order[curr_col].output_type) {
     case CELL_OUT_HUMAN_READABLE:
+    case CELL_OUT_UNINITIALIZED:
       tbl->column_order[curr_col].cell_content = mp_printf(tbl->pool, fmt, val ? "true" : "false");
       break;
     case CELL_OUT_MACHINE_READABLE:
index 3cf48238d343c0e0d2f37cd763d6e95bacae1c3d..1e79c587c28792f644b9e45d5799d9f20128e63c 100644 (file)
@@ -99,6 +99,7 @@ enum column_type {
 #define CELL_FLAG_MASK (CELL_ALIGN_LEFT)
 #define CELL_WIDTH_MASK        (~CELL_FLAG_MASK)
 
+#define CELL_OUT_UNINITIALIZED      -1
 #define CELL_OUT_HUMAN_READABLE     0
 #define CELL_OUT_MACHINE_READABLE   1
 
@@ -196,8 +197,8 @@ struct table {
 #define TBL_COLUMNS  .columns = (struct table_column [])
 #define TBL_COL_ORDER(order) .column_order = (struct table_col_info *) order, .cols_to_output = ARRAY_SIZE(order)
 #define TBL_COL_DELIMITER(_delimiter_) .col_delimiter = _delimiter_
-#define TBL_COL(_idx) { .idx = _idx, .output_type = 0, .next_column = -1 }
-#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = 0, .next_column = -1, .fmt = _fmt }
+#define TBL_COL(_idx) { .idx = _idx, .output_type = -1, .next_column = -1 }
+#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = -1, .next_column = -1, .fmt = _fmt }
 #define TBL_COL_TYPE(_idx, _type) { .idx = _idx, .output_type = _type, .next_column = -1 }
 
 #define TBL_OUTPUT_HUMAN_READABLE     .formatter = &table_fmt_human_readable