Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=1aae7115fa09d8de3af9293e09a383c4453ee871

commit 1aae7115fa09d8de3af9293e09a383c4453ee871
Author: James Buren <[email protected]>
Date:   Sun Aug 19 00:15:25 2012 -0500

convert function for converting size to string to C++

diff --git a/Utility.cc b/Utility.cc
index e745af2..9db576c 100644
--- a/Utility.cc
+++ b/Utility.cc
@@ -3,10 +3,13 @@
#include <string.h>
#include <errno.h>
#include <stdlib.h>
+#include <sstream>
#include "Utility.hh"

#define LOGFILE "fwsetup.log"

+using std::stringstream;
+
pid_t execute(const string &cmd)
{
pid_t pid = -1;
@@ -74,6 +77,43 @@ unsigned long long string_to_size(const string &text)
return n;
}

+string size_to_string(unsigned long long n)
+{
+  unsigned long long divisor = 0;
+  const char *suffix = 0;
+  stringstream buf;
+
+  if(n >= TEBIBYTE)
+  {
+    divisor = TEBIBYTE;
+    suffix = "TiB";
+  }
+  else if(n >= GIBIBYTE)
+  {
+    divisor = GIBIBYTE;
+    suffix = "GiB";
+  }
+  else if(n >= MEBIBYTE)
+  {
+    divisor = MEBIBYTE;
+    suffix = "MiB";
+  }
+  else if(n >= KIBIBYTE)
+  {
+    divisor = KIBIBYTE;
+    suffix = "KiB";
+  }
+  else
+  {
+    divisor = 1;
+    suffix = "BiB";
+  }
+
+  buf << (long double) n / divisor << suffix;
+
+  return buf.str();
+}
+
#ifdef NEWT
bool get_text_size(const string &text,int &width,int &height)
{
diff --git a/Utility.hh b/Utility.hh
index ee6dca0..ec9b3a8 100644
--- a/Utility.hh
+++ b/Utility.hh
@@ -12,6 +12,7 @@ using std::string;

pid_t execute(const string &cmd);
unsigned long long string_to_size(const string &text);
+string size_to_string(unsigned long long n);

#ifdef NEWT
bool get_text_size(const string &text,int &width,int &height);
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to