summaryrefslogtreecommitdiff
path: root/src/utils
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
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')
-rw-r--r--src/utils/size_info.c27
-rw-r--r--src/utils/size_info.h14
2 files changed, 41 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;
+}
diff --git a/src/utils/size_info.h b/src/utils/size_info.h
new file mode 100644
index 0000000..e0994c1
--- /dev/null
+++ b/src/utils/size_info.h
@@ -0,0 +1,14 @@
+#ifndef SIZE_INFO_H_
+#define SIZE_INFO_H_
+
+typedef enum size_unit { SIZE_B, SIZE_KB, SIZE_MB, SIZE_GB } size_unit_t;
+
+typedef struct size_info {
+ long double n;
+ size_unit_t unit;
+ const char *unit_str;
+} size_info_t;
+
+size_info_t num2sizeinfo(const unsigned long n);
+
+#endif