]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter&xtypes: size now supports auto units
authorRobert Kessl <kesslr@centrum.cz>
Thu, 24 Jul 2014 12:53:50 +0000 (14:53 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Thu, 24 Jul 2014 12:53:50 +0000 (14:53 +0200)
ucw/table-test-2.c
ucw/table-test-2.t
ucw/table-types.c

index f7d6a6ed15edcb10456cf0047efffa342cc08697..57742038a7d8261d5680a5402835e457b6e57061 100644 (file)
@@ -81,7 +81,7 @@ static void do_test2(void)
   out = bfdopen_shared(1, 4096);
   struct table *tbl = table_init(&test_tbl2);
   table_set_col_order_by_name(tbl, "");
-  const char *err = table_set_option_value(tbl, "cols", "size[KB],size[MB],size[GB],size[TB],ts[datetime],ts[timestamp]");
+  const char *err = table_set_option_value(tbl, "cols", "size[KB],size[MB],size[GB],size[TB],size[auto],ts[datetime],ts[timestamp]");
   if(err) {
     opt_failure("err in table_set_option_value: '%s'.", err);
     abort();
@@ -89,12 +89,14 @@ static void do_test2(void)
   table_start(tbl, out);
 
   u64 test_time = 1403685533;
-  s64 test_size = 4LU*(1024LU * 1024LU * 1024LU);
+  s64 test_size = 4LU*(1024LU * 1024LU);
 
   table_col_size(tbl, TEST_COL0_SIZE, test_size);
   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
   table_end_row(tbl);
 
+  test_size = test_size * 1024LU;
+
   table_col_size(tbl, TEST_COL0_SIZE, test_size);
   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
   table_end_row(tbl);
index 649621cb32a98cc24cbdcf8684d36ca591a9cf98..21b60ec3f459d9aac3130e2cc5e30248748b515b 100644 (file)
@@ -6,8 +6,8 @@ Out <<EOF
          4096MB           1403685533
             4GB  2014-06-25 08:38:53
             4TB  2014-06-25 08:38:53
-           size            size            size            size                   ts                   ts
-      4194304KB          4096MB             4GB             0TB  2014-06-25 08:38:53           1403685533
-      4194304KB          4096MB             4GB             0TB  2014-06-25 08:38:53           1403685533
-   4294967296KB       4194304MB          4096GB             4TB  2014-06-25 08:38:53           1403685533
+           size            size            size            size            size                   ts                   ts
+         4096KB             4MB             0GB             0TB             4MB  2014-06-25 08:38:53           1403685533
+      4194304KB          4096MB             4GB             0TB             4GB  2014-06-25 08:38:53           1403685533
+   4294967296KB       4194304MB          4096GB             4TB             4TB  2014-06-25 08:38:53           1403685533
 EOF
index 1894a9a6daddf673b929461bc69ee7b3eeac75a9..3efce3a441c06e421a861d595d2d0da4c42f0435 100644 (file)
@@ -25,6 +25,21 @@ static struct unit_definition xtype_units_size[] = {
   { 0, 0, 0 }
 };
 
+static enum size_units xt_size_auto_units(u64 sz)
+{
+  if(sz >= xtype_units_size[SIZE_UNIT_TERABYTE].num) {
+    return SIZE_UNIT_TERABYTE;
+  } else if(sz >= xtype_units_size[SIZE_UNIT_GIGABYTE].num) {
+    return SIZE_UNIT_GIGABYTE;
+  } else if(sz >= xtype_units_size[SIZE_UNIT_MEGABYTE].num) {
+    return SIZE_UNIT_MEGABYTE;
+  } else if(sz >= xtype_units_size[SIZE_UNIT_KILOBYTE].num) {
+    return SIZE_UNIT_KILOBYTE;
+  }
+
+  return SIZE_UNIT_BYTE;
+}
+
 static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool)
 {
   u64 curr_val = *(u64*) src;
@@ -39,8 +54,13 @@ static const char *xt_size_format(void *src, u32 fmt, struct mempool *pool)
     curr_val = curr_val;
     out_units = SIZE_UNIT_BYTE;
   } else if(fmt == XTYPE_FMT_PRETTY) {
-    curr_val = curr_val;
-    out_units = SIZE_UNIT_BYTE;
+    // the same as SIZE_UNIT_AUTO
+    out_units = xt_size_auto_units(curr_val);
+    curr_val = curr_val / xtype_units_size[out_units].num;
+  } else if((fmt & SIZE_UNITS_FIXED) != 0 && (fmt & SIZE_UNIT_AUTO) == SIZE_UNIT_AUTO) {
+    // the same as XTYPE_FMT_PRETTY
+    out_units = xt_size_auto_units(curr_val);
+    curr_val = curr_val / xtype_units_size[out_units].num;
   } else if((fmt & SIZE_UNITS_FIXED) != 0) {
     curr_val = curr_val / xtype_units_size[fmt & ~SIZE_UNITS_FIXED].num;
     out_units = fmt & ~SIZE_UNITS_FIXED;
@@ -60,6 +80,11 @@ static const char *xt_size_fmt_parse(const char *opt_str, u32 *dest, struct memp
     return NULL;
   }
 
+  if(strcmp(opt_str, "auto") == 0) {
+    *dest = SIZE_UNIT_AUTO | SIZE_UNITS_FIXED;
+    return NULL;
+  }
+
   int unit_idx = xtype_unit_parser(opt_str, xtype_units_size);
   if(unit_idx == -1) {
     return mp_printf(pool, "Unknown option '%s'", opt_str);