libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/FSize.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:       FSize.h
00020 
00021   Author:     Michael Andres <ma@suse.de>
00022   Maintainer: Michael Andres <ma@suse.de>
00023 
00024   Purpose: Store and operate on (file/package/partition) sizes (long long).
00025 
00026 /-*/
00027 #ifndef _FSize_h_
00028 #define _FSize_h_
00029 
00030 #include <iosfwd>
00031 #include <string>
00032 
00033 //
00034 //      CLASS NAME : FSize
00035 //
00036 /**
00037  * Store and operate on (file/package/partition) sizes (long long).
00038  **/
00039 class FSize {
00040 
00041   public:
00042 
00043     /**
00044      * The Units
00045      **/
00046     enum Unit { B = 0, K, M, G, T };
00047 
00048   private:
00049 
00050     /**
00051      * The size in Byte
00052      **/
00053     long long _size;
00054 
00055   public:
00056 
00057     static const long long KB = 1024;
00058     static const long long MB = 1024 * KB;
00059     static const long long GB = 1024 * MB;
00060     static const long long TB = 1024 * GB;
00061 
00062     /**
00063      * Return ammount of Byte in Unit.
00064      **/
00065     static long long factor( const Unit unit_r ) {
00066       switch ( unit_r ) {
00067       case T: return TB;
00068       case G: return GB;
00069       case M: return MB;
00070       case K: return KB;
00071       case B: break;
00072       }
00073       return 1;
00074     }
00075 
00076     /**
00077      * String representation of Unit.
00078      **/
00079     static const char * unit( const Unit unit_r ) {
00080       switch ( unit_r ) {
00081       case T: return "TB";
00082       case G: return "GB";
00083       case M: return "MB";
00084       case K: return "kB";
00085       case B: break;
00086       }
00087       return "B";
00088     }
00089 
00090   public:
00091 
00092     /**
00093      * Construct from size in Byte.
00094      **/
00095     FSize( const long long size_r = 0 )
00096       : _size( size_r )
00097     {}
00098 
00099     /**
00100      * Construct from size in certain unit.
00101      * E.g. <code>FSize( 1, FSize::K )<code> makes 1024 Byte.
00102      **/
00103     FSize( const long long size_r, const Unit unit_r )
00104       : _size( size_r * factor( unit_r ) )
00105     {}
00106 
00107     /**
00108       Construct from string containing a number in given unit.
00109     */
00110     FSize( const std::string &sizeStr, const Unit unit_r = B );
00111 
00112     /**
00113      * Conversion to long long
00114      **/
00115     operator long long() const { return _size; }
00116 
00117     FSize & operator+=( const long long rhs ) { _size += rhs; return *this; }
00118     FSize & operator-=( const long long rhs ) { _size -= rhs; return *this; }
00119     FSize & operator*=( const long long rhs ) { _size *= rhs; return *this; }
00120     FSize & operator/=( const long long rhs ) { _size /= rhs; return *this; }
00121 
00122     FSize & operator++(/*prefix*/) { _size += 1; return *this; }
00123     FSize & operator--(/*prefix*/) { _size -= 1; return *this; }
00124 
00125     FSize operator++(int/*postfix*/) { return _size++; }
00126     FSize operator--(int/*postfix*/) { return _size--; }
00127 
00128     /**
00129      * Adjust size to multiple of <code>blocksize_r</code>
00130      **/
00131     FSize & fillBlock( FSize blocksize_r = KB );
00132 
00133     /**
00134      * Return size adjusted to multiple of <code>blocksize_r</code>
00135      **/
00136     FSize fullBlock( FSize blocksize_r = KB ) const { FSize ret( _size ); return ret.fillBlock(  blocksize_r ); }
00137 
00138     /**
00139      * Return size in Unit ( not rounded )
00140      **/
00141     long long operator()( const Unit unit_r ) const { return _size / factor( unit_r ); }
00142 
00143     /**
00144      * Return the best unit for string representation.
00145      **/
00146     Unit bestUnit() const;
00147 
00148     /**
00149      * Used as precision argument to form(), the 'best' precision according to
00150      * Unist is chosen.
00151      **/
00152     static const unsigned bestPrec = (unsigned)-1;
00153 
00154     /**
00155      * Return string representation in given Unit. Parameter <code>fw</code> and
00156      * <code>prec</code> denote field width and precision as in a "%*.*f" printf
00157      * format string. Avalue of <code>bestPrec</code> automatically picks an
00158      * appropriate precision depending on the unit.
00159      * If <code>showunit</code> ist true, the string representaion
00160      * of Unit is <em>appended<em> separated by a single blank.
00161      *
00162      * If Unit is <b>B</b>yte, precision is set to zero.
00163      **/
00164     std::string form( const Unit unit_r, unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const;
00165 
00166     /**
00167      * Return string representation in bestUnit.
00168      **/
00169     std::string form( unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const {
00170       return form( bestUnit(), fw, prec, showunit );
00171     }
00172 
00173     /**
00174      * Default string representation (precision 1 and unit appended).
00175      **/
00176     std::string asString() const;
00177 };
00178 
00179 
00180 #endif // _FSize_h_
 All Classes Functions Variables Enumerations Friends