From 092a3226bd3ebd3972a1f1d8b989f0abc88d205a Mon Sep 17 00:00:00 2001 From: kervala Date: Tue, 2 Feb 2016 12:37:06 +0100 Subject: [PATCH] Changed: New function bytesToHumanReadableUnits to use specific strings for units --HG-- branch : develop --- code/nel/include/nel/misc/common.h | 4 ++++ code/nel/src/misc/common.cpp | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/code/nel/include/nel/misc/common.h b/code/nel/include/nel/misc/common.h index 710a9e6df..3b5465eaa 100644 --- a/code/nel/include/nel/misc/common.h +++ b/code/nel/include/nel/misc/common.h @@ -336,6 +336,10 @@ void itoaInt64 (sint64 number, char *str, sint64 base = 10); std::string bytesToHumanReadable (const std::string &bytes); std::string bytesToHumanReadable (uint64 bytes); +/// Convert a number in bytes into a string that is easily readable by an human, for example 105123 -> "102kb" +/// Using units array as string: 0 => B, 1 => KiB, 2 => MiB, 3 => GiB, etc... +std::string bytesToHumanReadableUnits (uint64 bytes, const std::vector &units); + /// Convert a human readable into a bytes, for example "102kb" -> 105123 uint32 humanReadableToBytes (const std::string &str); diff --git a/code/nel/src/misc/common.cpp b/code/nel/src/misc/common.cpp index 5115547a5..119323331 100644 --- a/code/nel/src/misc/common.cpp +++ b/code/nel/src/misc/common.cpp @@ -380,7 +380,26 @@ string bytesToHumanReadable (uint64 bytes) div++; res = newres; } - return toString ("%" NL_I64 "u%s", res, divTable[div]); + return toString ("%" NL_I64 "u %s", res, divTable[div]); +} + +std::string bytesToHumanReadableUnits (uint64 bytes, const std::vector &units) +{ + if (units.empty()) return ""; + + uint div = 0; + uint last = units.size()-1; + uint64 res = bytes; + uint64 newres = res; + for(;;) + { + newres /= 1024; + if(newres < 8 || div > 3 || div == last) + break; + ++div; + res = newres; + } + return toString ("%" NL_I64 "u %s", res, units[div].c_str()); } uint32 humanReadableToBytes (const string &str)