From d1ea9075a01179298cdaed952d643cf59d648e96 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 1 Feb 2011 11:18:36 +0100 Subject: [PATCH] Allow to select the length of the symbol, short or long - short symbols are B, KB, MB, etc. - long symbols are Bytes, KBytes, MBytes, etc. --- src/stringfunc.cpp | 80 ++++++++++++++++++++++++++++------------------------ src/stringfunc.hxx | 8 ++++- 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/stringfunc.cpp b/src/stringfunc.cpp index 317a736..73de2cb 100644 --- a/src/stringfunc.cpp +++ b/src/stringfunc.cpp @@ -892,13 +892,41 @@ string to_upper(const string &src) } #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; @@ -909,57 +937,35 @@ string nice_unit_format( } 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(); diff --git a/src/stringfunc.hxx b/src/stringfunc.hxx index b7da87e..596f8c6 100644 --- a/src/stringfunc.hxx +++ b/src/stringfunc.hxx @@ -197,9 +197,15 @@ enum UnitSystem { 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); -- 1.7.1