Imported Upstream version 15.21.0
[platform/upstream/libzypp.git] / zypp / ByteCount.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ByteCount.cc
10  *
11 */
12 #include <iostream>
13
14 #include "zypp/ByteCount.h"
15
16 using std::endl;
17
18 ///////////////////////////////////////////////////////////////////
19 namespace zypp
20 { /////////////////////////////////////////////////////////////////
21
22   const ByteCount::Unit ByteCount::B( 1LL, "B", 0 );
23
24   const ByteCount::Unit ByteCount::K( 1024LL, "KiB", 1 );
25   const ByteCount::Unit ByteCount::KiB( K );
26   const ByteCount::Unit ByteCount::M( 1048576LL, "MiB", 1 );
27   const ByteCount::Unit ByteCount::MiB( M );
28   const ByteCount::Unit ByteCount::G( 1073741824LL, "GiB", 2 );
29   const ByteCount::Unit ByteCount::GiB( G );
30   const ByteCount::Unit ByteCount::T( 1099511627776LL, "TiB", 3 );
31   const ByteCount::Unit ByteCount::TiB( T );
32
33   const ByteCount::Unit ByteCount::kB( 1000LL, "kB", 1 );
34   const ByteCount::Unit ByteCount::MB( 1000000LL, "MB", 1 );
35   const ByteCount::Unit ByteCount::GB( 1000000000LL, "GB", 2 );
36   const ByteCount::Unit ByteCount::TB( 1000000000000LL, "TB", 3 );
37
38   ///////////////////////////////////////////////////////////////////
39   //
40   //    METHOD NAME : ByteCount::fillBlock
41   //    METHOD TYPE : ByteCount &
42   //
43   ByteCount & ByteCount::fillBlock( ByteCount blocksize_r )
44   {
45     if ( _count && blocksize_r )
46       {
47         SizeType diff = _count % blocksize_r;
48         if ( diff )
49           {
50             if ( _count > 0 )
51               {
52                 _count += blocksize_r;
53                 _count -= diff;
54               }
55             else
56               {
57                 _count -= blocksize_r;
58                 _count += diff;
59               }
60           }
61       }
62     return *this;
63   }
64
65   ///////////////////////////////////////////////////////////////////
66   //
67   //    METHOD NAME : ByteCount::bestUnit
68   //    METHOD TYPE : ByteCount::Unit
69   //
70   const ByteCount::Unit & ByteCount::bestUnit() const
71   {
72     SizeType usize( _count < 0 ? -_count : _count );
73     if ( usize < K.factor() )
74       return B;
75     if ( usize < M.factor() )
76       return K;
77     if ( usize < G.factor() )
78       return M;
79     if ( usize < T.factor() )
80       return G;
81     return T;
82   }
83
84   ///////////////////////////////////////////////////////////////////
85   //
86   //    METHOD NAME : ByteCount::bestUnit1000
87   //    METHOD TYPE : ByteCount::Unit
88   //
89   const ByteCount::Unit & ByteCount::bestUnit1000() const
90   {
91     SizeType usize( _count < 0 ? -_count : _count );
92     if ( usize < kB.factor() )
93       return B;
94     if ( usize < MB.factor() )
95       return kB;
96     if ( usize < GB.factor() )
97       return MB;
98     if ( usize < TB.factor() )
99       return GB;
100     return TB;
101   }
102
103   /////////////////////////////////////////////////////////////////
104 } // namespace zypp
105 ///////////////////////////////////////////////////////////////////