From 266984871d3cd31fb1aed7ed6d334fdd496248aa Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 1 Feb 2011 09:59:24 +0100 Subject: [PATCH] Moved format_kb from arnielizer to libi2ncommon - 1st step to merge nice_unit_format() and format_kb() --- src/stringfunc.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/stringfunc.hxx | 1 + 2 files changed, 55 insertions(+), 0 deletions(-) diff --git a/src/stringfunc.cpp b/src/stringfunc.cpp index f9c7d95..317a736 100644 --- a/src/stringfunc.cpp +++ b/src/stringfunc.cpp @@ -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); diff --git a/src/stringfunc.hxx b/src/stringfunc.hxx index 628106e..b7da87e 100644 --- a/src/stringfunc.hxx +++ b/src/stringfunc.hxx @@ -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); -- 1.7.1