libyui

/data/gitorious.org/libyui/libyui-master/src/FSize.h

00001 /**************************************************************************
00002 Copyright (C) 2000 - 2010 Novell, Inc.
00003 All Rights Reserved.
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License along
00016 with this program; if not, write to the Free Software Foundation, Inc.,
00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018 
00019 **************************************************************************/
00020 
00021 
00022 /*---------------------------------------------------------------------\
00023 |                                                                      |
00024 |                      __   __    ____ _____ ____                      |
00025 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00026 |                       \ V / _` \___ \ | |   __) |                    |
00027 |                        | | (_| |___) || |  / __/                     |
00028 |                        |_|\__,_|____/ |_| |_____|                    |
00029 |                                                                      |
00030 |                               core system                            |
00031 |                                                        (C) SuSE GmbH |
00032 \----------------------------------------------------------------------/
00033 
00034   File:       FSize.h
00035 
00036   Author:     Michael Andres <ma@suse.de>
00037   Maintainer: Michael Andres <ma@suse.de>
00038 
00039   Purpose: Store and operate on (file/package/partition) sizes (long long).
00040 
00041 /-*/
00042 #ifndef _FSize_h_
00043 #define _FSize_h_
00044 
00045 #include <iosfwd>
00046 #include <string>
00047 
00049 //
00050 //      CLASS NAME : FSize
00051 //
00055 class FSize {
00056 
00057   public:
00058 
00062     enum Unit { B = 0, K, M, G, T };
00063 
00064   private:
00065 
00069     long long _size;
00070 
00071   public:
00072 
00073     static const long long KB = 1024;
00074     static const long long MB = 1024 * KB;
00075     static const long long GB = 1024 * MB;
00076     static const long long TB = 1024 * GB;
00077 
00081     static long long factor( const Unit unit_r ) {
00082       switch ( unit_r ) {
00083       case T: return TB;
00084       case G: return GB;
00085       case M: return MB;
00086       case K: return KB;
00087       case B: break;
00088       }
00089       return 1;
00090     }
00091 
00095     static const char * unit( const Unit unit_r ) {
00096       switch ( unit_r ) {
00097       case T: return "TB";
00098       case G: return "GB";
00099       case M: return "MB";
00100       case K: return "kB";
00101       case B: break;
00102       }
00103       return "B";
00104     }
00105 
00106   public:
00107 
00111     FSize( const long long size_r = 0 )
00112       : _size( size_r )
00113     {}
00114 
00119     FSize( const long long size_r, const Unit unit_r )
00120       : _size( size_r * factor( unit_r ) )
00121     {}
00122 
00126     FSize( const std::string &sizeStr, const Unit unit_r = B );
00127 
00131     operator long long() const { return _size; }
00132 
00133     FSize & operator+=( const long long rhs ) { _size += rhs; return *this; }
00134     FSize & operator-=( const long long rhs ) { _size -= rhs; return *this; }
00135     FSize & operator*=( const long long rhs ) { _size *= rhs; return *this; }
00136     FSize & operator/=( const long long rhs ) { _size /= rhs; return *this; }
00137 
00138     FSize & operator++(/*prefix*/) { _size += 1; return *this; }
00139     FSize & operator--(/*prefix*/) { _size -= 1; return *this; }
00140 
00141     FSize operator++(int/*postfix*/) { return _size++; }
00142     FSize operator--(int/*postfix*/) { return _size--; }
00143 
00147     FSize & fillBlock( FSize blocksize_r = KB );
00148 
00152     FSize fullBlock( FSize blocksize_r = KB ) const { FSize ret( _size ); return ret.fillBlock(  blocksize_r ); }
00153 
00157     long long operator()( const Unit unit_r ) const { return _size / factor( unit_r ); }
00158 
00162     Unit bestUnit() const;
00163 
00168     static const unsigned bestPrec = (unsigned)-1;
00169 
00180     std::string form( const Unit unit_r, unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const;
00181 
00185     std::string form( unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const {
00186       return form( bestUnit(), fw, prec, showunit );
00187     }
00188 
00192     std::string asString() const;
00193 };
00194 
00196 
00197 #endif // _FSize_h_
 All Classes Functions Variables Enumerations Friends