- Create the cache directly from the schema (installed) file.
[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   const ByteCount::Unit ByteCount::K( 1024LL, "K", 1 );
24   const ByteCount::Unit ByteCount::M( 1048576LL, "M", 1 );
25   const ByteCount::Unit ByteCount::G( 1073741824LL, "G", 2 );
26   const ByteCount::Unit ByteCount::T( 1099511627776LL, "T", 3 );
27
28   const ByteCount::Unit ByteCount::kB( 1000LL, "kB", 1 );
29   const ByteCount::Unit ByteCount::MB( 1000000LL, "MB", 1 );
30   const ByteCount::Unit ByteCount::GB( 1000000000LL, "GB", 2 );
31   const ByteCount::Unit ByteCount::TB( 1000000000000LL, "TB", 3 );
32
33   ///////////////////////////////////////////////////////////////////
34   //
35   //    METHOD NAME : ByteCount::fillBlock
36   //    METHOD TYPE : ByteCount &
37   //
38   ByteCount & ByteCount::fillBlock( ByteCount blocksize_r )
39   {
40     if ( _count && blocksize_r )
41       {
42         SizeType diff = _count % blocksize_r;
43         if ( diff )
44           {
45             if ( _count > 0 )
46               {
47                 _count += blocksize_r;
48                 _count -= diff;
49               }
50             else
51               {
52                 _count -= blocksize_r;
53                 _count += diff;
54               }
55           }
56       }
57     return *this;
58   }
59
60   ///////////////////////////////////////////////////////////////////
61   //
62   //    METHOD NAME : ByteCount::bestUnit
63   //    METHOD TYPE : ByteCount::Unit
64   //
65   const ByteCount::Unit & ByteCount::bestUnit() const
66   {
67     SizeType usize( _count < 0 ? -_count : _count );
68     if ( usize < K.factor() )
69       return B;
70     if ( usize < M.factor() )
71       return K;
72     if ( usize < G.factor() )
73       return M;
74     if ( usize < T.factor() )
75       return G;
76     return T;
77   }
78
79   ///////////////////////////////////////////////////////////////////
80   //
81   //    METHOD NAME : ByteCount::bestUnit1000
82   //    METHOD TYPE : ByteCount::Unit
83   //
84   const ByteCount::Unit & ByteCount::bestUnit1000() const
85   {
86     SizeType usize( _count < 0 ? -_count : _count );
87     if ( usize < kB.factor() )
88       return B;
89     if ( usize < MB.factor() )
90       return kB;
91     if ( usize < GB.factor() )
92       return MB;
93     if ( usize < TB.factor() )
94       return GB;
95     return TB;
96   }
97
98   /////////////////////////////////////////////////////////////////
99 } // namespace zypp
100 ///////////////////////////////////////////////////////////////////