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

#include "Poco/SortedDirectoryIterator.h"
#include <algorithm>


namespace Poco {


SortedDirectoryIterator::SortedDirectoryIterator()
	: DirectoryIterator(), _is_finished(true)
{
}


SortedDirectoryIterator::SortedDirectoryIterator(const std::string& rPath)
	: DirectoryIterator(rPath), _is_finished(false)
{
	scan();
	next();
}


SortedDirectoryIterator::SortedDirectoryIterator(const DirectoryIterator& iterator)
	: DirectoryIterator(iterator), _is_finished(false)
{
	scan();
	next();
}


SortedDirectoryIterator::SortedDirectoryIterator(const File& file)
	: DirectoryIterator(file), _is_finished(false)
{
	scan();
	next();
}


SortedDirectoryIterator::SortedDirectoryIterator(const Path& rPath)
	: DirectoryIterator(rPath), _is_finished(false)
{
	scan();
	next();
}


SortedDirectoryIterator::~SortedDirectoryIterator()
{
}


SortedDirectoryIterator& SortedDirectoryIterator::operator ++()
{
	if (!_is_finished)
	{
		next();
	}
	return *this;
}


void SortedDirectoryIterator::scan()
{
	DirectoryIterator end_it;
	while (*this != end_it)
	{
		bool isDir = false;
		try
		{
			isDir = (*this)->isDirectory();
		}
		catch (...) {}
		if (isDir)
			_directories.push_back(_path.toString());
		else
			_files.push_back(_path.toString());

		DirectoryIterator::operator++();
	}

	std::sort(_directories.begin(), _directories.end());
	std::sort(_files.begin(), _files.end());
}


void SortedDirectoryIterator::next()
{
	DirectoryIterator end_it;
	if (!_directories.empty())
	{
		_path.assign(_directories.front());
		_directories.pop_front();
		_file = _path;
	}
	else if (!_files.empty())
	{
		_path.assign(_files.front());
		_files.pop_front();
		_file = _path;
	}
	else
	{
		_is_finished = true;
		_path = end_it.path();
		_file = _path;
	}
}


} // namespace Poco