-
Notifications
You must be signed in to change notification settings - Fork 2
/
Directory.h
239 lines (175 loc) · 6.88 KB
/
Directory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifndef Directory_H_121223013
#define Directory_H_121223013
#include <iosfwd>
#include <iostream>
#include <list>
#include <string>
#include <stack>
#include "Node.h"
#include "Visitor.h"
class Directory : public Node {
std::list<Node *> nodeList;
std::string name;
std::string date_created;
friend class DirectoryIterator;
friend class ConstDirectoryIterator;
template<typename F> void DoRecursive(F& f, const Directory *pdir=nullptr, std::string path="");
public:
// nested stl-compliant forward iterator classes. They iterate the entire Directoy tree structure in order.
class DirectoryIterator : public std::iterator<std::forward_iterator_tag, Node *> {
friend class Directory;
typedef std::pair< std::list<Node *>::iterator, std::list<Node *>::iterator> iterators_pair;
std::stack< iterators_pair > iters_stack;
Node *pCurrentNode; // TODO: Change to iterators_pair& ?
Directory *pDirectory; // This is the top level directory which will be iterated.
public:
explicit DirectoryIterator(Directory & dir);
// Required forward iterator methods follow
DirectoryIterator();
DirectoryIterator(const DirectoryIterator&);
DirectoryIterator& operator=(const DirectoryIterator& other);
/*
Node *next();
bool hasNext();
*/
Node &operator*() const;
Node *operator->() const;
DirectoryIterator& operator++();
DirectoryIterator operator++(int);
bool operator==(const DirectoryIterator& x) const;
bool operator!=(const DirectoryIterator& x) const;
};
class ConstDirectoryIterator : public std::iterator<std::forward_iterator_tag, const Node *> {
friend class Directory;
DirectoryIterator inner_iter;
public:
explicit ConstDirectoryIterator(const Directory & dir);
// This ctor will function as a cast operator, to convert a DirectoryIterator into a ConstDirectoryIterator
ConstDirectoryIterator(const DirectoryIterator &iter);
// Required forward iterator methods follow
ConstDirectoryIterator();
ConstDirectoryIterator(const ConstDirectoryIterator&);
ConstDirectoryIterator& operator=(const ConstDirectoryIterator& other);
const Node &operator*() const;
const Node *operator->() const;
ConstDirectoryIterator& operator++();
ConstDirectoryIterator operator++(int);
bool operator==(const ConstDirectoryIterator& x) const;
bool operator!=(const ConstDirectoryIterator& x) const;
};
typedef DirectoryIterator iterator;
typedef ConstDirectoryIterator const_iterator;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
Directory(const std::string& dir_name, const std::string& created);
void print(std::ostream&, Directory *pdir=nullptr, std::string path= "") const;
template<typename F> void traverse(F& f);
/*
* All these methods are virtual in Node
*/
void adopt(Node *pnode) throw(node_logic_error);
Node *getChild(int i) throw(node_logic_error, std::out_of_range);
void orphan(Node *pnode) throw(node_logic_error, std::invalid_argument);
// preliminary code
virtual unsigned int getProtection() const throw(node_logic_error)
{
return 0;
}
std::string getName() const throw(node_logic_error)
{
return name;
}
std::string getDateCreated() const throw(node_logic_error)
{
return date_created;
}
long getSize() const throw(node_logic_error);
void accept(Visitor& v) const;
~Directory();
};
inline void Directory::accept(Visitor& v) const
{
v.visit(this);
}
// recursive generic method
template<typename Functor> void Directory::DoRecursive(Functor& F, const Directory *pdir, std::string parent_path)
{
std::list<Node *>::const_iterator list_iter = pdir->nodeList.begin();
std::list<Node *>::const_iterator end_iter = pdir->nodeList.end();
std::string dir_name = pdir->getName();
F(pdir, parent_path);
parent_path += dir_name + "/";
for (; list_iter != end_iter; ++list_iter) {
const Node *pNode = *list_iter;
if (dynamic_cast<const Directory *>(pNode)) {
const Directory *p = static_cast<const Directory *>(pNode);
DoRecursive(F, p, parent_path);
} else {
F(pNode, parent_path);
}
}
}
template<typename F> void Directory::traverse(F& f)
{
this->DoRecursive(f, this, std::string(""));
}
inline Node &Directory::DirectoryIterator::operator *() const
{
return *pCurrentNode;
}
inline Directory::DirectoryIterator::DirectoryIterator() : pDirectory(0), pCurrentNode(0), iters_stack()
{
}
inline Directory::DirectoryIterator Directory::begin()
{
return DirectoryIterator(*this);
}
inline Directory::DirectoryIterator Directory::end()
{
return DirectoryIterator();
}
inline bool Directory::DirectoryIterator::operator!=(const DirectoryIterator& rhs) const
{
return !operator==(rhs);
}
inline Directory::ConstDirectoryIterator::ConstDirectoryIterator(const Directory & dir) : inner_iter(const_cast<Directory &>(dir))
{
}
inline Directory::ConstDirectoryIterator::ConstDirectoryIterator() : inner_iter()
{
}
inline Directory::ConstDirectoryIterator::ConstDirectoryIterator(const ConstDirectoryIterator& citer) : inner_iter(citer.inner_iter)
{
}
inline Directory::ConstDirectoryIterator::ConstDirectoryIterator(const DirectoryIterator& iter) : inner_iter(iter)
{
}
inline Directory::ConstDirectoryIterator& Directory::ConstDirectoryIterator::operator++()
{
++inner_iter;
return *this;
}
inline const Node &Directory::ConstDirectoryIterator::operator*() const
{
return const_cast<const Node &>(*inner_iter);
}
inline const Node *Directory::ConstDirectoryIterator::operator->() const
{
return const_cast<const Node *>(inner_iter.operator->());
}
inline bool Directory::ConstDirectoryIterator::operator==(const ConstDirectoryIterator& x) const
{
return inner_iter == x.inner_iter;
}
inline bool Directory::ConstDirectoryIterator::operator!=(const ConstDirectoryIterator& x) const
{
return inner_iter != x.inner_iter;
}
inline Directory::ConstDirectoryIterator& Directory::ConstDirectoryIterator::operator=(const ConstDirectoryIterator& rhs)
{
inner_iter = rhs.inner_iter;
return *this;
}
#endif