diff options
author | Mark Wong <markwkm@gmail.com> | 2009-03-01 11:56:08 -0800 |
---|---|---|
committer | Mark Wong <markwkm@gmail.com> | 2009-03-01 11:56:08 -0800 |
commit | f4372fd2fd1b425253c635428803ae80b1a345af (patch) | |
tree | 7c0fcd62b950d920fe06634cd54518bcebba5327 | |
parent | 396d8086bf80bdf28c8d73e94f5294255d200c8d (diff) |
Added a format_b() function to format strings from numbers that in in
bytes.
-rw-r--r-- | utils.c | 46 | ||||
-rw-r--r-- | utils.h | 1 |
2 files changed, 45 insertions, 2 deletions
@@ -554,6 +554,50 @@ format_time(long seconds) return (result); } +#define NUM_STRINGS 8 + +/* + * format_b(amt) - format a byte memory value, returning a string + * suitable for display. Returns a pointer to a static + * area that changes each call. "amt" is converted to a + * string with a trailing "B". If "amt" is 10000 or greater, + * then it is formatted as megabytes (rounded) with a + * trailing "K". And so on... + */ + +char * +format_b(long long amt) + +{ + static char retarray[NUM_STRINGS][16]; + static int index = 0; + register char *ret; + register char tag = 'B'; + + ret = retarray[index]; + index = (index + 1) % NUM_STRINGS; + + if (amt >= 10000) + { + amt = (amt + 512) / 1024; + tag = 'K'; + if (amt >= 10000) + { + amt = (amt + 512) / 1024; + tag = 'B'; + if (amt >= 10000) + { + amt = (amt + 512) / 1024; + tag = 'G'; + } + } + } + + snprintf(ret, sizeof(retarray[index]) - 1, "%lld%c", amt, tag); + + return (ret); +} + /* * format_k(amt) - format a kilobyte memory value, returning a string * suitable for display. Returns a pointer to a static @@ -577,8 +621,6 @@ format_time(long seconds) * to convert the modulo operation into something quicker. What a hack! */ -#define NUM_STRINGS 8 - char * format_k(long amt) @@ -27,6 +27,7 @@ long percentages(int, int64_t *, int64_t *, int64_t *, int64_t *); char *errmsg(int); char *format_percent(double); char *format_time(long); +char *format_b(long long); char *format_k(long); char *string_list(char **); void debug_set(int); |