1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/DiskUsage.h
12 #ifndef ZYPP_DISKUSAGE_H
13 #define ZYPP_DISKUSAGE_H
18 ///////////////////////////////////////////////////////////////////
20 { /////////////////////////////////////////////////////////////////
25 * @short Holds data about how much space will be needed per directory.
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)
34 // assert leading and trailing '/'
37 if ( *path.begin() != '/' ) path.insert( path.begin(), '/' );
38 if ( *path.rbegin() != '/' ) path.insert( path.end(), '/' );
42 mutable unsigned _size;
43 mutable unsigned _files;
44 friend std::ostream & operator<<( std::ostream & str, const Entry & obj );
46 * Test for equality based on directory name.
48 bool operator==( const Entry & rhs ) const {
49 return path == rhs.path;
52 * Order based on directory name.
54 bool operator<( const Entry & rhs ) const {
55 return path < rhs.path;
58 * Return true if this entry denotes a directory equal to or below rhs._dirname.
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 );
65 * Return true if this entry denotes a directory equal to or below dirname_r.
67 bool isBelow( const std::string & dirname_r ) const {
68 return isBelow( Entry( dirname_r ) );
72 * Numerical operation based on size and files values.
74 const Entry & operator+=( const Entry & rhs ) const {
80 * Numerical operation based on size and files values.
82 const Entry & operator-=( const Entry & rhs ) const {
89 typedef std::set<Entry> EntrySet;
95 * Add an entry. If already present, sum up the new entries size and files value.
97 void add( const Entry & newent_r ) {
98 std::pair<EntrySet::iterator,bool> res = _dirs.insert( newent_r );
100 *res.first += newent_r;
104 * Add an entry. If already present, sum up the new entries size and files value.
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 ) );
110 * Whether there is no entry available.
112 bool empty() const { return _dirs.empty(); }
116 unsigned size() const { return _dirs.size(); }
120 void clear() { _dirs.clear(); }
122 * Sum up any entries for dirname_r and its descendants and remove them
123 * on the fly. Return the result.
125 Entry extract( const std::string & dirname_r );
129 typedef EntrySet::iterator iterator;
130 typedef EntrySet::reverse_iterator reverse_iterator;
133 * Forward iterator pointing to the first entry (if any)
135 iterator begin() { return _dirs.begin(); }
137 * Forward iterator pointing behind the last entry.
139 iterator end() { return _dirs.end(); }
141 * Reverse iterator pointing to the last entry (if any)
143 reverse_iterator rbegin() { return _dirs.rbegin(); }
145 * Reverse iterator pointing before the first entry.
147 reverse_iterator rend() { return _dirs.rend(); }
149 typedef EntrySet::const_iterator const_iterator;
150 typedef EntrySet::const_reverse_iterator const_reverse_iterator;
153 * Forward const iterator pointing to the first entry (if any)
155 const_iterator begin() const { return _dirs.begin(); }
157 * Forward const iterator pointing behind the last entry.
159 const_iterator end() const { return _dirs.end(); }
161 * Reverse const iterator pointing to the last entry (if any)
163 const_reverse_iterator rbegin() const { return _dirs.rbegin(); }
165 * Reverse const iterator pointing before the first entry.
167 const_reverse_iterator rend()const { return _dirs.rend(); }
171 friend std::ostream & operator<<( std::ostream & str, const DiskUsage & obj );
174 ///////////////////////////////////////////////////////////////////
175 /////////////////////////////////////////////////////////////////
177 ///////////////////////////////////////////////////////////////////
178 #endif // ZYPP_DISKUSAGE_H