@@ -1,5 +1,4 @@ | |||||
#pragma once | #pragma once | ||||
#include <list> | |||||
#include <vector> | #include <vector> | ||||
#include <common.hpp> | #include <common.hpp> | ||||
@@ -24,10 +23,10 @@ template <typename... Paths> | |||||
std::string join(const std::string& path1, const std::string& path2, Paths... paths) { | std::string join(const std::string& path1, const std::string& path2, Paths... paths) { | ||||
return join(join(path1, path2), 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. | `depth` is the number of directories to recurse. 0 depth does not recurse. -1 depth recurses infinitely. | ||||
*/ | */ | ||||
std::list<std::string> getEntries(const std::string& dirPath, int depth = 0); | |||||
std::vector<std::string> getEntries(const std::string& dirPath, int depth = 0); | |||||
bool exists(const std::string& path); | bool exists(const std::string& path); | ||||
/** Returns whether the given path is a file. */ | /** Returns whether the given path is a file. */ | ||||
bool isFile(const std::string& path); | bool isFile(const std::string& path); | ||||
@@ -58,25 +58,26 @@ std::string join(const std::string& path1, const std::string& path2) { | |||||
} | } | ||||
std::list<std::string> getEntries(const std::string& dirPath, int depth) { | |||||
static void appendEntries(std::vector<std::string>& entries, const fs::path& dir, int depth) { | |||||
try { | try { | ||||
std::list<std::string> 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). | // Recurse if depth > 0 (limited recursion) or depth < 0 (infinite recursion). | ||||
if (depth != 0) { | if (depth != 0) { | ||||
if (fs::is_directory(entry.path())) { | |||||
std::list<std::string> 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<std::string> getEntries(const std::string& dirPath, int depth) { | |||||
std::vector<std::string> entries; | |||||
appendEntries(entries, fs::u8path(dirPath), depth); | |||||
return entries; | |||||
} | } | ||||