// @(#)root/memstat:$Name$:$Id: TMemStatHelpers.cxx 24366 2008-06-19 09:50:29Z anar $
// Author: Anar Manafov (A.Manafov@gsi.de) 09/05/2008

/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

// STD
#include <iomanip>
#include <sstream>
// ROOT
#include "Rtypes.h"
// Memstat
#include "TMemStatHelpers.h"

using namespace std;

//______________________________________________________________________________
string Memstat::dig2bytes(Long64_t bytes)
{
   // This function creates a string representation of the number of bytes,
   // represented as a number in B, kB, MB or GB depending on the value.
   // The result is rounded to a sensible number of digits

   ostringstream ss;
   ss << fixed;

   if (bytes < 0) {
      ss << '-';
      bytes = -bytes;
   }

   static const long kB = 1024L;
   static const long lMB = kB * kB;
   static const long lGB = lMB * kB;

   if (bytes < kB)
      ss << bytes << " B";
   else if (bytes < (10L * kB))
      ss << setprecision(2) << ((double)bytes / (float)kB) << " kB";
   else if (bytes < (100L * kB))
      ss << setprecision(1) << ((double)bytes / (float)kB) << " kB";
   else if (bytes < lMB)
      ss << setprecision(0) << ((double)bytes / (float)kB) << " kB";
   else if (bytes < (10L * lMB))
      ss << setprecision(2) << ((double)bytes / (float)lMB) << " MB";
   else if (bytes < (100L * lMB))
      ss << setprecision(1) << ((double)bytes / (float)lMB) << " MB";
   else if (bytes < lGB)
      ss << setprecision(0) << ((double)bytes / (float)lMB) << " MB";
   else
      ss << setprecision(2) << ((double)bytes / (float)lGB) << " GB";

   return ss.str();
}

Last change: Wed Jun 25 08:29:11 2008
Last generated: 2008-06-25 08:29

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.