summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorAaron Marcher <me@drkhsh.at>2018-05-21 14:44:21 +0200
committerAaron Marcher <me@drkhsh.at>2018-05-21 14:44:21 +0200
commit10dbc9543edd5b5a7929dd8fa87ed33dd545375c (patch)
tree6f95a31f3a4c3d95b317a3280870dffb5ba5d4e1 /util.c
parentfc0dde5a60a066b8506834f508aebf0ea5055990 (diff)
Merge fmt_human_2 and fmt_human_10 to one function
Now only one function, fmt_human, takes an additional argument "base".
Diffstat (limited to 'util.c')
-rw-r--r--util.c32
1 files changed, 12 insertions, 20 deletions
diff --git a/util.c b/util.c
index 6dd08d7..fc8b72a 100644
--- a/util.c
+++ b/util.c
@@ -87,32 +87,24 @@ bprintf(const char *fmt, ...)
}
const char *
-fmt_human_2(size_t num)
+fmt_human(size_t num, int base)
{
size_t i;
double scaled;
- const char *prefix[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei",
- "Zi", "Yi" };
-
- scaled = num;
- for (i = 0; i < LEN(prefix) && scaled >= 1024; i++) {
- scaled /= 1024.0;
+ const char *siprefix[] = { "", "k", "M", "G", "T", "P", "E", "Z", "Y" };
+ const char *iecprefix[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei",
+ "Zi", "Yi" };
+ char *prefix[9];
+
+ if (base == 1000) {
+ memcpy(prefix, siprefix, sizeof(prefix));
+ } else if (base == 1024) {
+ memcpy(prefix, iecprefix, sizeof(prefix));
}
- return bprintf("%.1f%s", scaled, prefix[i]);
-}
-
-const char *
-fmt_human_10(size_t num)
-{
- size_t i;
- double scaled;
- const char *prefix[] = { "", "K", "M", "G", "T", "P", "E",
- "Z", "Y" };
-
scaled = num;
- for (i = 0; i < LEN(prefix) && scaled >= 1000; i++) {
- scaled /= 1000.0;
+ for (i = 0; i < LEN(prefix) && scaled >= 1024; i++) {
+ scaled /= base;
}
return bprintf("%.1f%s", scaled, prefix[i]);