Split uc_human_bytesz in uc_human_bytesz_dec/uc_human_bytesz_bin, for
decimal and binary conversion (http://en.wikipedia.org/wiki/Megabyte)
This commit is contained in:
@@ -77,11 +77,15 @@ int
|
|||||||
getfields(char *str, char **args, int max, int mflag, const char *set);
|
getfields(char *str, char **args, int max, int mflag, const char *set);
|
||||||
|
|
||||||
//Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.)
|
//Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.)
|
||||||
//Conversion is done in decimal (base 10 ,1 kB == 1000 bytes)
|
|
||||||
///result should be at least of size 12,
|
///result should be at least of size 12,
|
||||||
///returns the result argument
|
///returns the result argument
|
||||||
char *
|
char *
|
||||||
uc_human_bytesz(uint64_t bytes, char *result, size_t result_len);
|
uc_human_bytesz_dec(uint64_t bytes, char *result, size_t result_len);
|
||||||
|
|
||||||
|
//same as uc_human_bytesz_bin, but Conversion is done in binary
|
||||||
|
//(base 2 ,1 KiB == 1024 bytes)
|
||||||
|
char *
|
||||||
|
uc_human_bytesz_bin(uint64_t bytes, char *result, size_t result_len);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+20
-1
@@ -3,7 +3,7 @@
|
|||||||
#include "ucore/string.h"
|
#include "ucore/string.h"
|
||||||
|
|
||||||
char *
|
char *
|
||||||
uc_human_bytesz(uint64_t bytes, char *result, size_t result_len)
|
uc_human_bytesz_dec(uint64_t bytes, char *result, size_t result_len)
|
||||||
{
|
{
|
||||||
if (bytes < 1000) {
|
if (bytes < 1000) {
|
||||||
snprintf(result, result_len, "%" PRIu64 " b", bytes);
|
snprintf(result, result_len, "%" PRIu64 " b", bytes);
|
||||||
@@ -22,3 +22,22 @@ uc_human_bytesz(uint64_t bytes, char *result, size_t result_len)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
uc_human_bytesz_bin(uint64_t bytes, char *result, size_t result_len)
|
||||||
|
{
|
||||||
|
if (bytes < 1024) {
|
||||||
|
snprintf(result, result_len, "%" PRIu64 " b", bytes);
|
||||||
|
} else if (bytes < 1048576) {
|
||||||
|
snprintf(result, result_len, "%.2f KiB", bytes/1024.0);
|
||||||
|
} else if (bytes < 1073741824ULL) {
|
||||||
|
snprintf(result, result_len, "%.2f MiB", bytes/1048576.0);
|
||||||
|
} else if (bytes < 1099511627776ULL) {
|
||||||
|
snprintf(result, result_len, "%.2f GiB", bytes/1073741824.0);
|
||||||
|
} else if (bytes < 1125899906842624ULL) {
|
||||||
|
snprintf(result, result_len, "%.2f TiB", bytes/1099511627776.0);
|
||||||
|
} else {
|
||||||
|
snprintf(result, result_len, "%.2f PiB", bytes/1125899906842624.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,11 @@ START_TEST (test_human_bytesz_bytes)
|
|||||||
char result[128];
|
char result[128];
|
||||||
char *rc;
|
char *rc;
|
||||||
|
|
||||||
rc = uc_human_bytesz(999, result, sizeof result);
|
rc = uc_human_bytesz_dec(999, result, sizeof result);
|
||||||
|
fail_if(rc == NULL);
|
||||||
|
ck_assert_str_eq(result, "999 b");
|
||||||
|
|
||||||
|
rc = uc_human_bytesz_bin(999, result, sizeof result);
|
||||||
fail_if(rc == NULL);
|
fail_if(rc == NULL);
|
||||||
ck_assert_str_eq(result, "999 b");
|
ck_assert_str_eq(result, "999 b");
|
||||||
END_TEST
|
END_TEST
|
||||||
@@ -15,7 +19,11 @@ START_TEST (test_human_bytesz_bytes_0)
|
|||||||
char result[128];
|
char result[128];
|
||||||
char *rc;
|
char *rc;
|
||||||
|
|
||||||
rc = uc_human_bytesz(0, result, sizeof result);
|
rc = uc_human_bytesz_dec(0, result, sizeof result);
|
||||||
|
fail_if(rc == NULL);
|
||||||
|
ck_assert_str_eq(result, "0 b");
|
||||||
|
|
||||||
|
rc = uc_human_bytesz_bin(0, result, sizeof result);
|
||||||
fail_if(rc == NULL);
|
fail_if(rc == NULL);
|
||||||
ck_assert_str_eq(result, "0 b");
|
ck_assert_str_eq(result, "0 b");
|
||||||
END_TEST
|
END_TEST
|
||||||
@@ -24,45 +32,65 @@ START_TEST (test_human_bytesz_kilobytes)
|
|||||||
char result[128];
|
char result[128];
|
||||||
char *rc;
|
char *rc;
|
||||||
|
|
||||||
rc = uc_human_bytesz(64000, result, sizeof result);
|
rc = uc_human_bytesz_dec(64000, result, sizeof result);
|
||||||
fail_if(rc == NULL);
|
fail_if(rc == NULL);
|
||||||
ck_assert_str_eq(result, "64.00 kB");
|
ck_assert_str_eq(result, "64.00 kB");
|
||||||
|
|
||||||
|
rc = uc_human_bytesz_bin(64000, result, sizeof result);
|
||||||
|
fail_if(rc == NULL);
|
||||||
|
ck_assert_str_eq(result, "62.50 KiB");
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_human_bytesz_megabytes)
|
START_TEST (test_human_bytesz_megabytes)
|
||||||
char result[128];
|
char result[128];
|
||||||
char *rc;
|
char *rc;
|
||||||
|
|
||||||
rc = uc_human_bytesz(6409000, result, sizeof result);
|
rc = uc_human_bytesz_dec(6409000, result, sizeof result);
|
||||||
fail_if(rc == NULL);
|
fail_if(rc == NULL);
|
||||||
ck_assert_str_eq(result, "6.41 MB");
|
ck_assert_str_eq(result, "6.41 MB");
|
||||||
|
|
||||||
|
rc = uc_human_bytesz_bin(6409000, result, sizeof result);
|
||||||
|
fail_if(rc == NULL);
|
||||||
|
ck_assert_str_eq(result, "6.11 MiB");
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_human_bytesz_gigabytes)
|
START_TEST (test_human_bytesz_gigabytes)
|
||||||
char result[128];
|
char result[128];
|
||||||
char *rc;
|
char *rc;
|
||||||
|
|
||||||
rc = uc_human_bytesz(126999400000, result, sizeof result);
|
rc = uc_human_bytesz_dec(126999400000, result, sizeof result);
|
||||||
fail_if(rc == NULL);
|
fail_if(rc == NULL);
|
||||||
ck_assert_str_eq(result, "127.00 GB");
|
ck_assert_str_eq(result, "127.00 GB");
|
||||||
|
|
||||||
|
rc = uc_human_bytesz_bin(126999400000, result, sizeof result);
|
||||||
|
fail_if(rc == NULL);
|
||||||
|
ck_assert_str_eq(result, "118.28 GiB");
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_human_bytesz_terrabytes)
|
START_TEST (test_human_bytesz_terrabytes)
|
||||||
char result[128];
|
char result[128];
|
||||||
char *rc;
|
char *rc;
|
||||||
|
|
||||||
rc = uc_human_bytesz(12453400030000, result, sizeof result);
|
rc = uc_human_bytesz_dec(12453400030000, result, sizeof result);
|
||||||
fail_if(rc == NULL);
|
fail_if(rc == NULL);
|
||||||
ck_assert_str_eq(result, "12.45 TB");
|
ck_assert_str_eq(result, "12.45 TB");
|
||||||
|
|
||||||
|
rc = uc_human_bytesz_bin(12453400030000, result, sizeof result);
|
||||||
|
fail_if(rc == NULL);
|
||||||
|
ck_assert_str_eq(result, "11.33 TiB");
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_human_bytesz_petabytes)
|
START_TEST (test_human_bytesz_petabytes)
|
||||||
char result[128];
|
char result[128];
|
||||||
char *rc;
|
char *rc;
|
||||||
|
|
||||||
rc = uc_human_bytesz(~0LL, result, sizeof result);
|
rc = uc_human_bytesz_dec(~0LL, result, sizeof result);
|
||||||
fail_if(rc == NULL);
|
fail_if(rc == NULL);
|
||||||
ck_assert_str_eq(result, "18446.74 PB");
|
ck_assert_str_eq(result, "18446.74 PB");
|
||||||
|
|
||||||
|
rc = uc_human_bytesz_bin(~0ULL, result, sizeof result);
|
||||||
|
fail_if(rc == NULL);
|
||||||
|
ck_assert_str_eq(result, "16384.00 PiB");
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user