Allow to select the length of the symbol, short or long
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 1 Feb 2011 10:18:36 +0000 (11:18 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 1 Feb 2011 10:18:36 +0000 (11:18 +0100)
- short symbols are B, KB, MB, etc.
- long symbols are Bytes, KBytes, MBytes, etc.

src/stringfunc.cpp
src/stringfunc.hxx

index 317a736..73de2cb 100644 (file)
@@ -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();
index b7da87e..596f8c6 100644 (file)
@@ -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);