]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: update of xtypes for tableprinter
authorRobert Kessl <kesslr@centrum.cz>
Wed, 16 Jul 2014 11:41:52 +0000 (13:41 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Wed, 16 Jul 2014 11:41:52 +0000 (13:41 +0200)
ucw/table-types.c
ucw/table-types.h

index 750aa9423704d9944c0598ee876486b2162e8592..bea583a720ac45f3cbb940791acb2e3b6fd37c8f 100644 (file)
@@ -6,8 +6,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-const struct xtype xt_size;
-const struct xtype xt_timestamp;
+/** xt_size **/
 
 static const char *unit_suffix[] = {
   [SIZE_UNIT_BYTE] = "",
@@ -17,6 +16,33 @@ static const char *unit_suffix[] = {
   [SIZE_UNIT_TERABYTE] = "TB"
 };
 
+static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool)
+{
+  u64 curr_val = *(u64*) src;
+  uint out_type = 0;
+
+  static u64 unit_div[] = {
+    [SIZE_UNIT_BYTE] = (u64) 1,
+    [SIZE_UNIT_KILOBYTE] = (u64) 1024LLU,
+    [SIZE_UNIT_MEGABYTE] = (u64) (1024LLU * 1024LLU),
+    [SIZE_UNIT_GIGABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU),
+    [SIZE_UNIT_TERABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU * 1024LLU)
+  };
+
+  if(fmt == XTYPE_FMT_DEFAULT || fmt == XTYPE_FMT_RAW) {
+    curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
+    out_type = SIZE_UNIT_BYTE;
+  } else if(fmt == XTYPE_FMT_PRETTY) {
+    curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
+    out_type = SIZE_UNIT_BYTE;
+  } else if((fmt & SIZE_UNITS_FIXED) != 0) {
+    curr_val = curr_val / unit_div[fmt & ~SIZE_UNITS_FIXED];
+    out_type = fmt & ~SIZE_UNITS_FIXED;
+  }
+
+  return mp_printf(pool, "%lu%s", curr_val, unit_suffix[out_type]);
+}
+
 bool table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
 {
   struct table_column *col_def = tbl->column_order[col_inst_idx].col_def;
@@ -47,6 +73,19 @@ bool table_set_col_opt_size(struct table *tbl, uint col_inst_idx, const char *co
   return true;
 }
 
+TABLE_COL_BODY(size, u64)
+
+const struct xtype xt_size = {
+  .size = sizeof(u64),
+  .name = "size",
+  //.parse = xt_size_parse,
+  .format = xt_size_format,
+};
+
+/** xt_timestamp **/
+
+#define FORMAT_TIME_SIZE 20    // Minimum buffer size
+
 bool table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const char *col_arg, char **err)
 {
   int col_type_idx = tbl->column_order[col_inst_idx].idx;
@@ -73,80 +112,33 @@ bool table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const cha
   return true;
 }
 
-void table_col_size_name(struct table *tbl, const char *col_name, u64 val)
+static const char *xt_timestamp_format(void *src, u32 fmt, struct mempool *pool)
 {
-  int col = table_get_col_idx(tbl, col_name);
-  table_col_size(tbl, col, val);
-}
-
-void table_col_size(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_def == COL_TYPE_ANY || COL_TYPE_SIZE == tbl->columns[col].type_def);
-
-  tbl->last_printed_col = col;
-  tbl->row_printing_started = 1;
-
-  static u64 unit_div[] = {
-    [SIZE_UNIT_BYTE] = (u64) 1,
-    [SIZE_UNIT_KILOBYTE] = (u64) 1024LLU,
-    [SIZE_UNIT_MEGABYTE] = (u64) (1024LLU * 1024LLU),
-    [SIZE_UNIT_GIGABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU),
-    [SIZE_UNIT_TERABYTE] = (u64) (1024LLU * 1024LLU * 1024LLU * 1024LLU)
-  };
-
-  // FIXME: add the SIZE_UNIT_AUTO
-  TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
-    // FIXME: do some rounding? Or maybe use double and floating-point printing?
-    uint out_type = 0;
-    u64 curr_val = val;
-
-    if(curr_col->output_type == XTYPE_FMT_DEFAULT || curr_col->output_type == XTYPE_FMT_RAW) {
-      curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
-      out_type = SIZE_UNIT_BYTE;
-    } else if(curr_col->output_type == XTYPE_FMT_PRETTY) {
-      curr_val = curr_val / unit_div[SIZE_UNIT_BYTE];
-      out_type = SIZE_UNIT_BYTE;
-    } else if((curr_col->output_type & SIZE_UNITS_FIXED) != 0) {
-      curr_val = curr_val / unit_div[curr_col->output_type & ~SIZE_UNITS_FIXED];
-      out_type = curr_col->output_type & ~SIZE_UNITS_FIXED;
-    }
-
-    curr_col->cell_content = mp_printf(tbl->pool, "%lu%s", curr_val, unit_suffix[out_type]);
-  } TBL_COL_ITER_END
-}
-
-#define FORMAT_TIME_SIZE 20    // Minimum buffer size
-
-void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val)
-{
-  int col = table_get_col_idx(tbl, col_name);
-  table_col_size(tbl, col, val);
-}
-
-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_def == COL_TYPE_ANY || COL_TYPE_TIMESTAMP == tbl->columns[col].type_def);
-
   char formatted_time_buf[FORMAT_TIME_SIZE] = { 0 };
 
-  time_t tmp_time = (time_t)val;
+  time_t tmp_time = *(time_t*)src;
   struct tm t = *gmtime(&tmp_time);
-  TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
-    switch (curr_col->output_type) {
-    case XTYPE_FMT_DEFAULT:
-    case XTYPE_FMT_RAW:
-      sprintf(formatted_time_buf, "%lu", val);
-      break;
-    case XTYPE_FMT_PRETTY:
-      strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
+  switch (fmt) {
+  case XTYPE_FMT_DEFAULT:
+  case XTYPE_FMT_RAW:
+    sprintf(formatted_time_buf, "%lu", tmp_time);
     break;
-    default:
-      abort();
-      break;
-    }
+  case XTYPE_FMT_PRETTY:
+    strftime(formatted_time_buf, FORMAT_TIME_SIZE, "%F %T", &t);
+    break;
+  default:
+    abort();
+    break;
+  }
 
-    curr_col->cell_content = mp_printf(tbl->pool, "%s", formatted_time_buf);
-  } TBL_COL_ITER_END
+  return mp_printf(pool, "%s", formatted_time_buf);
 }
+
+TABLE_COL_BODY(timestamp, u64)
+
+const struct xtype xt_timestamp = {
+  .size = sizeof(u64),
+  .name = "timestamp",
+  //.parse = xt_timestamp_parse,
+  .format = xt_timestamp_format,
+};
index 8ca14f6ee29465439d0332267ed124d9613c302f..6cdc8d590f66e98a3c4a06dfc6d9bd8e36e1a71d 100644 (file)
@@ -38,10 +38,15 @@ bool table_set_col_opt_timestamp(struct table *tbl, uint col_inst_idx, const cha
 #define TBL_COL_SIZE_FMT(_name, _width, _units)         { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_SIZE, .set_col_instance_option = table_set_col_opt_size }
 #define TBL_COL_TIMESTAMP_FMT(_name, _width, _fmt)      { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_TIMESTAMP, .set_col_instance_option = table_set_col_opt_timestamp }
 
+
+/*
 void table_col_size_name(struct table *tbl, const char *col_name, u64 val);
 void table_col_timestamp_name(struct table *tbl, const char * col_name, u64 val);
 
 void table_col_size(struct table *tbl, int col, u64 val);
 void table_col_timestamp(struct table *tbl, int col, u64 val);
+*/
+TABLE_COL_PROTO(size, u64)
+TABLE_COL_PROTO(timestamp, u64)
 
 #endif