Under SVN controll by accident
[platform/upstream/libzypp.git] / zypp / @Review / FSize.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       FSize.cc
14
15   Author:     Michael Andres <ma@suse.de>
16   Maintainer: Michael Andres <ma@suse.de>
17
18   Purpose:
19
20 /-*/
21
22 #include <stdlib.h>
23
24 #include <iostream>
25
26 #include <y2util/stringutil.h>
27 #include <y2util/FSize.h>
28
29 using namespace std;
30
31 FSize::FSize( const string &sizeStr, const Unit unit_r )
32   : _size( atoll( sizeStr.c_str() ) * factor( unit_r ) )
33 {
34 }
35
36 ///////////////////////////////////////////////////////////////////
37 //
38 //
39 //      METHOD NAME : FSize::fillBlock
40 //      METHOD TYPE : FSize &
41 //
42 //      DESCRIPTION :
43 //
44 FSize & FSize::fillBlock( FSize blocksize_r )
45 {
46   if ( _size && blocksize_r ) {
47     long long diff = _size % blocksize_r;
48     if ( diff ) {
49       if ( _size > 0 )
50         _size += blocksize_r;
51       _size -= diff;
52     }
53   }
54   return *this;
55 }
56
57 ///////////////////////////////////////////////////////////////////
58 //
59 //
60 //      METHOD NAME : FSize::bestUnit
61 //      METHOD TYPE : FSize::Unit
62 //
63 //      DESCRIPTION :
64 //
65 FSize::Unit FSize::bestUnit() const
66 {
67   long long usize( _size < 0 ? -_size : _size );
68   if ( usize < KB )
69     return B;
70   if ( usize < MB )
71     return K;
72   if ( usize < GB )
73     return M;
74   if ( usize < TB )
75     return G;
76   return T;
77 }
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   string ret = stringutil::form( "%*.*f", fw, prec, ( double( _size ) / factor( unit_r ) ) );
102   if ( showunit ) {
103     ret = stringutil::form( "%s %s", ret.c_str(), unit( unit_r ) );
104   }
105   return ret;
106 }
107
108
109 ///////////////////////////////////////////////////////////////////
110 //
111 //
112 //      METHOD NAME : FSize::asString
113 //      METHOD TYPE : std::string
114 //
115 //      DESCRIPTION :
116 //
117 std::string FSize::asString() const
118 {
119   return form();
120 }
121
122 /******************************************************************
123 **
124 **
125 **      FUNCTION NAME : operator<<
126 **      FUNCTION TYPE : std::ostream &
127 **
128 **      DESCRIPTION :
129 */
130 std::ostream & operator<<( std::ostream & str, const FSize & obj )
131 {
132   return str << obj.asString();
133 }
134