wibble
0.1.28
|
00001 #ifndef WIBBLE_SYS_DIRECTORY_H 00002 #define WIBBLE_SYS_DIRECTORY_H 00003 00004 #include <string> 00005 #include <dirent.h> // opendir, closedir 00006 #include <memory> // auto_ptr 00007 #include <sys/types.h> // mode_t 00008 #include <sys/stat.h> // struct stat 00009 00010 struct stat; 00011 00012 namespace wibble { 00013 namespace sys { 00014 namespace fs { 00015 00021 std::auto_ptr<struct stat> stat(const std::string& pathname); 00022 00024 bool access(const std::string& s, int m); 00025 00027 bool exists(const std::string& s); 00028 00032 std::string abspath(const std::string& pathname); 00033 00037 void mkdirIfMissing(const std::string& dir, mode_t mode); 00038 00041 void mkpath(const std::string& dir); 00042 00045 void mkFilePath(const std::string& file); 00046 00048 std::string readFile(const std::string &file); 00049 00051 void writeFile(const std::string &file, const std::string &data); 00052 00058 bool deleteIfExists(const std::string& file); 00059 00061 void renameIfExists(const std::string& src, const std::string& dst); 00062 00064 void unlink(const std::string& fname); 00065 00067 void rmdir(const std::string& dirname); 00068 00070 void rmtree(const std::string& dir); 00071 00077 bool isdir(const std::string& pathname); 00078 00080 bool isDirectory(const std::string& pathname) __attribute__ ((deprecated)); 00081 00083 class Directory 00084 { 00085 std::string m_path; 00086 00087 public: 00088 class const_iterator 00089 { 00090 DIR* dir; 00091 struct dirent* d; 00092 00093 public: 00094 // Create an end iterator 00095 const_iterator() : dir(0), d(0) {} 00096 // Create a begin iterator 00097 const_iterator(DIR* dir) : dir(dir), d(0) { ++(*this); } 00098 // Cleanup properly 00099 ~const_iterator() { if (dir) closedir(dir); } 00100 00101 // auto_ptr style copy semantics 00102 const_iterator(const const_iterator& i) 00103 { 00104 dir = i.dir; 00105 d = i.d; 00106 const_iterator* wi = const_cast<const_iterator*>(&i); 00107 wi->dir = 0; 00108 wi->d = 0; 00109 } 00110 const_iterator& operator=(const const_iterator& i) 00111 { 00112 // Catch a = a 00113 if (&i == this) return *this; 00114 if (dir) closedir(dir); 00115 dir = i.dir; 00116 d = i.d; 00117 const_iterator* wi = const_cast<const_iterator*>(&i); 00118 wi->dir = 0; 00119 wi->d = 0; 00120 return *this; 00121 } 00122 00123 const_iterator& operator++() 00124 { 00125 if ((d = readdir(dir)) == 0) 00126 { 00127 closedir(dir); 00128 dir = 0; 00129 } 00130 return *this; 00131 } 00132 00133 std::string operator*() const { return d->d_name; } 00134 struct dirent* operator->() { return d; } 00135 const struct dirent* operator->() const { return d; } 00136 00137 bool operator==(const const_iterator& iter) const 00138 { 00139 return dir == iter.dir && d == iter.d; 00140 } 00141 bool operator!=(const const_iterator& iter) const 00142 { 00143 return dir != iter.dir || d != iter.d; 00144 } 00145 }; 00146 00147 Directory(const std::string& path) : m_path(path) {} 00148 00150 const std::string& path() const { return m_path; } 00151 00153 bool valid(); 00154 00156 const_iterator begin(); 00157 00159 const_iterator end() const; 00160 00162 bool isdir(const const_iterator& i) const; 00163 }; 00164 00165 } 00166 } 00167 } 00168 00169 // vim:set ts=4 sw=4: 00170 #endif