Browse Source

Make system::getEntries() return vector instead of list.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
2eb4967261
2 changed files with 15 additions and 15 deletions
  1. +2
    -3
      include/system.hpp
  2. +13
    -12
      src/system.cpp

+ 2
- 3
include/system.hpp View File

@@ -1,5 +1,4 @@
#pragma once
#include <list>
#include <vector>

#include <common.hpp>
@@ -24,10 +23,10 @@ template <typename... Paths>
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<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);
/** Returns whether the given path is a file. */
bool isFile(const std::string& path);


+ 13
- 12
src/system.cpp View File

@@ -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 {
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).
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;
}




Loading…
Cancel
Save