1 #ifndef _MIMETIC_OS_DIRECTORY_H_
2 #define _MIMETIC_OS_DIRECTORY_H_
5 #include <mimetic/libconfig.h>
20 enum Type { Unknown, RegularFile, Directory, Link };
21 DirEntry(): type(Unknown) {}
25 friend class iterator;
26 struct iterator:
public std::iterator<std::forward_iterator_tag, DirEntry>
29 : m_dirp(0), m_dirh(0), m_eoi(true)
32 iterator(Directory* dirp)
33 : m_dirp(dirp), m_eoi(false)
35 m_dirh = opendir(m_dirp->m_path.c_str());
38 m_dirent = readdir(m_dirh);
52 const DirEntry& operator*()
const
56 const DirEntry* operator->()
const
60 iterator& operator++()
62 if((m_dirent = readdir(m_dirh)) == NULL)
70 iterator operator++(
int)
76 bool operator==(
const iterator& right)
78 if(m_eoi && right.m_eoi)
82 m_eoi == right.m_eoi &&
83 m_dirp->m_path == right.m_dirp->m_path &&
84 m_dirent && right.m_dirent &&
85 #ifdef _DIRENT_HAVE_D_TYPE
86 m_dirent->d_type == right.m_dirent->d_type &&
88 std::string(m_dirent->d_name) == right.m_dirent->d_name;
90 bool operator!=(
const iterator& right)
92 return !operator==(right);
95 void setDirent(
struct dirent* dent)
97 m_de.name = dent->d_name;
98 m_de.type = DirEntry::Unknown;
99 #ifdef _DIRENT_HAVE_D_TYPE
103 m_de.type = DirEntry::Directory;
106 m_de.type = DirEntry::RegularFile;
109 m_de.type = DirEntry::Link;
118 struct dirent* m_dirent;
121 Directory(
const std::string& dir)
129 {
return iterator(
this); }
131 {
return iterator(); };
135 return stat(m_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
137 static bool exists(
const std::string& dname)
140 return stat(dname.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
142 static bool create(
const std::string& dname)
145 return mkdir(dname.c_str(), 0755) == 0;
149 static bool remove(
const std::string& dname)
154 return rmdir(dname.c_str()) == 0;
156 const std::string& path()
const