}
#endif
+
+const int MAX_SYMBOL_FORMATS = 9;
+
+const string symbolFormatShort[MAX_SYMBOL_FORMATS] = {
+ " B",
+ " KB",
+ " MB",
+ " GB",
+ " TB",
+ " PB",
+ " EB",
+ " ZB",
+ " YB"
+};
+
+const string symbolFormatLong[MAX_SYMBOL_FORMATS] = {
+ " Bytes",
+ " KBytes",
+ " MBytes",
+ " GBytes",
+ " TBytes",
+ " PBytes",
+ " EBytes",
+ " ZBytes",
+ " YBytes"
+};
+
string nice_unit_format(
const int64_t input,
- const UnitSystem system
+ const UnitSystem system,
+ const UnitSymbolFormat symbolformat
)
{
+ // select the system of units (decimal or binary)
int multiple = 0;
-
if (system == US_SI)
{
multiple = 1000;
}
long double size = input;
- int sizecount = 0;
+ // check the size of the input number to fit in the appropriate symbol
+ int sizecount = 0;
while (size > multiple)
{
size = size / multiple;
sizecount++;
}
- long double tmp; // round
+ // round the input number
+ long double tmp;
tmp = size * 10;
tmp += 0.5;
tmp = (int64_t) (tmp);
tmp = (long double) (tmp) / (long double) (10);
size = tmp;
+ // format the input number, placing the appropriate symbol
ostringstream out;
-
out.setf (ios::fixed);
- out.precision (2);
- switch (sizecount)
+ if (symbolformat == USF_SHORT)
{
- case 0:
- out << size << i18n (" Bytes");
- break;
- case 1:
- out << size << i18n (" KBytes");
- break;
- case 2:
- out << size << i18n (" MBytes");
- break;
- case 3:
- out << size << i18n (" GBytes");
- break;
- case 4:
- out << size << i18n (" TBytes");
- break;
- case 5:
- out << size << i18n (" PBytes");
- break;
- case 6:
- out << size << i18n (" EBytes");
- break;
- case 7:
- out << size << i18n (" ZBytes");
- break;
- case 8:
- out << size << i18n (" YBytes");
- break;
- default:
- out << size << "*10^" << (sizecount*3)<< i18n (" Bytes");
- break;
+ out.precision(1);
+ out << size << i18n( symbolFormatShort[sizecount].c_str() );
+ }
+ else
+ {
+ out.precision (2);
+ out << size << i18n( symbolFormatLong[sizecount].c_str() );
}
return out.str();
US_IEC // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
};
+enum UnitSymbolFormat {
+ USF_SHORT, // B, KB, MB, ...
+ USF_LONG // Byte, KByte, MByte, ...
+};
+
std::string nice_unit_format(
const int64_t input,
- const UnitSystem system = US_SI
+ const UnitSystem system = US_SI,
+ const UnitSymbolFormat symbolformat = USF_LONG
);
std::string format_kb(long long kb);