Changed: New function bytesToHumanReadableUnits to use specific strings for units

--HG--
branch : develop
hg/feature/material-editor
kervala 9 years ago
parent b692b78718
commit 092a3226bd

@ -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);

@ -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)

Loading…
Cancel
Save