Moved format_kb from arnielizer to libi2ncommon
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 1 Feb 2011 08:59:24 +0000 (09:59 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 1 Feb 2011 08:59:24 +0000 (09:59 +0100)
- 1st step to merge nice_unit_format() and format_kb()

src/stringfunc.cpp
src/stringfunc.hxx

index f9c7d95..317a736 100644 (file)
@@ -966,6 +966,60 @@ string nice_unit_format(
 } // eo nice_unit_format(int input)
 
 
+string format_kb(long long bytes)
+{
+        int sizecount = 0;
+        long double calcTraffic = bytes;
+
+        /*
+        * Solange durch 1024 Teilen bis
+        * der naechste Teilschritt < 1 waere oder
+        * Abbruch bei Maximalumrechnung ->TB
+        */
+        while (calcTraffic > 1024 && sizecount<=6) {
+                calcTraffic = calcTraffic / 1024;
+                sizecount++;
+        }
+
+        long double tmp;                                                        // round
+        tmp = calcTraffic*10;
+        tmp += 0.5;
+        tmp = (long long) (tmp);
+        tmp = (long double)(tmp)/(long double)(10);
+        calcTraffic = tmp;
+
+        ostringstream out;
+
+        out.setf (ios::fixed);
+        out.precision(1);
+        switch (sizecount) {
+                case 1:
+                        out << calcTraffic << " KB";
+                        break;
+                case 2:
+                        out << calcTraffic << " MB";
+                        break;
+                case 3:
+                        out << calcTraffic << " GB";
+                        break;
+                case 4:
+                        out << calcTraffic << " TB";
+                        break;
+                case 5:
+                        out << calcTraffic << " PB";
+                        break;
+                case 6:
+                        out << calcTraffic << " EB";
+                        break;
+                default:
+                        out << calcTraffic << " B";
+                        break;
+        }
+
+        return out.str();
+}
+
+
 string escape(const string &s)
 {
    string out(s);
index 628106e..b7da87e 100644 (file)
@@ -201,6 +201,7 @@ std::string nice_unit_format(
         const int64_t input,
         const UnitSystem system = US_SI
 );
+std::string format_kb(long long kb);
 
 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
 bool replace_all(std::string &base, const char *ist, const char *soll);