From c9c06d392a84b93b7b9ad75dc061e25f982d6ff6 Mon Sep 17 00:00:00 2001 From: ilor <kailoran@gmail.com> Date: Sun, 10 Apr 2011 14:09:31 +0200 Subject: [PATCH] add PathSearcher::list_files to find a list of files in the search path matching an extension --- libpwrutils/pathsearch.cpp | 18 ++++++++++++++++++ libpwrutils/pathsearch.h | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/libpwrutils/pathsearch.cpp b/libpwrutils/pathsearch.cpp index e130c02..c89b7a4 100644 --- a/libpwrutils/pathsearch.cpp +++ b/libpwrutils/pathsearch.cpp @@ -100,6 +100,24 @@ bool PathSearcherBase::open_stream(const std::string& filename, return false; } +std::vector<std::string> PathSearcherBase::list_files(const std::string& suffix) const +{ + using boost::filesystem::directory_iterator; + std::vector<std::string> out; + foreach (const std::string& s, get_search_path()) { + boost::filesystem::path p(s); + if (boost::filesystem::is_directory(s)) { + for (directory_iterator i(p); i != directory_iterator(); ++i) { + boost::filesystem::path in = i->path(); + if (in.extension() == suffix) { + out.push_back(in.stem()); + } + } + } + } + return out; +} + ConfigPathSetter::ConfigPathSetter(PathSearcherBase& ps, const std::string &new_path) diff --git a/libpwrutils/pathsearch.h b/libpwrutils/pathsearch.h index 6a1696a..956f68d 100644 --- a/libpwrutils/pathsearch.h +++ b/libpwrutils/pathsearch.h @@ -85,6 +85,11 @@ public: bool open_stream(const std::string& filename, std::ifstream& ifs, const std::string& info = ""); + /** + * Look for files matching a condition. + */ + std::vector<std::string> list_files(const std::string& suffix) const; + private: /// The search paths std::vector<std::string> paths_; -- GitLab