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;
28 typedef std::forward_iterator_tag iterator_category;
29 typedef DirEntry value_type;
30 typedef std::ptrdiff_t difference_type;
31 typedef DirEntry* pointer;
32 typedef DirEntry& reference;
35 : m_dirp(0), m_dirh(0), m_eoi(true)
38 iterator(Directory* dirp)
39 : m_dirp(dirp), m_eoi(false)
41 m_dirh = opendir(m_dirp->m_path.c_str());
44 m_dirent = readdir(m_dirh);
58 const DirEntry& operator*()
const
62 const DirEntry* operator->()
const
66 iterator& operator++()
68 if((m_dirent = readdir(m_dirh)) == NULL)
76 iterator operator++(
int)
82 bool operator==(
const iterator& right)
84 if(m_eoi && right.m_eoi)
88 m_eoi == right.m_eoi &&
89 m_dirp->m_path == right.m_dirp->m_path &&
90 m_dirent && right.m_dirent &&
91 #ifdef _DIRENT_HAVE_D_TYPE
92 m_dirent->d_type == right.m_dirent->d_type &&
94 std::string(m_dirent->d_name) == right.m_dirent->d_name;
96 bool operator!=(
const iterator& right)
98 return !operator==(right);
101 void setDirent(
struct dirent* dent)
103 m_de.name = dent->d_name;
104 m_de.type = DirEntry::Unknown;
105 #ifdef _DIRENT_HAVE_D_TYPE
109 m_de.type = DirEntry::Directory;
112 m_de.type = DirEntry::RegularFile;
115 m_de.type = DirEntry::Link;
124 struct dirent* m_dirent;
127 Directory(
const std::string& dir)
135 {
return iterator(
this); }
137 {
return iterator(); };
141 return stat(m_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
143 static bool exists(
const std::string& dname)
146 return stat(dname.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
148 static bool create(
const std::string& dname)
151 return mkdir(dname.c_str(), 0755) == 0;
155 static bool remove(
const std::string& dname)
160 return rmdir(dname.c_str()) == 0;
162 const std::string& path()
const