DiskUsage.h

Go to the documentation of this file.
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       const std::string path;
00035       mutable unsigned _size;
00036       mutable unsigned _files;
00037       friend std::ostream & operator<<( std::ostream & str, const Entry & obj );
00041       bool operator==( const Entry & rhs ) const {
00042         return  path == rhs.path;
00043       }
00047       bool operator<( const Entry & rhs ) const {
00048         return  path < rhs.path;
00049       }
00053       bool isBelow( const Entry & rhs ) const {
00054         // whether _dirname has prefix rhs._dirname
00055         return( path.compare( 0, rhs.path.size(), rhs.path ) == 0 );
00056       }
00060       bool isBelow( const std::string & dirname_r ) const {
00061         return  isBelow( Entry( dirname_r ) );
00062       }
00063 
00067       const Entry & operator=( const Entry & rhs ) const {
00068         return rhs;
00069       }
00073       const Entry & operator+=( const Entry & rhs ) const {
00074         _size  += rhs._size;
00075         _files += rhs._files;
00076         return *this;
00077       }
00081       const Entry & operator-=( const Entry & rhs ) const {
00082         _size  -= rhs._size;
00083         _files -= rhs._files;
00084         return *this;
00085       }
00086     };
00087   private:
00088     typedef std::set<Entry> EntrySet;
00089     EntrySet _dirs;
00090   public:
00091 
00092     DiskUsage() {};
00096     void add( const Entry & newent_r ) {
00097       std::pair<EntrySet::iterator,bool> res = _dirs.insert( newent_r );
00098       if ( !res.second ) {
00099         *res.first += newent_r;
00100       }
00101     }
00105     void add( const std::string & dirname_r, const unsigned & size_r = 0, const unsigned & files_r = 0 ) {
00106       add( Entry( dirname_r, size_r, files_r ) );
00107     }
00111     unsigned size() const { return _dirs.size(); }
00115     void clear() { _dirs.clear(); }
00120     Entry extract( const std::string & dirname_r );
00121 
00122   public:
00123 
00124     typedef EntrySet::iterator               iterator;
00125     typedef EntrySet::reverse_iterator       reverse_iterator;
00126  
00130     iterator begin() { return _dirs.begin(); }
00134     iterator end() { return _dirs.end(); }
00138     reverse_iterator rbegin() { return _dirs.rbegin(); }
00142     reverse_iterator rend() { return _dirs.rend(); }
00143  
00144     typedef EntrySet::const_iterator         const_iterator;
00145     typedef EntrySet::const_reverse_iterator const_reverse_iterator;
00146  
00150     const_iterator begin() const { return _dirs.begin(); }
00154     const_iterator end() const { return _dirs.end(); }
00158     const_reverse_iterator rbegin() const { return _dirs.rbegin(); }
00162     const_reverse_iterator rend()const { return _dirs.rend(); }
00163 
00164   public:
00165 
00166     friend std::ostream & operator<<( std::ostream & str, const DiskUsage & obj );
00167 
00168   };
00171 } // namespace zypp
00173 #endif // ZYPP_DISKUSAGE_H

Generated on Thu Jul 6 00:07:21 2006 for zypp by  doxygen 1.4.6