diff options
author | João Valverde <j@v6e.pt> | 2021-11-29 19:29:55 +0000 |
---|---|---|
committer | Wireshark GitLab Utility <gerald+gitlab-utility@wireshark.org> | 2021-11-29 22:13:32 +0000 |
commit | 504de90a3ca325b1a8e279e87cdb3396a7dfa73d (patch) | |
tree | 70347df60693b21a78e4467b2568e2b4679efdaf /wsutil | |
parent | 51f2a56b7c2c1f13242503f21498d6023631c045 (diff) |
wsutil: Split format_size() enum
Use an enum to select units and a bit flag for the other options,
currently only prefix type.
Diffstat (limited to 'wsutil')
-rw-r--r-- | wsutil/str_util.c | 23 | ||||
-rw-r--r-- | wsutil/str_util.h | 31 | ||||
-rw-r--r-- | wsutil/test_wsutil.c | 6 |
3 files changed, 30 insertions, 30 deletions
diff --git a/wsutil/str_util.c b/wsutil/str_util.c index 5426c504b9..eec1bee148 100644 --- a/wsutil/str_util.c +++ b/wsutil/str_util.c @@ -197,8 +197,9 @@ DIAG_ON(format) /* Given a size, return its value in a human-readable format */ /* This doesn't handle fractional values. We might want to make size a double. */ -gchar * -format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e flags) +char * +format_size_wmem(wmem_allocator_t *allocator, int64_t size, + format_size_units_e unit, uint16_t flags) { wmem_strbuf_t *human_str = wmem_strbuf_new(allocator, NULL); int power = 1000; @@ -210,7 +211,7 @@ format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e f if (thousands_grouping_fmt == NULL) test_printf_thousands_grouping(); - if ((flags & FORMAT_SIZE_PFX_MASK) == format_size_prefix_iec) { + if (flags & FORMAT_SIZE_PREFIX_IEC) { pfx_off = 4; power = 1024; } @@ -232,25 +233,25 @@ format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e f is_small = TRUE; } - switch (flags & FORMAT_SIZE_UNIT_MASK) { - case format_size_unit_none: + switch (unit) { + case FORMAT_SIZE_UNIT_NONE: break; - case format_size_unit_bytes: + case FORMAT_SIZE_UNIT_BYTES: wmem_strbuf_append(human_str, is_small ? " bytes" : "B"); break; - case format_size_unit_bits: + case FORMAT_SIZE_UNIT_BITS: wmem_strbuf_append(human_str, is_small ? " bits" : "b"); break; - case format_size_unit_bits_s: + case FORMAT_SIZE_UNIT_BITS_S: wmem_strbuf_append(human_str, is_small ? " bits/s" : "bps"); break; - case format_size_unit_bytes_s: + case FORMAT_SIZE_UNIT_BYTES_S: wmem_strbuf_append(human_str, is_small ? " bytes/s" : "Bps"); break; - case format_size_unit_packets: + case FORMAT_SIZE_UNIT_PACKETS: wmem_strbuf_append(human_str, is_small ? " packets" : "packets"); break; - case format_size_unit_packets_s: + case FORMAT_SIZE_UNIT_PACKETS_S: wmem_strbuf_append(human_str, is_small ? " packets/s" : "packets/s"); break; default: diff --git a/wsutil/str_util.h b/wsutil/str_util.h index 8a2eaabce8..5e844acb95 100644 --- a/wsutil/str_util.h +++ b/wsutil/str_util.h @@ -112,17 +112,17 @@ WS_DLL_PUBLIC int ws_xton(char ch); typedef enum { - format_size_unit_none = 0, /**< No unit will be appended. You must supply your own. */ - format_size_unit_bytes = 1, /**< "bytes" for un-prefixed sizes, "B" otherwise. */ - format_size_unit_bits = 2, /**< "bits" for un-prefixed sizes, "b" otherwise. */ - format_size_unit_bits_s = 3, /**< "bits/s" for un-prefixed sizes, "bps" otherwise. */ - format_size_unit_bytes_s = 4, /**< "bytes/s" for un-prefixed sizes, "Bps" otherwise. */ - format_size_unit_packets = 5, /**< "packets" */ - format_size_unit_packets_s = 6, /**< "packets/s" */ - format_size_prefix_si = 0 << 8, /**< SI (power of 1000) prefixes will be used. */ - format_size_prefix_iec = 1 << 8 /**< IEC (power of 1024) prefixes will be used. */ - /* XXX format_size_prefix_default_for_this_particular_os ? */ -} format_size_flags_e; + FORMAT_SIZE_UNIT_NONE, /**< No unit will be appended. You must supply your own. */ + FORMAT_SIZE_UNIT_BYTES, /**< "bytes" for un-prefixed sizes, "B" otherwise. */ + FORMAT_SIZE_UNIT_BITS, /**< "bits" for un-prefixed sizes, "b" otherwise. */ + FORMAT_SIZE_UNIT_BITS_S, /**< "bits/s" for un-prefixed sizes, "bps" otherwise. */ + FORMAT_SIZE_UNIT_BYTES_S, /**< "bytes/s" for un-prefixed sizes, "Bps" otherwise. */ + FORMAT_SIZE_UNIT_PACKETS, /**< "packets" */ + FORMAT_SIZE_UNIT_PACKETS_S, /**< "packets/s" */ +} format_size_units_e; + +#define FORMAT_SIZE_PREFIX_SI (1 << 0) /**< SI (power of 1000) prefixes will be used. */ +#define FORMAT_SIZE_PREFIX_IEC (1 << 1) /**< IEC (power of 1024) prefixes will be used. */ /** Given a size, return its value in a human-readable format * @@ -134,9 +134,11 @@ typedef enum { * @return A newly-allocated string representing the value. */ WS_DLL_PUBLIC -gchar *format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e flags); +char *format_size_wmem(wmem_allocator_t *allocator, int64_t size, + format_size_units_e unit, uint16_t flags); -#define format_size(size, flags) format_size_wmem(NULL, size, flags) +#define format_size(size, unit, flags) \ + format_size_wmem(NULL, size, unit, flags) WS_DLL_PUBLIC gchar printable_char_or_period(gchar c); @@ -150,9 +152,6 @@ gchar printable_char_or_period(gchar c); #ifdef __cplusplus } - -/* Should we just have separate unit and prefix enums instead? */ -extern format_size_flags_e operator|(format_size_flags_e lhs, format_size_flags_e rhs); #endif /* __cplusplus */ #endif /* __STR_UTIL_H__ */ diff --git a/wsutil/test_wsutil.c b/wsutil/test_wsutil.c index db386f8100..f85b986e2f 100644 --- a/wsutil/test_wsutil.c +++ b/wsutil/test_wsutil.c @@ -19,15 +19,15 @@ static void test_format_size(void) { char *str; - str = format_size(10000, format_size_unit_bytes); + str = format_size(10000, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI); g_assert_cmpstr(str, ==, "10 kB"); g_free(str); - str = format_size(100000, format_size_unit_bytes|format_size_prefix_iec); + str = format_size(100000, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_IEC); g_assert_cmpstr(str, ==, "97 KiB"); g_free(str); - str = format_size(20971520, format_size_unit_bits|format_size_prefix_iec); + str = format_size(20971520, FORMAT_SIZE_UNIT_BITS, FORMAT_SIZE_PREFIX_IEC); g_assert_cmpstr(str, ==, "20 Mib"); g_free(str); } |