Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

FSize.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013   File:       FSize.h
00014 
00015   Author:     Michael Andres <ma@suse.de>
00016   Maintainer: Michael Andres <ma@suse.de>
00017 
00018   Purpose: Store and operate on (file/package/partition) sizes (long long).
00019 
00020 /-*/
00021 #ifndef _FSize_h_
00022 #define _FSize_h_
00023 
00024 #include <iosfwd>
00025 #include <string>
00026 
00028 //
00029 //      CLASS NAME : FSize
00030 //
00034 class FSize {
00035 
00036   public:
00037 
00041     enum Unit { B = 0, K, M, G, T };
00042 
00043   private:
00044 
00048     long long _size;
00049 
00050   public:
00051 
00052     static const long long KB = 1024;
00053     static const long long MB = 1024 * KB;
00054     static const long long GB = 1024 * MB;
00055     static const long long TB = 1024 * GB;
00056 
00060     static long long factor( const Unit unit_r ) {
00061       switch ( unit_r ) {
00062       case T: return TB;
00063       case G: return GB;
00064       case M: return MB;
00065       case K: return KB;
00066       case B: break;
00067       }
00068       return 1;
00069     }
00070 
00074     static const char * unit( const Unit unit_r ) {
00075       switch ( unit_r ) {
00076       case T: return "TB";
00077       case G: return "GB";
00078       case M: return "MB";
00079       case K: return "kB";
00080       case B: break;
00081       }
00082       return "B";
00083     }
00084 
00085   public:
00086 
00090     FSize( const long long size_r = 0 )
00091       : _size( size_r )
00092     {}
00093 
00098     FSize( const long long size_r, const Unit unit_r )
00099       : _size( size_r * factor( unit_r ) )
00100     {}
00101 
00105     FSize( const std::string &sizeStr, const Unit unit_r = B );
00106 
00110     operator long long() const { return _size; }
00111 
00112     FSize & operator+=( const long long rhs ) { _size += rhs; return *this; }
00113     FSize & operator-=( const long long rhs ) { _size -= rhs; return *this; }
00114     FSize & operator*=( const long long rhs ) { _size *= rhs; return *this; }
00115     FSize & operator/=( const long long rhs ) { _size /= rhs; return *this; }
00116 
00117     FSize & operator++(/*prefix*/) { _size += 1; return *this; }
00118     FSize & operator--(/*prefix*/) { _size -= 1; return *this; }
00119 
00120     FSize operator++(int/*postfix*/) { return _size++; }
00121     FSize operator--(int/*postfix*/) { return _size--; }
00122 
00126     FSize & fillBlock( FSize blocksize_r = KB );
00127 
00131     FSize fullBlock( FSize blocksize_r = KB ) const { FSize ret( _size ); return ret.fillBlock(  blocksize_r ); }
00132 
00136     long long operator()( const Unit unit_r ) const { return _size / factor( unit_r ); }
00137 
00141     Unit bestUnit() const;
00142 
00147     static const unsigned bestPrec = (unsigned)-1;
00148 
00159     std::string form( const Unit unit_r, unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const;
00160 
00164     std::string form( unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const {
00165       return form( bestUnit(), fw, prec, showunit );
00166     }
00167 
00171     std::string asString() const;
00172 
00176     friend std::ostream & operator<<( std::ostream & str, const FSize & obj );
00177 };
00178 
00180 
00181 #endif // _FSize_h_

Generated on Thu Feb 23 23:56:10 2006 for liby2util by doxygen 1.3.6