aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/include/Poco/RecursiveDirectoryIterator.h
blob: c0ce2285eeef6668ec0c8241cc02a90e8715f739 (plain) (blame)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// RecursiveDirectoryIterator.h
//
// Library: Foundation
// Package: Filesystem
// Module:  RecursiveDirectoryIterator
//
// Definition of the RecursiveDirectoryIterator class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#ifndef Foundation_RecursiveDirectoryIterator_INCLUDED
#define Foundation_RecursiveDirectoryIterator_INCLUDED


#include "Poco/Foundation.h"
#include "Poco/File.h"
#include "Poco/Path.h"
#include "Poco/RecursiveDirectoryIteratorImpl.h"
#include "Poco/DirectoryIteratorStrategy.h"


namespace Poco {


class DirectoryIterator;


template<class TTravStr>
class RecursiveDirectoryIteratorImpl;


template<class TTravStr = ChildrenFirstTraverse>
class RecursiveDirectoryIterator
	/// The RecursiveDirectoryIterator class is used to enumerate
	/// all files in a directory and its subdirectories.
	///
	/// RecursiveDirectoryIterator has some limitations:
	///   * only forward iteration (++) is supported
	///   * an iterator copied from another one will always
	///     point to the same file as the original iterator,
	///     even is the original iterator has been advanced
	///     (all copies of an iterator share their state with
	///     the original iterator)
	///
	/// The class can follow different traversal strategies:
	///     * depth-first strategy;
	///     * siblings-first strategy.
	/// The stategies are set by template parameter.
	/// There are two corresponding typedefs:
	///     * SimpleRecursiveDirectoryIterator;
	///     * SiblingsFirstRecursiveDirectoryIterator.
	///
	/// The depth of traversal can be limited by constructor
	/// parameter maxDepth (which sets the infinite depth by default).
{
public:
	typedef RecursiveDirectoryIterator<TTravStr> MyType;

	enum
	{
		D_INFINITE = 0 /// Constant for infinite traverse depth.
	};

	RecursiveDirectoryIterator()
		/// Creates the end iterator.
		: _pImpl(0)
	{
	}

	RecursiveDirectoryIterator(const std::string& path, UInt16 maxDepth = D_INFINITE)
		/// Creates a recursive directory iterator for the given path.
		: _pImpl(new ImplType(path, maxDepth)), _path(Path(_pImpl->get())), _file(_path)
	{
	}

	RecursiveDirectoryIterator(const MyType& iterator):
		/// Creates a copy of another recursive directory iterator.
		_pImpl(iterator._pImpl), _path(iterator._path), _file(iterator._file)
	{
	}

	RecursiveDirectoryIterator(const DirectoryIterator& iterator, UInt16 maxDepth = D_INFINITE):
		/// Creates a recursive directory iterator for the path of
		/// non-recursive directory iterator.
		_pImpl(new ImplType(iterator->path(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
	{
	}

	RecursiveDirectoryIterator(const File& file, UInt16 maxDepth = D_INFINITE):
		/// Creates a recursive directory iterator for the given path.
		_pImpl(new ImplType(file.path(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
	{
	}

	RecursiveDirectoryIterator(const Path& path, UInt16 maxDepth = D_INFINITE):
		/// Creates a recursive directory iterator for the given path.
		_pImpl(new ImplType(path.toString(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
	{
	}

	~RecursiveDirectoryIterator()
		/// Destroys the DirectoryIterator.
	{
		if (_pImpl)
			_pImpl->release();
	}

	const std::string& name() const
		/// Returns the current filename.
	{
		return _path.getFileName();
	}

	const Poco::Path& path() const
		/// Returns the current path.
	{
		return _path;
	}

	UInt16 depth() const
		/// Depth of recursion (counting from 1).
	{
		return _pImpl->depth();
	}

	UInt16 maxDepth() const
		/// Max depth of recursion (counting from 1).
	{
		return _pImpl->maxDepth();
	}


	MyType& operator = (const MyType& it)
	{
		if (_pImpl)
			_pImpl->release();
		_pImpl = it._pImpl;
		if (_pImpl)
		{
			_pImpl->duplicate();
			_path = it._path;
			_file = _path;
		}
		return *this;
	}

	MyType& operator = (const File& file)
	{
		if (_pImpl)
			_pImpl->release();
		_pImpl = new ImplType(file.path());
		_path = Path(_pImpl->get());
		_file = _path;
		return *this;
	}


	MyType& operator = (const Path& path)
	{
		if (_pImpl)
			_pImpl->release();
		_pImpl = new ImplType(path.toString());
		_path = Path(_pImpl->get());
		_file = _path;
		return *this;
	}

	MyType& operator = (const std::string& path)
	{
		if (_pImpl)
			_pImpl->release();
		_pImpl = new ImplType(path);
		_path = Path(_pImpl->get());
		_file = _path;
		return *this;
	}

	MyType& operator ++ ()
	{
		if (_pImpl)
		{
			_path = Path(_pImpl->next());
			_file = _path;
		}
		return *this;
	}

	const File& operator * () const
	{
		return _file;
	}

	File& operator *()
	{
		return _file;
	}

	const File* operator -> () const
	{
		return &_file;
	}

	File* operator -> ()
	{
		return &_file;
	}

	template<class T1, class T2>
	friend inline bool operator ==(const RecursiveDirectoryIterator<T1>& a, const RecursiveDirectoryIterator<T2>& b);
	template<class T1, class T2>
	friend inline bool operator !=(const RecursiveDirectoryIterator<T1>& a, const RecursiveDirectoryIterator<T2>& b);

private:
	typedef RecursiveDirectoryIteratorImpl<TTravStr> ImplType;

	ImplType* _pImpl;
	Path _path;
	File _file;
};


//
// friend comparsion operators
//
template<class T1, class T2>
inline bool operator ==(const RecursiveDirectoryIterator<T1>& a, const RecursiveDirectoryIterator<T2>& b)
{
	return a.path().toString() == b.path().toString();;
}

template<class T1, class T2>
inline bool operator !=(const RecursiveDirectoryIterator<T1>& a, const RecursiveDirectoryIterator<T2>& b)
{
	return a.path().toString() != b.path().toString();;
}


//
// typedefs
//
typedef RecursiveDirectoryIterator<ChildrenFirstTraverse> SimpleRecursiveDirectoryIterator;
typedef RecursiveDirectoryIterator<SiblingsFirstTraverse> SiblingsFirstRecursiveDirectoryIterator;


} // namespace Poco


#endif // Foundation_RecursiveDirectoryIterator_INCLUDED