libzypp
10.5.0
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00012 #ifndef ZYPP_DISKUSAGE_H 00013 #define ZYPP_DISKUSAGE_H 00014 00015 #include <set> 00016 #include <string> 00017 00019 namespace zypp 00020 { 00021 00022 class DiskUsage { 00023 public: 00027 struct Entry { 00028 Entry() : _size(0), _files(0) {}; 00029 Entry(const std::string& path_r, 00030 const unsigned size_r = 0, 00031 const unsigned files_r = 0) 00032 : path(path_r), _size(size_r), _files(files_r) 00033 { 00034 // assert leading and trailing '/' 00035 if ( ! path.empty() ) 00036 { 00037 if ( *path.begin() != '/' ) path.insert( path.begin(), '/' ); 00038 if ( *path.rbegin() != '/' ) path.insert( path.end(), '/' ); 00039 } 00040 } 00041 std::string path; 00042 mutable unsigned _size; 00043 mutable unsigned _files; 00044 friend std::ostream & operator<<( std::ostream & str, const Entry & obj ); 00048 bool operator==( const Entry & rhs ) const { 00049 return path == rhs.path; 00050 } 00054 bool operator<( const Entry & rhs ) const { 00055 return path < rhs.path; 00056 } 00060 bool isBelow( const Entry & rhs ) const { 00061 // whether _dirname has prefix rhs._dirname 00062 return( path.compare( 0, rhs.path.size(), rhs.path ) == 0 ); 00063 } 00067 bool isBelow( const std::string & dirname_r ) const { 00068 return isBelow( Entry( dirname_r ) ); 00069 } 00070 00074 const Entry & operator+=( const Entry & rhs ) const { 00075 _size += rhs._size; 00076 _files += rhs._files; 00077 return *this; 00078 } 00082 const Entry & operator-=( const Entry & rhs ) const { 00083 _size -= rhs._size; 00084 _files -= rhs._files; 00085 return *this; 00086 } 00087 }; 00088 private: 00089 typedef std::set<Entry> EntrySet; 00090 EntrySet _dirs; 00091 public: 00092 00093 DiskUsage() {}; 00097 void add( const Entry & newent_r ) { 00098 std::pair<EntrySet::iterator,bool> res = _dirs.insert( newent_r ); 00099 if ( !res.second ) { 00100 *res.first += newent_r; 00101 } 00102 } 00106 void add( const std::string & dirname_r, const unsigned & size_r = 0, const unsigned & files_r = 0 ) { 00107 add( Entry( dirname_r, size_r, files_r ) ); 00108 } 00112 bool empty() const { return _dirs.empty(); } 00116 unsigned size() const { return _dirs.size(); } 00120 void clear() { _dirs.clear(); } 00125 Entry extract( const std::string & dirname_r ); 00126 00127 public: 00128 00129 typedef EntrySet::iterator iterator; 00130 typedef EntrySet::reverse_iterator reverse_iterator; 00131 00135 iterator begin() { return _dirs.begin(); } 00139 iterator end() { return _dirs.end(); } 00143 reverse_iterator rbegin() { return _dirs.rbegin(); } 00147 reverse_iterator rend() { return _dirs.rend(); } 00148 00149 typedef EntrySet::const_iterator const_iterator; 00150 typedef EntrySet::const_reverse_iterator const_reverse_iterator; 00151 00155 const_iterator begin() const { return _dirs.begin(); } 00159 const_iterator end() const { return _dirs.end(); } 00163 const_reverse_iterator rbegin() const { return _dirs.rbegin(); } 00167 const_reverse_iterator rend()const { return _dirs.rend(); } 00168 00169 public: 00170 00171 friend std::ostream & operator<<( std::ostream & str, const DiskUsage & obj ); 00172 00173 }; 00176 } // namespace zypp 00178 #endif // ZYPP_DISKUSAGE_H