// @(#)root/memstat:$Id$
// Author: Anar Manafov (A.Manafov@gsi.de) 2008-03-02

/*************************************************************************
* Copyright (C) 1995-2010, 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();
}
 TMemStatHelpers.cxx:1
 TMemStatHelpers.cxx:2
 TMemStatHelpers.cxx:3
 TMemStatHelpers.cxx:4
 TMemStatHelpers.cxx:5
 TMemStatHelpers.cxx:6
 TMemStatHelpers.cxx:7
 TMemStatHelpers.cxx:8
 TMemStatHelpers.cxx:9
 TMemStatHelpers.cxx:10
 TMemStatHelpers.cxx:11
 TMemStatHelpers.cxx:12
 TMemStatHelpers.cxx:13
 TMemStatHelpers.cxx:14
 TMemStatHelpers.cxx:15
 TMemStatHelpers.cxx:16
 TMemStatHelpers.cxx:17
 TMemStatHelpers.cxx:18
 TMemStatHelpers.cxx:19
 TMemStatHelpers.cxx:20
 TMemStatHelpers.cxx:21
 TMemStatHelpers.cxx:22
 TMemStatHelpers.cxx:23
 TMemStatHelpers.cxx:24
 TMemStatHelpers.cxx:25
 TMemStatHelpers.cxx:26
 TMemStatHelpers.cxx:27
 TMemStatHelpers.cxx:28
 TMemStatHelpers.cxx:29
 TMemStatHelpers.cxx:30
 TMemStatHelpers.cxx:31
 TMemStatHelpers.cxx:32
 TMemStatHelpers.cxx:33
 TMemStatHelpers.cxx:34
 TMemStatHelpers.cxx:35
 TMemStatHelpers.cxx:36
 TMemStatHelpers.cxx:37
 TMemStatHelpers.cxx:38
 TMemStatHelpers.cxx:39
 TMemStatHelpers.cxx:40
 TMemStatHelpers.cxx:41
 TMemStatHelpers.cxx:42
 TMemStatHelpers.cxx:43
 TMemStatHelpers.cxx:44
 TMemStatHelpers.cxx:45
 TMemStatHelpers.cxx:46
 TMemStatHelpers.cxx:47
 TMemStatHelpers.cxx:48
 TMemStatHelpers.cxx:49
 TMemStatHelpers.cxx:50
 TMemStatHelpers.cxx:51
 TMemStatHelpers.cxx:52
 TMemStatHelpers.cxx:53
 TMemStatHelpers.cxx:54
 TMemStatHelpers.cxx:55
 TMemStatHelpers.cxx:56
 TMemStatHelpers.cxx:57
 TMemStatHelpers.cxx:58