From: Robert Kessl Date: Thu, 24 Jul 2014 12:53:50 +0000 (+0200) Subject: tableprinter&xtypes: size now supports auto units X-Git-Tag: v6.1~3^2~50 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=24ad1fdf22b5df09bee1e20255d965f9a65c56ed;p=libucw.git tableprinter&xtypes: size now supports auto units --- diff --git a/ucw/table-test-2.c b/ucw/table-test-2.c index f7d6a6ed..57742038 100644 --- a/ucw/table-test-2.c +++ b/ucw/table-test-2.c @@ -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); diff --git a/ucw/table-test-2.t b/ucw/table-test-2.t index 649621cb..21b60ec3 100644 --- a/ucw/table-test-2.t +++ b/ucw/table-test-2.t @@ -6,8 +6,8 @@ Out <= 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);