libyui  3.3.2
FSize.cc
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.cc
20 
21  Author: Michael Andres <ma@suse.de>
22  Maintainer: Michael Andres <ma@suse.de>
23 
24  Purpose:
25 
26 /-*/
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <iostream>
31 
32 #include "FSize.h"
33 
34 FSize::FSize( const std::string &sizeStr, const Unit unit_r )
35  : _size( atoll( sizeStr.c_str() ) * factor( unit_r ) )
36 {
37 }
38 
39 //
40 //
41 // METHOD NAME : FSize::fillBlock
42 // METHOD TYPE : FSize &
43 //
44 // DESCRIPTION :
45 //
46 FSize & FSize::fillBlock( FSize blocksize_r )
47 {
48  if ( _size && blocksize_r ) {
49  long long diff = _size % blocksize_r;
50  if ( diff ) {
51  if ( _size > 0 )
52  _size += blocksize_r;
53  _size -= diff;
54  }
55  }
56  return *this;
57 }
58 
59 //
60 //
61 // METHOD NAME : FSize::bestUnit
62 // METHOD TYPE : FSize::Unit
63 //
64 // DESCRIPTION :
65 //
67 {
68  long long usize( _size < 0 ? -_size : _size );
69  if ( usize < KB )
70  return B;
71  if ( usize < MB )
72  return K;
73  if ( usize < GB )
74  return M;
75  if ( usize < TB )
76  return G;
77  return T;
78 }
79 
80 //
81 //
82 // METHOD NAME : FSize::form
83 // METHOD TYPE : std::string
84 //
85 // DESCRIPTION :
86 //
87 std::string FSize::form( const Unit unit_r, unsigned fw, unsigned prec, const bool showunit ) const
88 {
89  if ( prec == bestPrec ) {
90  switch ( unit_r )
91  {
92  case T: prec = 3; break;
93  case G: prec = 2; break;
94  case M: prec = 1; break;
95  case K: prec = 1; break;
96  case B: prec = 0; break;
97  }
98  } else if ( unit_r == B )
99  prec = 0; // doesn't make sense for Byte
100 
101  char buffer[80]; // should be long enough for any numeric sprintf()
102  snprintf( buffer, sizeof( buffer ),
103  "%*.*f",
104  fw, prec, ( double( _size ) / factor( unit_r ) ) );
105 
106  std::string ret( buffer );
107 
108  if ( showunit )
109  ret += std::string(" ") + unit( unit_r );
110 
111  return ret;
112 }
113 
114 
115 //
116 //
117 // METHOD NAME : FSize::asString
118 // METHOD TYPE : std::string
119 //
120 // DESCRIPTION :
121 //
122 std::string FSize::asString() const
123 {
124  return form();
125 }
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
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