00001 #ifndef WIBBLE_SYS_DIRECTORY_H
00002 #define WIBBLE_SYS_DIRECTORY_H
00003
00004 #include <string>
00005 #include <dirent.h>
00006 #include <memory>
00007 #include <sys/types.h>
00008
00009 struct stat;
00010
00011 namespace wibble {
00012 namespace sys {
00013 namespace fs {
00014
00020 std::auto_ptr<struct stat> stat(const std::string& pathname);
00021
00023 bool access(const std::string &s, int m);
00024
00028 std::string abspath(const std::string& pathname);
00029
00033 void mkdirIfMissing(const std::string& dir, mode_t mode);
00034
00037 void mkpath(const std::string& dir);
00038
00041 void mkFilePath(const std::string& file);
00042
00044 class Directory
00045 {
00046 std::string m_path;
00047
00048 public:
00049 class const_iterator
00050 {
00051 DIR* dir;
00052 struct dirent* d;
00053
00054 public:
00055
00056 const_iterator() : dir(0), d(0) {}
00057
00058 const_iterator(DIR* dir) : dir(dir), d(0) { ++(*this); }
00059
00060 ~const_iterator() { if (dir) closedir(dir); }
00061
00062
00063 const_iterator(const const_iterator& i)
00064 {
00065 dir = i.dir;
00066 d = i.d;
00067 const_iterator* wi = const_cast<const_iterator*>(&i);
00068 wi->dir = 0;
00069 wi->d = 0;
00070 }
00071 const_iterator& operator=(const const_iterator& i)
00072 {
00073
00074 if (&i == this) return *this;
00075 if (dir) closedir(dir);
00076 dir = i.dir;
00077 d = i.d;
00078 const_iterator* wi = const_cast<const_iterator*>(&i);
00079 wi->dir = 0;
00080 wi->d = 0;
00081 }
00082
00083 const_iterator& operator++()
00084 {
00085 if ((d = readdir(dir)) == 0)
00086 {
00087 closedir(dir);
00088 dir = 0;
00089 }
00090 return *this;
00091 }
00092
00093 std::string operator*() { return d->d_name; }
00094 struct dirent* operator->() { return d; }
00095
00096 bool operator==(const const_iterator& iter) const
00097 {
00098 return dir == iter.dir && d == iter.d;
00099 }
00100 bool operator!=(const const_iterator& iter) const
00101 {
00102 return dir != iter.dir || d != iter.d;
00103 }
00104 };
00105
00106 Directory(const std::string& path) : m_path(path) {}
00107
00109 const std::string& path() const { return m_path; }
00110
00112 bool valid();
00113
00115 const_iterator begin();
00116
00118 const_iterator end() const;
00119 };
00120
00121 }
00122 }
00123 }
00124
00125
00126 #endif