diff --git a/include/system.hpp b/include/system.hpp index 16172c91..4419939c 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -24,6 +24,10 @@ std::string join(const std::string& path1, const std::string& path2, Paths... pa `depth` is the number of directories to recurse. 0 depth does not recurse. -1 depth recurses infinitely. */ std::vector getEntries(const std::string& dirPath, int depth = 0); +/** Expands a glob pattern such as `dir/file*.txt` to a list of paths. +Paths are sorted. +*/ +std::vector glob(const std::string& pattern); bool exists(const std::string& path); /** Returns whether the given path is a file. */ bool isFile(const std::string& path); diff --git a/src/system.cpp b/src/system.cpp index d4130b18..2e0a1bc3 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -9,6 +9,7 @@ #include #include #include // for abi::__cxa_demangle +#include #if defined ARCH_LIN || defined ARCH_MAC #include @@ -88,6 +89,22 @@ std::vector getEntries(const std::string& dirPath, int depth) { } +std::vector glob(const std::string& pattern) { + glob_t glob_result; + memset(&glob_result, 0, sizeof(glob_result)); + std::vector paths; + + if (!glob(pattern.c_str(), GLOB_BRACE, NULL, &glob_result)) { + for (size_t i = 0; i < glob_result.gl_pathc; i++) { + paths.push_back(std::string(glob_result.gl_pathv[i])); + } + } + + globfree(&glob_result); + return paths; +} + + bool exists(const std::string& path) { try { return fs::exists(fs::u8path(path));