• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

fs.h

Go to the documentation of this file.
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 
00029 std::string abspath(const std::string& pathname);
00030 
00034 void mkdirIfMissing(const std::string& dir, mode_t mode);
00035 
00038 void mkpath(const std::string& dir);
00039 
00042 void mkFilePath(const std::string& file);
00043 
00045 std::string readFile(const std::string &file);
00046 void writeFile(const std::string &file, const std::string &data);
00047 
00053 bool deleteIfExists(const std::string& file);
00054 
00060 bool isDirectory(const std::string& pathname);
00061 
00063 class Directory
00064 {
00065     std::string m_path;
00066 
00067 public:
00068     class const_iterator
00069     {
00070         DIR* dir;
00071         struct dirent* d;
00072 
00073     public:
00074         // Create an end iterator
00075         const_iterator() : dir(0), d(0) {}
00076         // Create a begin iterator
00077         const_iterator(DIR* dir) : dir(dir), d(0) { ++(*this); }
00078         // Cleanup properly
00079         ~const_iterator() { if (dir) closedir(dir); }
00080 
00081         // auto_ptr style copy semantics
00082         const_iterator(const const_iterator& i)
00083         {
00084             dir = i.dir;
00085             d = i.d;
00086             const_iterator* wi = const_cast<const_iterator*>(&i);
00087             wi->dir = 0;
00088             wi->d = 0;
00089         }
00090         const_iterator& operator=(const const_iterator& i)
00091         {
00092             // Catch a = a
00093             if (&i == this) return *this;
00094             if (dir) closedir(dir);
00095             dir = i.dir;
00096             d = i.d;
00097             const_iterator* wi = const_cast<const_iterator*>(&i);
00098             wi->dir = 0;
00099             wi->d = 0;
00100         }
00101 
00102         const_iterator& operator++()
00103         {
00104             if ((d = readdir(dir)) == 0)
00105             {
00106                 closedir(dir);
00107                 dir = 0;
00108             }
00109             return *this;
00110         }
00111 
00112         std::string operator*() const { return d->d_name; }
00113         struct dirent* operator->() { return d; }
00114         const struct dirent* operator->() const { return d; }
00115 
00116         bool operator==(const const_iterator& iter) const
00117         {
00118             return dir == iter.dir && d == iter.d;
00119         }
00120         bool operator!=(const const_iterator& iter) const
00121         {
00122             return dir != iter.dir || d != iter.d;
00123         }
00124     };
00125 
00126     Directory(const std::string& path) : m_path(path) {}
00127 
00129     const std::string& path() const { return m_path; }
00130 
00132     bool valid();
00133 
00135     const_iterator begin();
00136 
00138     const_iterator end() const;
00139 };
00140 
00141 }
00142 }
00143 }
00144 
00145 // vim:set ts=4 sw=4:
00146 #endif

Generated on Tue May 10 2011 16:51:50 for wibble by  doxygen 1.7.1