libzypp  13.10.6
DiskUsage.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_DISKUSAGE_H
13 #define ZYPP_DISKUSAGE_H
14 
15 #include <set>
16 #include <string>
17 
19 namespace zypp
20 {
21 
22  class DiskUsage {
23  public:
27  struct Entry {
28  Entry() : _size(0), _files(0) {};
29  Entry(const std::string& path_r,
30  const unsigned size_r = 0,
31  const unsigned files_r = 0)
32  : path(path_r), _size(size_r), _files(files_r)
33  {
34  // assert leading and trailing '/'
35  if ( ! path.empty() )
36  {
37  if ( *path.begin() != '/' ) path.insert( path.begin(), '/' );
38  if ( *path.rbegin() != '/' ) path.insert( path.end(), '/' );
39  }
40  }
41  std::string path;
42  mutable unsigned _size;
43  mutable unsigned _files;
44  friend std::ostream & operator<<( std::ostream & str, const Entry & obj );
48  bool operator==( const Entry & rhs ) const {
49  return path == rhs.path;
50  }
54  bool operator<( const Entry & rhs ) const {
55  return path < rhs.path;
56  }
60  bool isBelow( const Entry & rhs ) const {
61  // whether _dirname has prefix rhs._dirname
62  return( path.compare( 0, rhs.path.size(), rhs.path ) == 0 );
63  }
67  bool isBelow( const std::string & dirname_r ) const {
68  return isBelow( Entry( dirname_r ) );
69  }
70 
74  const Entry & operator+=( const Entry & rhs ) const {
75  _size += rhs._size;
76  _files += rhs._files;
77  return *this;
78  }
82  const Entry & operator-=( const Entry & rhs ) const {
83  _size -= rhs._size;
84  _files -= rhs._files;
85  return *this;
86  }
87  };
88  private:
89  typedef std::set<Entry> EntrySet;
91  public:
92 
93  DiskUsage() {};
97  void add( const Entry & newent_r ) {
98  std::pair<EntrySet::iterator,bool> res = _dirs.insert( newent_r );
99  if ( !res.second ) {
100  *res.first += newent_r;
101  }
102  }
106  void add( const std::string & dirname_r, const unsigned & size_r = 0, const unsigned & files_r = 0 ) {
107  add( Entry( dirname_r, size_r, files_r ) );
108  }
112  bool empty() const { return _dirs.empty(); }
116  unsigned size() const { return _dirs.size(); }
120  void clear() { _dirs.clear(); }
125  Entry extract( const std::string & dirname_r );
126 
127  public:
128 
129  typedef EntrySet::iterator iterator;
130  typedef EntrySet::reverse_iterator reverse_iterator;
131 
135  iterator begin() { return _dirs.begin(); }
139  iterator end() { return _dirs.end(); }
143  reverse_iterator rbegin() { return _dirs.rbegin(); }
147  reverse_iterator rend() { return _dirs.rend(); }
148 
149  typedef EntrySet::const_iterator const_iterator;
150  typedef EntrySet::const_reverse_iterator const_reverse_iterator;
151 
155  const_iterator begin() const { return _dirs.begin(); }
159  const_iterator end() const { return _dirs.end(); }
163  const_reverse_iterator rbegin() const { return _dirs.rbegin(); }
167  const_reverse_iterator rend()const { return _dirs.rend(); }
168 
169  public:
170 
171  friend std::ostream & operator<<( std::ostream & str, const DiskUsage & obj );
172 
173  };
176 } // namespace zypp
178 #endif // ZYPP_DISKUSAGE_H
EntrySet::iterator iterator
Definition: DiskUsage.h:129
const Entry & operator-=(const Entry &rhs) const
Numerical operation based on size and files values.
Definition: DiskUsage.h:82
std::set< Entry > EntrySet
Definition: DiskUsage.h:89
EntrySet::const_iterator const_iterator
Definition: DiskUsage.h:149
const_reverse_iterator rend() const
Reverse const iterator pointing before the first entry.
Definition: DiskUsage.h:167
bool isBelow(const Entry &rhs) const
Return true if this entry denotes a directory equal to or below rhs._dirname.
Definition: DiskUsage.h:60
void clear()
Clear EntrySet.
Definition: DiskUsage.h:120
void add(const Entry &newent_r)
Add an entry.
Definition: DiskUsage.h:97
friend std::ostream & operator<<(std::ostream &str, const DiskUsage &obj)
Definition: DiskUsage.cc:80
Holds data about how much space will be needed per directory.
Definition: DiskUsage.h:27
const_iterator end() const
Forward const iterator pointing behind the last entry.
Definition: DiskUsage.h:159
const_reverse_iterator rbegin() const
Reverse const iterator pointing to the last entry (if any)
Definition: DiskUsage.h:163
iterator begin()
Forward iterator pointing to the first entry (if any)
Definition: DiskUsage.h:135
reverse_iterator rend()
Reverse iterator pointing before the first entry.
Definition: DiskUsage.h:147
const_iterator begin() const
Forward const iterator pointing to the first entry (if any)
Definition: DiskUsage.h:155
const Entry & operator+=(const Entry &rhs) const
Numerical operation based on size and files values.
Definition: DiskUsage.h:74
bool operator<(const Entry &rhs) const
Order based on directory name.
Definition: DiskUsage.h:54
Entry extract(const std::string &dirname_r)
Sum up any entries for dirname_r and its descendants and remove them on the fly.
Definition: DiskUsage.cc:25
EntrySet::const_reverse_iterator const_reverse_iterator
Definition: DiskUsage.h:150
EntrySet::reverse_iterator reverse_iterator
Definition: DiskUsage.h:130
reverse_iterator rbegin()
Reverse iterator pointing to the last entry (if any)
Definition: DiskUsage.h:143
EntrySet _dirs
Definition: DiskUsage.h:90
iterator end()
Forward iterator pointing behind the last entry.
Definition: DiskUsage.h:139
Entry(const std::string &path_r, const unsigned size_r=0, const unsigned files_r=0)
Definition: DiskUsage.h:29
void add(const std::string &dirname_r, const unsigned &size_r=0, const unsigned &files_r=0)
Add an entry.
Definition: DiskUsage.h:106
bool operator==(const Entry &rhs) const
Test for equality based on directory name.
Definition: DiskUsage.h:48
bool empty() const
Whether there is no entry available.
Definition: DiskUsage.h:112
unsigned size() const
Number of entries.
Definition: DiskUsage.h:116
bool isBelow(const std::string &dirname_r) const
Return true if this entry denotes a directory equal to or below dirname_r.
Definition: DiskUsage.h:67
std::string path
Definition: DiskUsage.h:41
friend std::ostream & operator<<(std::ostream &str, const Entry &obj)
Definition: DiskUsage.cc:20