From 577fc0845718ed8ad5bebf02a277c0579a817f77 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 17 May 2024 17:58:38 +0200 Subject: wanda: restructure source layout --- source/lib/src/system/filesystem.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 source/lib/src/system/filesystem.cpp (limited to 'source/lib/src/system/filesystem.cpp') diff --git a/source/lib/src/system/filesystem.cpp b/source/lib/src/system/filesystem.cpp new file mode 100644 index 0000000..04dfff3 --- /dev/null +++ b/source/lib/src/system/filesystem.cpp @@ -0,0 +1,31 @@ +#include "wanda/system/filesystem.hpp" + +#include +#include + +namespace wanda::system +{ + std::optional scan(std::filesystem::path source, bool(filter)(std::filesystem::path const &)) + { + if (!std::filesystem::is_directory(source)) + { + return std::nullopt; + } + auto entries = std::filesystem::recursive_directory_iterator{source}; + auto result = path_list{}; + for (auto & entry : entries | std::views::filter(filter)) + { + result.push_back(entry.path()); + } + return result; + } + + 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::system \ No newline at end of file -- cgit v1.2.3