1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Michael Andres <ma@suse.de>
16 Maintainer: Michael Andres <ma@suse.de>
18 Purpose: Store and operate on (file/package/partition) sizes (long long).
27 ///////////////////////////////////////////////////////////////////
32 * @short Store and operate on (file/package/partition) sizes (long long).
41 enum Unit { B = 0, K, M, G, T };
52 static const long long KB = 1024;
53 static const long long MB = 1024 * KB;
54 static const long long GB = 1024 * MB;
55 static const long long TB = 1024 * GB;
58 * Return ammount of Byte in Unit.
60 static long long factor( const Unit unit_r ) {
72 * String representation of Unit.
74 static const char * unit( const Unit unit_r ) {
88 * Construct from size in Byte.
90 FSize( const long long size_r = 0 )
95 * Construct from size in certain unit.
96 * E.g. <code>FSize( 1, FSize::K )</code> makes 1024 Byte.
98 FSize( const long long size_r, const Unit unit_r )
99 : _size( size_r * factor( unit_r ) )
103 Construct from string containing a number in given unit.
105 FSize( const std::string &sizeStr, const Unit unit_r = B );
108 * Conversion to long long
110 operator long long() const { return _size; }
112 FSize & operator+=( const long long rhs ) { _size += rhs; return *this; }
113 FSize & operator-=( const long long rhs ) { _size -= rhs; return *this; }
114 FSize & operator*=( const long long rhs ) { _size *= rhs; return *this; }
115 FSize & operator/=( const long long rhs ) { _size /= rhs; return *this; }
117 FSize & operator++(/*prefix*/) { _size += 1; return *this; }
118 FSize & operator--(/*prefix*/) { _size -= 1; return *this; }
120 FSize operator++(int/*postfix*/) { return _size++; }
121 FSize operator--(int/*postfix*/) { return _size--; }
124 * Adjust size to multiple of <code>blocksize_r</code>
126 FSize & fillBlock( FSize blocksize_r = KB );
129 * Return size adjusted to multiple of <code>blocksize_r</code>
131 FSize fullBlock( FSize blocksize_r = KB ) const { FSize ret( _size ); return ret.fillBlock( blocksize_r ); }
134 * Return size in Unit ( not rounded )
136 long long operator()( const Unit unit_r ) const { return _size / factor( unit_r ); }
139 * Return the best unit for string representation.
141 Unit bestUnit() const;
144 * Used as precision argument to form(), the 'best' precision according to
147 static const unsigned bestPrec = (unsigned)-1;
150 * Return string representation in given Unit. Parameter <code>fw</code> and
151 * <code>prec</code> denote field width and precision as in a "%*.*f" printf
152 * format string. Avalue of <code>bestPrec</code> automatically picks an
153 * appropriate precision depending on the unit.
154 * If <code>showunit</code> ist true, the string representaion
155 * of Unit is <em>appended</em> separated by a single blank.
157 * If Unit is <b>B</b>yte, precision is set to zero.
159 std::string form( const Unit unit_r, unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const;
162 * Return string representation in bestUnit.
164 std::string form( unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const {
165 return form( bestUnit(), fw, prec, showunit );
169 * Default string representation (precision 1 and unit appended).
171 std::string asString() const;
176 friend std::ostream & operator<<( std::ostream & str, const FSize & obj );
179 ///////////////////////////////////////////////////////////////////