/[debian]/mimetic/branches/upstream/current/mimetic/os/directory.h
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/os/directory.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations)
Sat Feb 25 16:49:20 2006 UTC (15 years, 5 months ago) by gregoa
File MIME type: text/plain
File size: 3863 byte(s)
[svn-inject] Installing original source of mimetic
1 #ifndef _MIMETIC_OS_DIRECTORY_H_
2 #define _MIMETIC_OS_DIRECTORY_H_
3 #include <string>
4 #include <iterator>
5 #include <dirent.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8
9 namespace mimetic
10 {
11
12 class Directory
13 {
14 public:
15 struct DirEntry
16 {
17 enum Type { Unknown, RegularFile, Directory, Link };
18 DirEntry(): type(Unknown) {}
19 std::string name;
20 Type type;
21 };
22 friend class iterator;
23 struct iterator: public std::iterator<std::forward_iterator_tag, DirEntry>
24 {
25 iterator() // end() it
26 : m_dirp(0), m_dirh(0), m_eoi(true)
27 {
28 }
29 iterator(Directory* dirp) // begin() it
30 : m_dirp(dirp), m_eoi(false)
31 {
32 m_dirh = opendir(m_dirp->m_path.c_str());
33 if(m_dirh)
34 {
35 m_dirent = readdir(m_dirh);
36 setDirent(m_dirent);
37 } else {
38 // opendir error, set equal to end()
39 m_dirp = 0;
40 m_dirh = 0;
41 m_eoi = true;
42 }
43 }
44 ~iterator()
45 {
46 if(m_dirh)
47 closedir(m_dirh);
48 }
49 const DirEntry& operator*() const
50 {
51 return m_de;
52 }
53 const DirEntry* operator->() const
54 {
55 return &m_de;
56 }
57 iterator& operator++()
58 {
59 if((m_dirent = readdir(m_dirh)) == NULL)
60 {
61 m_eoi = true;
62 return *this;
63 }
64 setDirent(m_dirent);
65 return *this;
66 }
67 iterator operator++(int) // postfix
68 {
69 iterator it = *this;
70 ++*this;
71 return it;
72 }
73 bool operator==(const iterator& right)
74 {
75 if(m_eoi && right.m_eoi)
76 return true;
77
78 return
79 m_eoi == right.m_eoi &&
80 m_dirp->m_path == right.m_dirp->m_path &&
81 m_dirent && right.m_dirent &&
82 m_dirent->d_type == right.m_dirent->d_type &&
83 std::string(m_dirent->d_name) == right.m_dirent->d_name;
84 }
85 bool operator!=(const iterator& right)
86 {
87 return !operator==(right);
88 }
89 private:
90 void setDirent(struct dirent* dent)
91 {
92 m_de.name = dent->d_name;
93 switch(dent->d_type)
94 {
95 case DT_DIR:
96 m_de.type = DirEntry::Directory;
97 break;
98 case DT_REG:
99 m_de.type = DirEntry::RegularFile;
100 break;
101 case DT_LNK:
102 m_de.type = DirEntry::Link;
103 break;
104 default:
105 m_de.type = DirEntry::Unknown;
106 }
107 }
108 Directory* m_dirp;
109 DIR* m_dirh;
110 bool m_eoi;
111 DirEntry m_de;
112 struct dirent* m_dirent;
113 };
114
115 Directory(const std::string& dir)
116 : m_path(dir)
117 {
118 }
119 ~Directory()
120 {
121 }
122 iterator begin()
123 { return iterator(this); }
124 iterator end()
125 { return iterator(); };
126 bool exists() const
127 {
128 struct stat st;
129 return stat(m_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
130 }
131 static bool exists(const std::string& dname)
132 {
133 struct stat st;
134 return stat(dname.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
135 }
136 static bool create(const std::string& dname)
137 {
138 if(!exists(dname))
139 return mkdir(dname.c_str(), 0755) == 0;
140 else
141 return 0;
142 }
143 static bool remove(const std::string& dname)
144 {
145 if(!exists(dname))
146 return 0;
147 else
148 return rmdir(dname.c_str()) == 0;
149 }
150 const std::string& path() const
151 {
152 return m_path;
153 }
154 private:
155 std::string m_path;
156 };
157
158 }
159
160 #endif
161

  ViewVC Help
Powered by ViewVC 1.1.26