sbuild-dirstream.h

Go to the documentation of this file.
00001 /* Copyright © 2003,2006  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 2 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, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
00017  *
00018  *********************************************************************/
00019 
00020 #ifndef SBUILD_DIRSTREAM_H
00021 #define SBUILD_DIRSTREAM_H
00022 
00023 #include <sbuild/sbuild-custom-error.h>
00024 
00025 #include <iostream>
00026 #include <deque>
00027 #include <string>
00028 
00029 #include <sys/types.h>
00030 #include <dirent.h>
00031 
00032 namespace sbuild
00033 {
00034 
00046   class direntry
00047   {
00048   public:
00050     direntry()
00051     { std::memset(&this->data, 0, sizeof(struct dirent)); }
00052 
00058     direntry(const struct dirent *entry)
00059     { std::memcpy(&this->data, entry, sizeof(struct dirent)); }
00060 
00066     direntry(direntry const& orig)
00067     { memcpy(&this->data, &orig.data, sizeof(struct dirent)); }
00068 
00070     virtual ~direntry()
00071     {}
00072 
00078     long inode() const
00079     { return this->data.d_ino; }
00080 
00086     unsigned char type() const
00087     { return this->data.d_type; }
00088 
00094     std::string name() const
00095     { return this->data.d_name; }
00096 
00102     struct dirent const& dirent()
00103     { return this->data; }
00104 
00105   private:
00107     struct dirent data;
00108   }; // class direntry
00109 
00121   class dirstream
00122     {
00123     public:
00125       enum error_code
00126         {
00127           DIR_OPEN, 
00128           DIR_READ  
00129         };
00130 
00132     typedef custom_error<error_code> error;
00133 
00139       dirstream(std::string const& dir);
00140 
00142       virtual ~dirstream();
00143 
00153       void open(std::string const& dirname);
00154 
00162       void close();
00163 
00171       bool eof() const;
00172 
00180       bool bad() const;
00181 
00188       operator bool ();
00189 
00196       bool
00197       operator ! ();
00198 
00199       friend dirstream&
00200       operator >> (dirstream& stream,
00201                    direntry&  entry);
00202 
00203     private:
00211       void read (int quantity=1);
00212 
00214       std::string dirname;
00215 
00217       DIR *dir;
00218 
00223       std::deque<direntry> data;
00224 
00226       bool error_status;
00227 
00229       bool eof_status;
00230   };
00231 
00236   dirstream&
00237   operator >> (dirstream& stream,
00238                direntry&  entry);
00239 
00240 }
00241 
00242 #endif /* SBUILD_DIRSTREAM_H */
00243 
00244 /*
00245  * Local Variables:
00246  * mode:C++
00247  * End:
00248  */

Generated on Fri Jul 14 19:52:17 2006 for schroot by  doxygen 1.4.7