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 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 00041 class direntry 00042 { 00043 public: 00045 direntry() 00046 { std::memset(&this->data, 0, sizeof(struct dirent)); } 00047 00053 direntry(const struct dirent *entry) 00054 { std::memcpy(&this->data, entry, sizeof(struct dirent)); } 00055 00061 direntry(direntry const& orig) 00062 { memcpy(&this->data, &orig.data, sizeof(struct dirent)); } 00063 00065 virtual ~direntry() 00066 {} 00067 00073 long inode() const 00074 { return this->data.d_ino; } 00075 00081 unsigned char type() const 00082 { return this->data.d_type; } 00083 00089 std::string name() const 00090 { return this->data.d_name; } 00091 00097 struct dirent const& dirent() 00098 { return this->data; } 00099 00100 private: 00102 struct dirent data; 00103 }; // class direntry 00104 00116 class dirstream 00117 { 00118 public: 00120 enum error_code 00121 { 00122 DIR_OPEN, 00123 DIR_READ 00124 }; 00125 00127 typedef custom_error<error_code> error; 00128 00134 dirstream(std::string const& dir); 00135 00137 virtual ~dirstream(); 00138 00148 void open(std::string const& dirname); 00149 00157 void close(); 00158 00166 bool eof() const; 00167 00175 bool bad() const; 00176 00183 operator bool (); 00184 00191 bool 00192 operator ! (); 00193 00194 friend dirstream& 00195 operator >> (dirstream& stream, 00196 direntry& entry); 00197 00198 private: 00206 void read (int quantity=1); 00207 00209 std::string dirname; 00210 00212 DIR *dir; 00213 00218 std::deque<direntry> data; 00219 00221 bool error_status; 00222 00224 bool eof_status; 00225 }; 00226 00235 dirstream& 00236 operator >> (dirstream& stream, 00237 direntry& entry); 00238 00239 } 00240 00241 #endif /* SBUILD_DIRSTREAM_H */ 00242 00243 /* 00244 * Local Variables: 00245 * mode:C++ 00246 * End: 00247 */