aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/src/DirectoryIteratorStrategy.cpp
blob: 21c9755bb4bb0a71ed4e10c7b18f66da6c29cc7a (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
//
// RecursiveDirectoryIteratorStategies.cpp
//
// Library: Foundation
// Package: Filesystem
// Module:  RecursiveDirectoryIterator
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/DirectoryIteratorStrategy.h"


namespace Poco {


//
// TraverseBase
//
TraverseBase::TraverseBase(DepthFunPtr depthDeterminer, UInt16 maxDepth)
	: _depthDeterminer(depthDeterminer), _maxDepth(maxDepth)
{
}


inline bool TraverseBase::isFiniteDepth()
{
	return _maxDepth != D_INFINITE;
}


bool TraverseBase::isDirectory(Poco::File& file)
{
	try
	{
		return file.isDirectory();
	}
	catch (...)
	{
		return false;
	}
}


//
// ChildrenFirstTraverse
//
ChildrenFirstTraverse::ChildrenFirstTraverse(DepthFunPtr depthDeterminer, UInt16 maxDepth)
	: TraverseBase(depthDeterminer, maxDepth)
{
}


const std::string ChildrenFirstTraverse::next(Stack* itStack, bool* isFinished)
{
	// pointer mustn't point to NULL and iteration mustn't be finished
	poco_check_ptr(isFinished);
	poco_assert(!(*isFinished));

	std::stack<DirectoryIterator> it;

	//_depthDeterminer(it);

	// go deeper into not empty directory
	// (if depth limit allows)
	bool isDepthLimitReached = isFiniteDepth() && _depthDeterminer(*itStack) >= _maxDepth;
	if (!isDepthLimitReached && isDirectory(*itStack->top()))
	{
		DirectoryIterator child_it(itStack->top().path());
		// check if directory is empty
		if (child_it != _itEnd)
		{
			itStack->push(child_it);
			return child_it->path();
		}
	}

	++(itStack->top());

	poco_assert(!itStack->empty());
	// return up until there isn't right sibling
	while (itStack->top() == _itEnd)
	{
		itStack->pop();

		// detect end of traversal
		if (itStack->empty())
		{
			*isFinished = true;
			return _itEnd->path();
		}
		else
		{
			++(itStack->top());
		}
	}

	return itStack->top()->path();
}


//
// SiblingsFirstTraverse
//
SiblingsFirstTraverse::SiblingsFirstTraverse(DepthFunPtr depthDeterminer, UInt16 maxDepth)
	: TraverseBase(depthDeterminer, maxDepth)
{
	_dirsStack.push(std::queue<std::string>());
}


const std::string SiblingsFirstTraverse::next(Stack* itStack, bool* isFinished)
{
	// pointer mustn't point to NULL and iteration mustn't be finished
	poco_check_ptr(isFinished);
	poco_assert(!(*isFinished));

	// add dirs to queue (if depth limit allows)
	bool isDepthLimitReached = isFiniteDepth() && _depthDeterminer(*itStack) >= _maxDepth;
	if (!isDepthLimitReached && isDirectory(*itStack->top()))
	{
		const std::string& p = itStack->top()->path();
		_dirsStack.top().push(p);
	}

	++(itStack->top());

	poco_assert(!itStack->empty());
	// return up until there isn't right sibling
	while (itStack->top() == _itEnd)
	{
		// try to find first not empty directory and go deeper
		while (!_dirsStack.top().empty())
		{
			std::string dir = _dirsStack.top().front();
			_dirsStack.top().pop();
			DirectoryIterator child_it(dir);

			// check if directory is empty
			if (child_it != _itEnd)
			{
				itStack->push(child_it);
				_dirsStack.push(std::queue<std::string>());
				return child_it->path();
			}
		}

		// if fail go upper
		itStack->pop();
		_dirsStack.pop();

		// detect end of traversal
		if (itStack->empty())
		{
			*isFinished = true;
			return _itEnd->path();
		}
	}

	return itStack->top()->path();
}


} // namespace Poco