diff --git a/include/system.hpp b/include/system.hpp index b108aab8..1f048d70 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include @@ -24,10 +23,10 @@ template std::string join(const std::string& path1, const std::string& path2, Paths... paths) { return join(join(path1, path2), paths...); } -/** Returns a list of all entries (directories, files, symbolic links, etc) in a directory. +/** Returns all entries (directories, files, symbolic links, etc) in a directory. `depth` is the number of directories to recurse. 0 depth does not recurse. -1 depth recurses infinitely. */ -std::list getEntries(const std::string& dirPath, int depth = 0); +std::vector getEntries(const std::string& dirPath, int depth = 0); 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 3c52f64b..51e53047 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -58,25 +58,26 @@ std::string join(const std::string& path1, const std::string& path2) { } -std::list getEntries(const std::string& dirPath, int depth) { +static void appendEntries(std::vector& entries, const fs::path& dir, int depth) { try { - std::list entries; - for (auto& entry : fs::directory_iterator(fs::u8path(dirPath))) { - std::string subEntry = entry.path().generic_u8string(); - entries.push_back(subEntry); + for (const auto& entry : fs::directory_iterator(dir)) { + entries.push_back(entry.path().generic_u8string()); // Recurse if depth > 0 (limited recursion) or depth < 0 (infinite recursion). if (depth != 0) { - if (fs::is_directory(entry.path())) { - std::list subEntries = getEntries(subEntry, depth - 1); - entries.splice(entries.end(), subEntries); + if (entry.is_directory()) { + appendEntries(entries, entry.path(), depth - 1); } } } - return entries; - } - catch (fs::filesystem_error& e) { - return {}; } + catch (fs::filesystem_error& e) {} +} + + +std::vector getEntries(const std::string& dirPath, int depth) { + std::vector entries; + appendEntries(entries, fs::u8path(dirPath), depth); + return entries; }