Browse Source

Add system::getEntriesRecursive().

tags/v1.1.6
Andrew Belt 5 years ago
parent
commit
798d01d240
2 changed files with 20 additions and 0 deletions
  1. +1
    -0
      include/system.hpp
  2. +19
    -0
      src/system.cpp

+ 1
- 0
include/system.hpp View File

@@ -13,6 +13,7 @@ namespace system {

/** Returns a list of all entries (directories, files, symbols) in a directory. */
std::list<std::string> getEntries(const std::string& path);
std::list<std::string> getEntriesRecursive(const std::string &path, int depth);
/** Returns whether the given path is a file. */
bool isFile(const std::string& path);
/** Returns whether the given path is a directory. */


+ 19
- 0
src/system.cpp View File

@@ -53,6 +53,25 @@ std::list<std::string> getEntries(const std::string& path) {
}


std::list<std::string> getEntriesRecursive(const std::string &path, int depth) {
std::list<std::string> entries = getEntries(path);
if (depth > 0) {
// Don't iterate using iterators because the list will be growing.
size_t limit = entries.size();
auto it = entries.begin();
for (size_t i = 0; i < limit; i++) {
const std::string &entry = *it++;
if (isDirectory(entry)) {
std::list<std::string> subEntries = getEntriesRecursive(entry, depth - 1);
// Append subEntries to entries
entries.splice(entries.end(), subEntries);
}
}
}
return entries;
}


bool isFile(const std::string& path) {
struct stat statbuf;
if (stat(path.c_str(), &statbuf))


Loading…
Cancel
Save