sbuild-dirstream.h

Go to the documentation of this file.
00001 /* Copyright © 2003,2006-2007  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software: you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation, either version 3 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>.
00016  *
00017  *********************************************************************/
00018 
00019 #ifndef SBUILD_DIRSTREAM_H
00020 #define SBUILD_DIRSTREAM_H
00021 
00022 #include <sbuild/sbuild-custom-error.h>
00023 
00024 #include <iostream>
00025 #include <deque>
00026 #include <string>
00027 
00028 #include <sys/types.h>
00029 #include <dirent.h>
00030 
00031 namespace sbuild
00032 {
00033 
00040   class direntry
00041   {
00042   public:
00044     direntry()
00045     { std::memset(&this->data, 0, sizeof(struct dirent)); }
00046 
00052     direntry(const struct dirent *entry)
00053     { std::memcpy(&this->data, entry, sizeof(struct dirent)); }
00054 
00060     direntry(direntry const& orig)
00061     { memcpy(&this->data, &orig.data, sizeof(struct dirent)); }
00062 
00064     virtual ~direntry()
00065     {}
00066 
00072     long inode() const
00073     { return this->data.d_ino; }
00074 
00080     unsigned char type() const
00081     { return this->data.d_type; }
00082 
00088     std::string name() const
00089     { return this->data.d_name; }
00090 
00096     struct dirent const& dirent()
00097     { return this->data; }
00098 
00099   private:
00101     struct dirent data;
00102   }; // class direntry
00103 
00115   class dirstream
00116     {
00117     public:
00119       enum error_code
00120         {
00121           DIR_OPEN, 
00122           DIR_READ  
00123         };
00124 
00126     typedef custom_error<error_code> error;
00127 
00133       dirstream(std::string const& dir);
00134 
00136       virtual ~dirstream();
00137 
00147       void open(std::string const& dirname);
00148 
00156       void close();
00157 
00165       bool eof() const;
00166 
00174       bool bad() const;
00175 
00182       operator bool ();
00183 
00190       bool
00191       operator ! ();
00192 
00193       friend dirstream&
00194       operator >> (dirstream& stream,
00195                    direntry&  entry);
00196 
00197     private:
00205       void read (int quantity=1);
00206 
00208       std::string dirname;
00209 
00211       DIR *dir;
00212 
00217       std::deque<direntry> data;
00218 
00220       bool error_status;
00221 
00223       bool eof_status;
00224   };
00225 
00234   dirstream&
00235   operator >> (dirstream& stream,
00236                direntry&  entry);
00237 
00238 }
00239 
00240 #endif /* SBUILD_DIRSTREAM_H */
00241 
00242 /*
00243  * Local Variables:
00244  * mode:C++
00245  * End:
00246  */

Generated on Mon Jan 21 00:37:45 2008 for sbuild by  doxygen 1.5.4