libzypp  17.16.0
ByteCount.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 
14 #include "zypp/ByteCount.h"
15 
16 using std::endl;
17 
19 namespace zypp
20 {
21 
22  const ByteCount::Unit ByteCount::B( 1LL, "B", 0 );
23 
24  const ByteCount::Unit ByteCount::K( 1024LL, "KiB", 1 );
26  const ByteCount::Unit ByteCount::M( 1048576LL, "MiB", 1 );
28  const ByteCount::Unit ByteCount::G( 1073741824LL, "GiB", 2 );
30  const ByteCount::Unit ByteCount::T( 1099511627776LL, "TiB", 3 );
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 
39  //
40  // METHOD NAME : ByteCount::fillBlock
41  // METHOD TYPE : ByteCount &
42  //
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 
66  //
67  // METHOD NAME : ByteCount::bestUnit
68  // METHOD TYPE : ByteCount::Unit
69  //
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 
85  //
86  // METHOD NAME : ByteCount::bestUnit1000
87  // METHOD TYPE : ByteCount::Unit
88  //
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 
104 } // namespace zypp
SizeType _count
Definition: ByteCount.h:155
Unit::ValueType SizeType
Definition: ByteCount.h:37
Store and operate with byte count.
Definition: ByteCount.h:30
static const Unit GB
1000^3 Byte
Definition: ByteCount.h:62
ValueType factor() const
Definition: Unit.h:60
static const Unit TiB
Definition: ByteCount.h:55
static const Unit MB
1000^2 Byte
Definition: ByteCount.h:60
base::Unit Unit
Definition: ByteCount.h:36
ByteCount & fillBlock(ByteCount blocksize_r=K)
Adjust count to multiple of blocksize_r (default 1K).
Definition: ByteCount.cc:43
static const Unit G
1024^3 Byte
Definition: ByteCount.h:51
const Unit & bestUnit() const
Return the best Unit (B,K,M,G,T) for count.
Definition: ByteCount.cc:70
static const Unit GiB
Definition: ByteCount.h:52
const Unit & bestUnit1000() const
Return the best Unit (B,kB,MB,GB,TB) for count.
Definition: ByteCount.cc:89
static const Unit B
1 Byte
Definition: ByteCount.h:42
Simple handling of Units.
Definition: Unit.h:42
static const Unit M
1024^2 Byte
Definition: ByteCount.h:48
static const Unit K
1024 Byte
Definition: ByteCount.h:45
static const Unit MiB
Definition: ByteCount.h:49
static const Unit T
1024^4 Byte
Definition: ByteCount.h:54
static const Unit TB
1000^4 Byte
Definition: ByteCount.h:64
static const Unit kB
1000 Byte
Definition: ByteCount.h:58
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
static const Unit KiB
Definition: ByteCount.h:46