From 97390d565eca8004edb1d262c282aaf9e47c3b7b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 26 Nov 2018 23:19:40 +0100 Subject: fs: implement filesystem access --- src/filesystem.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/filesystem.cpp (limited to 'src/filesystem.cpp') diff --git a/src/filesystem.cpp b/src/filesystem.cpp new file mode 100644 index 0000000..4dfa183 --- /dev/null +++ b/src/filesystem.cpp @@ -0,0 +1,30 @@ +#include "filesystem.hpp" + +#include + +#include + +namespace wanda +{ + +std::optional scan(std::filesystem::path source, bool(filter)(std::filesystem::path const &)) +{ + if (!std::filesystem::is_directory(source)) + { + return std::nullopt; + } + + auto first = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{source}); + auto last = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{}); + return path_list{first, last}; +} + +std::filesystem::path random_pick(path_list const &paths) +{ + static auto generator = std::mt19937{std::random_device{}()}; + auto distribution = std::uniform_int_distribution{0, paths.size() - 1}; + + return paths[distribution(generator)]; +} + +} // namespace wanda \ No newline at end of file -- cgit v1.2.3