Under SVN controll by accident
[platform/upstream/libzypp.git] / zypp / @Review / FSize.h
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       FSize.h
14
15   Author:     Michael Andres <ma@suse.de>
16   Maintainer: Michael Andres <ma@suse.de>
17
18   Purpose: Store and operate on (file/package/partition) sizes (long long).
19
20 /-*/
21 #ifndef _FSize_h_
22 #define _FSize_h_
23
24 #include <iosfwd>
25 #include <string>
26
27 ///////////////////////////////////////////////////////////////////
28 //
29 //      CLASS NAME : FSize
30 //
31 /**
32  * @short Store and operate on (file/package/partition) sizes (long long).
33  **/
34 class FSize {
35
36   public:
37
38     /**
39      * The Units
40      **/
41     enum Unit { B = 0, K, M, G, T };
42
43   private:
44
45     /**
46      * The size in Byte
47      **/
48     long long _size;
49
50   public:
51
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;
56
57     /**
58      * Return ammount of Byte in Unit.
59      **/
60     static long long factor( const Unit unit_r ) {
61       switch ( unit_r ) {
62       case T: return TB;
63       case G: return GB;
64       case M: return MB;
65       case K: return KB;
66       case B: break;
67       }
68       return 1;
69     }
70
71     /**
72      * String representation of Unit.
73      **/
74     static const char * unit( const Unit unit_r ) {
75       switch ( unit_r ) {
76       case T: return "TB";
77       case G: return "GB";
78       case M: return "MB";
79       case K: return "kB";
80       case B: break;
81       }
82       return "B";
83     }
84
85   public:
86
87     /**
88      * Construct from size in Byte.
89      **/
90     FSize( const long long size_r = 0 )
91       : _size( size_r )
92     {}
93
94     /**
95      * Construct from size in certain unit.
96      * E.g. <code>FSize( 1, FSize::K )</code> makes 1024 Byte.
97      **/
98     FSize( const long long size_r, const Unit unit_r )
99       : _size( size_r * factor( unit_r ) )
100     {}
101
102     /**
103       Construct from string containing a number in given unit.
104     */
105     FSize( const std::string &sizeStr, const Unit unit_r = B );
106
107     /**
108      * Conversion to long long
109      **/
110     operator long long() const { return _size; }
111
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; }
116
117     FSize & operator++(/*prefix*/) { _size += 1; return *this; }
118     FSize & operator--(/*prefix*/) { _size -= 1; return *this; }
119
120     FSize operator++(int/*postfix*/) { return _size++; }
121     FSize operator--(int/*postfix*/) { return _size--; }
122
123     /**
124      * Adjust size to multiple of <code>blocksize_r</code>
125      **/
126     FSize & fillBlock( FSize blocksize_r = KB );
127
128     /**
129      * Return size adjusted to multiple of <code>blocksize_r</code>
130      **/
131     FSize fullBlock( FSize blocksize_r = KB ) const { FSize ret( _size ); return ret.fillBlock(  blocksize_r ); }
132
133     /**
134      * Return size in Unit ( not rounded )
135      **/
136     long long operator()( const Unit unit_r ) const { return _size / factor( unit_r ); }
137
138     /**
139      * Return the best unit for string representation.
140      **/
141     Unit bestUnit() const;
142
143     /**
144      * Used as precision argument to form(), the 'best' precision according to
145      * Unist is chosen.
146      **/
147     static const unsigned bestPrec = (unsigned)-1;
148
149     /**
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.
156      *
157      * If Unit is <b>B</b>yte, precision is set to zero.
158      **/
159     std::string form( const Unit unit_r, unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const;
160
161     /**
162      * Return string representation in bestUnit.
163      **/
164     std::string form( unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const {
165       return form( bestUnit(), fw, prec, showunit );
166     }
167
168     /**
169      * Default string representation (precision 1 and unit appended).
170      **/
171     std::string asString() const;
172
173     /**
174      * Write asString.
175      **/
176     friend std::ostream & operator<<( std::ostream & str, const FSize & obj );
177 };
178
179 ///////////////////////////////////////////////////////////////////
180
181 #endif // _FSize_h_