libyui  3.3.2
FSize.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: FSize.h
20 
21  Author: Michael Andres <ma@suse.de>
22  Maintainer: Michael Andres <ma@suse.de>
23 
24  Purpose: Store and operate on (file/package/partition) sizes (long long).
25 
26 /-*/
27 #ifndef _FSize_h_
28 #define _FSize_h_
29 
30 #include <iosfwd>
31 #include <string>
32 
33 //
34 // CLASS NAME : FSize
35 //
36 /**
37  * Store and operate on (file/package/partition) sizes (long long).
38  **/
39 class FSize {
40 
41  public:
42 
43  /**
44  * The Units
45  **/
46  enum Unit { B = 0, K, M, G, T };
47 
48  private:
49 
50  /**
51  * The size in Byte
52  **/
53  long long _size;
54 
55  public:
56 
57  static const long long KB = 1024;
58  static const long long MB = 1024 * KB;
59  static const long long GB = 1024 * MB;
60  static const long long TB = 1024 * GB;
61 
62  /**
63  * Return ammount of Byte in Unit.
64  **/
65  static long long factor( const Unit unit_r ) {
66  switch ( unit_r ) {
67  case T: return TB;
68  case G: return GB;
69  case M: return MB;
70  case K: return KB;
71  case B: break;
72  }
73  return 1;
74  }
75 
76  /**
77  * String representation of Unit.
78  **/
79  static const char * unit( const Unit unit_r ) {
80  switch ( unit_r ) {
81  case T: return "TB";
82  case G: return "GB";
83  case M: return "MB";
84  case K: return "kB";
85  case B: break;
86  }
87  return "B";
88  }
89 
90  public:
91 
92  /**
93  * Construct from size in Byte.
94  **/
95  FSize( const long long size_r = 0 )
96  : _size( size_r )
97  {}
98 
99  /**
100  * Construct from size in certain unit.
101  * E.g. <code>FSize( 1, FSize::K )<code> makes 1024 Byte.
102  **/
103  FSize( const long long size_r, const Unit unit_r )
104  : _size( size_r * factor( unit_r ) )
105  {}
106 
107  /**
108  Construct from string containing a number in given unit.
109  */
110  FSize( const std::string &sizeStr, const Unit unit_r = B );
111 
112  /**
113  * Conversion to long long
114  **/
115  operator long long() const { return _size; }
116 
117  FSize & operator+=( const long long rhs ) { _size += rhs; return *this; }
118  FSize & operator-=( const long long rhs ) { _size -= rhs; return *this; }
119  FSize & operator*=( const long long rhs ) { _size *= rhs; return *this; }
120  FSize & operator/=( const long long rhs ) { _size /= rhs; return *this; }
121 
122  FSize & operator++(/*prefix*/) { _size += 1; return *this; }
123  FSize & operator--(/*prefix*/) { _size -= 1; return *this; }
124 
125  FSize operator++(int/*postfix*/) { return _size++; }
126  FSize operator--(int/*postfix*/) { return _size--; }
127 
128  /**
129  * Adjust size to multiple of <code>blocksize_r</code>
130  **/
131  FSize & fillBlock( FSize blocksize_r = KB );
132 
133  /**
134  * Return size adjusted to multiple of <code>blocksize_r</code>
135  **/
136  FSize fullBlock( FSize blocksize_r = KB ) const { FSize ret( _size ); return ret.fillBlock( blocksize_r ); }
137 
138  /**
139  * Return size in Unit ( not rounded )
140  **/
141  long long operator()( const Unit unit_r ) const { return _size / factor( unit_r ); }
142 
143  /**
144  * Return the best unit for string representation.
145  **/
146  Unit bestUnit() const;
147 
148  /**
149  * Used as precision argument to form(), the 'best' precision according to
150  * Unist is chosen.
151  **/
152  static const unsigned bestPrec = (unsigned)-1;
153 
154  /**
155  * Return string representation in given Unit. Parameter <code>fw</code> and
156  * <code>prec</code> denote field width and precision as in a "%*.*f" printf
157  * format string. Avalue of <code>bestPrec</code> automatically picks an
158  * appropriate precision depending on the unit.
159  * If <code>showunit</code> ist true, the string representaion
160  * of Unit is <em>appended<em> separated by a single blank.
161  *
162  * If Unit is <b>B</b>yte, precision is set to zero.
163  **/
164  std::string form( const Unit unit_r, unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const;
165 
166  /**
167  * Return string representation in bestUnit.
168  **/
169  std::string form( unsigned fw = 0, unsigned prec = bestPrec, const bool showunit = true ) const {
170  return form( bestUnit(), fw, prec, showunit );
171  }
172 
173  /**
174  * Default string representation (precision 1 and unit appended).
175  **/
176  std::string asString() const;
177 };
178 
179 
180 #endif // _FSize_h_
static long long factor(const Unit unit_r)
Return ammount of Byte in Unit.
Definition: FSize.h:65
static const char * unit(const Unit unit_r)
String representation of Unit.
Definition: FSize.h:79
std::string asString() const
Default string representation (precision 1 and unit appended).
Definition: FSize.cc:122
FSize fullBlock(FSize blocksize_r=KB) const
Return size adjusted to multiple of blocksize_r
Definition: FSize.h:136
std::string form(unsigned fw=0, unsigned prec=bestPrec, const bool showunit=true) const
Return string representation in bestUnit.
Definition: FSize.h:169
FSize(const long long size_r, const Unit unit_r)
Construct from size in certain unit.
Definition: FSize.h:103
Unit
The Units.
Definition: FSize.h:46
Store and operate on (file/package/partition) sizes (long long).
Definition: FSize.h:39
FSize(const long long size_r=0)
Construct from size in Byte.
Definition: FSize.h:95
FSize & fillBlock(FSize blocksize_r=KB)
Adjust size to multiple of blocksize_r
Definition: FSize.cc:46
static const unsigned bestPrec
Used as precision argument to form(), the &#39;best&#39; precision according to Unist is chosen.
Definition: FSize.h:152
std::string form(const Unit unit_r, unsigned fw=0, unsigned prec=bestPrec, const bool showunit=true) const
Return string representation in given Unit.
Definition: FSize.cc:87
Unit bestUnit() const
Return the best unit for string representation.
Definition: FSize.cc:66
long long operator()(const Unit unit_r) const
Return size in Unit ( not rounded )
Definition: FSize.h:141