From 798d01d240063392f9b6dbc7f6ca0722b0c54bc2 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 16 Oct 2019 12:45:28 -0400 Subject: [PATCH] Add system::getEntriesRecursive(). --- include/system.hpp | 1 + src/system.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/system.hpp b/include/system.hpp index c482f0b2..77a38cbc 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -13,6 +13,7 @@ namespace system { /** Returns a list of all entries (directories, files, symbols) in a directory. */ std::list getEntries(const std::string& path); +std::list 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. */ diff --git a/src/system.cpp b/src/system.cpp index f0d78093..7ff0d323 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -53,6 +53,25 @@ std::list getEntries(const std::string& path) { } +std::list getEntriesRecursive(const std::string &path, int depth) { + std::list 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 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))