diff --git a/include/ucore/string.h b/include/ucore/string.h index fa586bb..6865403 100644 --- a/include/ucore/string.h +++ b/include/ucore/string.h @@ -77,11 +77,15 @@ int 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.) -//Conversion is done in decimal (base 10 ,1 kB == 1000 bytes) ///result should be at least of size 12, ///returns the result argument 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 } #endif diff --git a/src/human_bytesz.c b/src/human_bytesz.c index 00edcb4..ab8f658 100644 --- a/src/human_bytesz.c +++ b/src/human_bytesz.c @@ -3,7 +3,7 @@ #include "ucore/string.h" 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) { 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; } +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; +} diff --git a/test/test_human_bytesz.c b/test/test_human_bytesz.c index c3819d9..9a1d760 100644 --- a/test/test_human_bytesz.c +++ b/test/test_human_bytesz.c @@ -6,7 +6,11 @@ START_TEST (test_human_bytesz_bytes) char result[128]; 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); ck_assert_str_eq(result, "999 b"); END_TEST @@ -15,7 +19,11 @@ START_TEST (test_human_bytesz_bytes_0) char result[128]; 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); ck_assert_str_eq(result, "0 b"); END_TEST @@ -24,45 +32,65 @@ START_TEST (test_human_bytesz_kilobytes) char result[128]; char *rc; - rc = uc_human_bytesz(64000, result, sizeof result); + rc = uc_human_bytesz_dec(64000, result, sizeof result); fail_if(rc == NULL); 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 START_TEST (test_human_bytesz_megabytes) char result[128]; char *rc; - rc = uc_human_bytesz(6409000, result, sizeof result); + rc = uc_human_bytesz_dec(6409000, result, sizeof result); fail_if(rc == NULL); 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 START_TEST (test_human_bytesz_gigabytes) char result[128]; char *rc; - rc = uc_human_bytesz(126999400000, result, sizeof result); + rc = uc_human_bytesz_dec(126999400000, result, sizeof result); fail_if(rc == NULL); 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 START_TEST (test_human_bytesz_terrabytes) char result[128]; char *rc; - rc = uc_human_bytesz(12453400030000, result, sizeof result); + rc = uc_human_bytesz_dec(12453400030000, result, sizeof result); fail_if(rc == NULL); 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 START_TEST (test_human_bytesz_petabytes) char result[128]; char *rc; - rc = uc_human_bytesz(~0LL, result, sizeof result); + rc = uc_human_bytesz_dec(~0LL, result, sizeof result); fail_if(rc == NULL); 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