Skip to content

Commit 0e2ab05

Browse files
libsolutil: Adding findFilesRecursively() helper to find files recursively.
1 parent a3de6cd commit 0e2ab05

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

libsolutil/CommonIO.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,46 @@ inline std::ostream& operator<<(std::ostream& os, bytes const& _bytes)
4848
namespace util
4949
{
5050

51+
namespace detail
52+
{
53+
54+
template <typename Predicate>
55+
struct RecursiveFileCollector
56+
{
57+
Predicate predicate;
58+
std::vector<boost::filesystem::path> result {};
59+
60+
RecursiveFileCollector& operator()(boost::filesystem::path const& _directory)
61+
{
62+
if (!boost::filesystem::is_directory(_directory))
63+
return *this;
64+
auto iterator = boost::filesystem::directory_iterator(_directory);
65+
auto const iteratorEnd = boost::filesystem::directory_iterator();
66+
67+
while (iterator != iteratorEnd)
68+
{
69+
if (boost::filesystem::is_directory(iterator->status()))
70+
(*this)(iterator->path());
71+
72+
if (predicate(iterator->path()))
73+
result.push_back(iterator->path());
74+
75+
++iterator;
76+
}
77+
return *this;
78+
}
79+
};
80+
81+
template <typename Predicate>
82+
RecursiveFileCollector(Predicate) -> RecursiveFileCollector<Predicate>;
83+
}
84+
85+
template <typename Predicate>
86+
std::vector<boost::filesystem::path> findFilesRecursively(boost::filesystem::path const& _rootDirectory, Predicate _predicate)
87+
{
88+
return detail::RecursiveFileCollector{_predicate}(_rootDirectory).result;
89+
}
90+
5191
/// Retrieves and returns the contents of the given file as a std::string.
5292
/// If the file doesn't exist, it will throw a FileNotFound exception.
5393
/// If the file exists but is not a regular file, it will throw NotAFile exception.

0 commit comments

Comments
 (0)