Changed: New function bytesToHumanReadableUnits to use specific strings for units

This commit is contained in:
kervala 2016-02-02 12:37:06 +01:00
parent e77a8b17b7
commit 6679aa7621
2 changed files with 24 additions and 1 deletions

View file

@ -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<std::string> &units);
/// Convert a human readable into a bytes, for example "102kb" -> 105123
uint32 humanReadableToBytes (const std::string &str);

View file

@ -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<std::string> &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)