ByteCount.C

Go to the documentation of this file.
00001 /*!@file Util/ByteCount.C */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/ByteCount.C $
00035 // $Id: ByteCount.C 6164 2006-01-27 22:23:29Z rjpeters $
00036 //
00037 
00038 #ifndef UTIL_BYTECOUNT_C_DEFINED
00039 #define UTIL_BYTECOUNT_C_DEFINED
00040 
00041 #include "Util/ByteCount.H"
00042 
00043 #include "Util/StringConversions.H"
00044 #include "Util/log.H"
00045 
00046 #include <sstream>
00047 
00048 ByteCount::ByteCount(const std::string& str)
00049 {
00050   std::istringstream iss(str);
00051 
00052   double nbytes = -1.0;
00053 
00054   iss >> nbytes;
00055 
00056   if (nbytes < 0.0)
00057     conversion_error::raise<ByteCount>
00058       (str, "byte count must be positive");
00059 
00060   std::string suffix;
00061 
00062   iss >> suffix;
00063 
00064   if (suffix.empty() || suffix == "B")
00065     {
00066       // nothing to do here
00067     }
00068   else if (suffix == "KiB") // kibibyte = 2^10B
00069     {
00070       nbytes *= 1024;
00071     }
00072   else if (suffix == "MiB") // mebibyte = 2^20B
00073     {
00074       nbytes *= 1024*1024;
00075     }
00076   else if (suffix == "GiB") // gibibyte = 2^30B
00077     {
00078       nbytes *= 1024*1024*1024;
00079     }
00080 #if 0
00081   // we could add these extra suffixes, but they'll just be bogus on
00082   // any machine where size_t is 32 bits -- need to wait til we all
00083   // have 64-bit machines... ;)
00084   else if (suffix == "TiB") // tebibyte = 2^40B
00085     {
00086       nbytes *= 1024*1024*1024*1024;
00087     }
00088   else if (suffix == "PiB") // pebibyte = 2^50B
00089     {
00090       nbytes *= 1024*1024*1024*1024*1024;
00091     }
00092   else if (suffix == "EiB") // exbibyte = 2^60B
00093     {
00094       nbytes *= 1024*1024*1024*1024*1024*1024;
00095     }
00096 #endif
00097   else
00098     {
00099       conversion_error::raise<ByteCount>
00100         (str, sformat("unknown ByteCount suffix '%s', expected "
00101                       "B, KiB, MiB, or GiB", suffix.c_str()));
00102     }
00103 
00104   itsCount = size_t(nbytes);
00105 
00106   LDEBUG("parsed ByteCount string '%s' as %"ZU" bytes (prettyPrint=%s)",
00107          str.c_str(), itsCount, this->prettyPrint().c_str());
00108 }
00109 
00110 std::string ByteCount::prettyPrint() const
00111 {
00112   if (itsCount < 1024)
00113     return sformat("%"ZU"B", itsCount);
00114   else if (itsCount < 1024*1024)
00115     return sformat("%.3fKiB", itsCount/1024.0);
00116   else if (itsCount < 1024*1024*1024)
00117     return sformat("%.3fMiB", itsCount/(1024.0*1024.0));
00118   else
00119     return sformat("%.3fGiB", itsCount/(1024.0*1024.0*1024.0));
00120 }
00121 
00122 // ######################################################################
00123 /* So things look consistent in everyone's emacs... */
00124 /* Local Variables: */
00125 /* indent-tabs-mode: nil */
00126 /* End: */
00127 
00128 #endif // UTIL_BYTECOUNT_C_DEFINED
Generated on Sun May 8 08:06:58 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3