summaryrefslogtreecommitdiff
path: root/lib/common/print_size.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-02-10 21:44:14 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-02-10 21:52:09 +0100
commit13a0c1b05a4b175f13decf3b15567dc71ea043ac (patch)
treee7d551fd43f69d828c58eb2b9e6d74254bafff10 /lib/common/print_size.c
parent060970feee2beea37e837e0a2d557e878e5f8290 (diff)
Cleanup statistics print out
- Give a rounded input/output byte count. - Seperate groups by new lines. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common/print_size.c')
-rw-r--r--lib/common/print_size.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/common/print_size.c b/lib/common/print_size.c
new file mode 100644
index 0000000..6e76805
--- /dev/null
+++ b/lib/common/print_size.c
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * print_size.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+#include "common.h"
+
+void print_size(sqfs_u64 size, char *buffer, bool round_to_int)
+{
+ static const char *fractions = "0112334456678899";
+ static const char *suffices = "kMGTPEZY";
+ unsigned int fraction;
+ int suffix = -1;
+
+ while (size > 1024) {
+ ++suffix;
+ fraction = size % 1024;
+ size /= 1024;
+ }
+
+ if (suffix >= 0) {
+ fraction /= 64;
+
+ if (round_to_int) {
+ size = fraction >= 8 ? (size + 1) : size;
+
+ sprintf(buffer, "%u%c", (unsigned int)size,
+ suffices[suffix]);
+ } else {
+ sprintf(buffer, "%u.%c%c", (unsigned int)size,
+ fractions[fraction], suffices[suffix]);
+ }
+ } else {
+ sprintf(buffer, "%u", (unsigned int)size);
+ }
+}