summaryrefslogtreecommitdiff
path: root/src/utils/size_info.c
diff options
context:
space:
mode:
authorMole Shang <[email protected]>2023-08-08 12:16:59 +0800
committerMole Shang <[email protected]>2023-08-08 12:22:00 +0800
commitbbf7aed9ac484d7955b4d1888c9b2f93ffd44ba3 (patch)
tree944b132b070eaca3c5ef3a664d61b70d931a4271 /src/utils/size_info.c
parentf0121e1596dc5490b58b6dad1bfb8f3b83d19b94 (diff)
downloadhinata-bbf7aed9ac484d7955b4d1888c9b2f93ffd44ba3.tar.gz
hinata-bbf7aed9ac484d7955b4d1888c9b2f93ffd44ba3.tar.bz2
hinata-bbf7aed9ac484d7955b4d1888c9b2f93ffd44ba3.zip
ui: match size measures with corresponding units
Calculate the size in UI to align with suitable units.
Diffstat (limited to 'src/utils/size_info.c')
-rw-r--r--src/utils/size_info.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/utils/size_info.c b/src/utils/size_info.c
new file mode 100644
index 0000000..6b238bb
--- /dev/null
+++ b/src/utils/size_info.c
@@ -0,0 +1,27 @@
+#include "size_info.h"
+
+static const char *size_unit_map[] = {"B", "KB", "MB", "GB"};
+
+size_info_t num2sizeinfo(const unsigned long n) {
+ size_info_t size_info = {0};
+ long double tmp = (long double)n;
+ if ((unsigned long)tmp / 1024 == 0) {
+ size_info.unit = SIZE_B;
+ } else {
+ tmp /= 1024;
+ if ((unsigned long)tmp / 1024 == 0) {
+ size_info.unit = SIZE_KB;
+ } else {
+ tmp /= 1024;
+ if ((unsigned long)tmp / 1024 == 0) {
+ size_info.unit = SIZE_MB;
+ } else {
+ tmp /= 1024;
+ size_info.unit = SIZE_GB;
+ }
+ }
+ }
+ size_info.n = tmp;
+ size_info.unit_str = size_unit_map[size_info.unit];
+ return size_info;
+}