Sierra Toolkit Version of the Day
FormatMemorySize.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010, 2011 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #include <sstream>
00010 #include <iomanip>
00011 #include <cmath>
00012 
00013 #include <boost/lexical_cast.hpp>
00014 
00015 #include <stk_util/util/FormatMemorySize.hpp>
00016 
00017 namespace stk {
00018 
00019 std::string
00020 formatMemorySize(
00021   double                size)
00022 {
00023   std::string           result;
00024   
00025   static const double kb = 1024.0;
00026   // static const double mb = kb * kb;
00027   // static const double gb = kb * kb * kb;
00028 
00029   if (size < 0.0) {
00030     result = "-";
00031     size = -size;
00032   }
00033 
00034   // output size in kilo bytes
00035   result += boost::lexical_cast<std::string>(static_cast<unsigned long>(size / kb));
00036   result += " KB";
00037   // if (size < kb) {
00038   //   // output size in bytes
00039   //   result += boost::lexical_cast<std::string>(static_cast<unsigned long>(size));
00040   //   result += " B";
00041   // }
00042   // else if (size < mb) {
00043   //   // output size in kilo bytes
00044   //   result += boost::lexical_cast<std::string>(static_cast<unsigned long>(size / kb));
00045   //   result += " KB";
00046   // }
00047   // else if (size < gb) {
00048   //   // output size in mega bytes
00049   //   result += boost::lexical_cast<std::string>(static_cast<unsigned long>(size / mb));
00050   //   result += " MB";
00051   // }
00052   // else {
00053   //   // everything else output in giga bytes
00054   //   result += boost::lexical_cast<std::string>(static_cast<unsigned long>(size / gb));
00055   //   result += " GB";
00056   // }
00057   
00058   return result;
00059 }
00060 
00061 
00062 std::string
00063 formatMemorySize(
00064   MemorySize            size)
00065 {
00066   std::string           result;
00067   
00068   static const MemorySize kb = 1024;
00069   // static const MemorySize mb = kb * kb;
00070   // static const MemorySize gb = kb * kb * kb;
00071 
00072   // output size in kilo bytes
00073   result = boost::lexical_cast<std::string>(size / kb);
00074   result += " KB";
00075   
00076   // if (size < kb) {
00077   //   // output size in bytes
00078   //   result = boost::lexical_cast<std::string>(size);
00079   //   result += " B";
00080   // }
00081   // else if (size < mb) {
00082   //   // output size in kilo bytes
00083   //   result = boost::lexical_cast<std::string>(size / kb);
00084   //   result += " KB";
00085   // }
00086   // else if (size < gb) {
00087   //   // output size in mega bytes
00088   //   result = boost::lexical_cast<std::string>(size / mb);
00089   //   result += " MB";
00090   // }
00091   // else {
00092   //   // everything else output in giga bytes
00093   //   result = boost::lexical_cast<std::string>(size / gb);
00094   //   result += " GB";
00095   // }
00096   
00097   return result;
00098 }
00099 
00100 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends