myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2760] Code refactoring.


From: Giuseppe Scrivano
Subject: [myserver-commit] [2760] Code refactoring.
Date: Sun, 24 Aug 2008 20:58:01 +0000

Revision: 2760
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2760
Author:   gscrivano
Date:     2008-08-24 20:58:00 +0000 (Sun, 24 Aug 2008)

Log Message:
-----------
Code refactoring.  `HttpDir::getFormattedSize' was rewritten

Modified Paths:
--------------
    trunk/myserver/AUTHORS
    trunk/myserver/include/http_dir.h
    trunk/myserver/src/http_dir.cpp

Modified: trunk/myserver/AUTHORS
===================================================================
--- trunk/myserver/AUTHORS      2008-08-24 19:47:09 UTC (rev 2759)
+++ trunk/myserver/AUTHORS      2008-08-24 20:58:00 UTC (rev 2760)
@@ -2,10 +2,9 @@
 
 People who contributed with code or documentation:
 ==================================================
-Renato Nunes Bastos
-Georgy Berdyshev
-Russ Bohnhoff
-Alexandru Iancu
-Giuseppe Scrivano
-
-
+Renato Nunes Bastos <address@hidden>
+Debarshi Ray <address@hidden>
+Georgy Berdyshev <address@hidden>
+Russ Bohnhoff <address@hidden>
+Alexandru Iancu <address@hidden>
+Giuseppe Scrivano <address@hidden>

Modified: trunk/myserver/include/http_dir.h
===================================================================
--- trunk/myserver/include/http_dir.h   2008-08-24 19:47:09 UTC (rev 2759)
+++ trunk/myserver/include/http_dir.h   2008-08-24 20:58:00 UTC (rev 2760)
@@ -44,12 +44,13 @@
   HttpDir();
   virtual ~HttpDir();
 private:
+  double formatBytes(u_long bytes, u_int power);
   void formatHtml(string & in, string& out);
   static bool charIsLess(char i, char j);
   static bool compareFileStructByName (HttpDir::FileStruct i, 
HttpDir::FileStruct j);
   static bool compareFileStructByTime (HttpDir::FileStruct i, 
HttpDir::FileStruct j);
   static bool compareFileStructBySize (HttpDir::FileStruct i, 
HttpDir::FileStruct j);
-  void getFormattedSize(int bytes, string& out);
+  void getFormattedSize(u_long bytes, string& out);
 };
 
 

Modified: trunk/myserver/src/http_dir.cpp
===================================================================
--- trunk/myserver/src/http_dir.cpp     2008-08-24 19:47:09 UTC (rev 2759)
+++ trunk/myserver/src/http_dir.cpp     2008-08-24 20:58:00 UTC (rev 2760)
@@ -45,6 +45,7 @@
 #include <sstream>
 #include <vector>
 #include <algorithm>
+#include <iomanip>
 
 using namespace std;
 
@@ -105,118 +106,57 @@
   return 0;
 }
 
+/*!
+ *Get a formatted dobule representation of BYTES right shifted by POWER.
+ *\param bytes Number of bytes.
+ *\param power Power of 2.
+ *\return the double representation for BYTES>>POWER.
+ */
+double HttpDir::formatBytes(u_long bytes, u_int power)
+{
+  const u_long quotient = bytes >> power;
+  if (quotient == 0)
+    return -1;
 
+  const u_long unit = 1 << power;
+  const u_long remainder = bytes & (unit - 1);
+  const double result = quotient + static_cast<double>(remainder) / unit;
+  return result;
+}
+
+
 /*!
  *Fullfill the string out with a formatted representation for bytes.
  *\param bytes Size to format.
  *\param out Out string. 
  */
-void HttpDir::getFormattedSize(int bytes, string& out)
+void HttpDir::getFormattedSize(u_long bytes, string & out)
 {
-  ostringstream tmp;
+  const string symbols[] = {"TB", "GB", "MB", "KB", "bytes"};
+  const u_int powers[] = {40, 30, 20, 10, 0};
+  double result;
+  size_t i;
+  ostringstream osstr;
 
-  double leftover = 0.0;
-   
-  if(bytes < 1024)               //  Byte case
-    tmp << bytes << " bytes";
-  
-  else if ((bytes >= 1024) && (bytes < 1048576))  //  KB case
+  if (bytes == 0)
   {
-    u_long kb = static_cast<unsigned long int>(bytes / 1024);
-    leftover  = bytes % 1024;
-     
-    if(leftover < 50.0)
-      leftover = 0.0;
-    
-    // note: this case isn't handled through compiler casting
-    // using the output at the end!!!
-    // therefore it has to be here ...
-    else if(((static_cast<unsigned int>(leftover)) % 100) > 50)
-      leftover += 100.0;
-    
-    if(leftover >= 1000.0)
-    {
-      leftover = 0.0;
-      kb++;
-    }
-    else
-    {
-      while(leftover >= 10)
-      {  
-        if (leftover)
-          leftover /= 10;
-      }
-    }
-
-    // output ---> X.y KB
-    tmp << kb <<  "." << static_cast<unsigned int>(leftover) << " kb";
-    
+    out = "0";
+    return;
   }
-  else if ((bytes >= 1048576) && (bytes < 1073741824))  //  MB case
-  {
-    u_long mb = static_cast<unsigned long int>(bytes / 1048576);
-    leftover  = bytes % 1048576;
-    
-    if(leftover < 50.0)
-      leftover = 0.0;
-    
-    // note: this case isn't handled through compiler casting
-    // using the output at the end!!!
-    // therefore it has to be here ...
-    else if(((static_cast<unsigned int>(leftover)) % 100) > 50)
-      leftover += 100.0;
-    
-    if(leftover >= 1000.0)
-    {
-      leftover = 0.0;
-      mb++;
-    }
-    else
-    {
-      while(leftover >= 10)
-      {  
-        if (leftover)
-          leftover /= 10;
-      }
-    }
 
-    // output ---> X.y MB
-    tmp << mb <<  "." << static_cast<unsigned int>(leftover) << " MB";
-    
-  }
-  else //  GB case
+  for (i = 0; i < sizeof (powers); i++)
   {
-    u_long gb = static_cast<unsigned long int>(bytes / 1073741824);
-    leftover  = bytes % 1073741824;
-    
-    if(leftover < 50.0)
-      leftover = 0.0;
-    
-    // note: this case isn't handled through compiler casting
-    // using the output at the end!!!
-    // therefore it has to be here ...
-    else if(((static_cast<unsigned int>(leftover)) % 100) > 50)
-      leftover += 100.0;
-    
-    if(leftover >= 1000.0)
-    {
-      leftover = 0.0;
-      gb++;
-    }
-    else
-    {
-      while(leftover >= 10)
-      {  
-        if (leftover)
-          leftover /= 10;
-      }
-    }
-
-    // output ---> X.y GB
-    tmp << gb <<  "." << static_cast<unsigned int>(leftover) << " GB";
-    
+    result = formatBytes (bytes, powers[i]);
+    if (result != -1)
+      break;
   }
- out.assign(tmp.str());
+
+  if((result - floor(result)) < 0.01)
+    osstr << std::fixed << setprecision(0) << result << " " << symbols[i];
+  else
+    osstr << std::fixed << setprecision(2) << result << " " << symbols[i];
+  
+  out = osstr.str();
 }
 
 /*!






reply via email to

[Prev in Thread] Current Thread [Next in Thread]