From 01f9e76fe331ef271e7daaad1029352c8d5f56d9 Mon Sep 17 00:00:00 2001 From: ilor <kailoran@gmail.com> Date: Thu, 5 May 2011 15:06:32 +0200 Subject: [PATCH] path searcher updates --- libpwrutils/pathsearch.cpp | 4 ++-- libpwrutils/pathsearch.h | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/libpwrutils/pathsearch.cpp b/libpwrutils/pathsearch.cpp index 15f4e99..1584d79 100644 --- a/libpwrutils/pathsearch.cpp +++ b/libpwrutils/pathsearch.cpp @@ -72,7 +72,7 @@ const std::string& PathSearcherBase::get_path_separator() const } std::string PathSearcherBase::find_file(const std::string& filename, - const std::string& info) + const std::string& info) const { boost::filesystem::path i(filename); if (i.is_complete()) { @@ -101,7 +101,7 @@ std::string PathSearcherBase::find_file(const std::string& filename, } bool PathSearcherBase::open_stream(const std::string& filename, - std::ifstream& ifs, const std::string& info) + std::ifstream& ifs, const std::string& info) const { std::string f = find_file(filename, info); if (!f.empty()) { diff --git a/libpwrutils/pathsearch.h b/libpwrutils/pathsearch.h index 956f68d..c33aaa7 100644 --- a/libpwrutils/pathsearch.h +++ b/libpwrutils/pathsearch.h @@ -73,7 +73,7 @@ public: * is on. Empty info string suppreses loading info. */ std::string find_file(const std::string& filename, - const std::string& info = ""); + const std::string& info = "") const; /** * Open a file stream for a file in the library search path @@ -83,7 +83,22 @@ public: * is on. Empty info string suppreses loading info. */ bool open_stream(const std::string& filename, std::ifstream& ifs, - const std::string& info = ""); + const std::string& info = "") const; + + /** + * Convenience wrapper around find_file to throw an exception + * when the file is not found. + * Virtual, as it throws the exception defined by the child class. + */ + virtual std::string find_file_or_throw(const std::string& filename, + const std::string& where) const = 0; + + /** + * Wrapper around open_stream to throw an exception when the file is not + * found. Virtual, as it throws the exception defined by the child class. + */ + virtual void open_stream_or_throw(const std::string& filename, + std::ifstream& ifs, const std::string& where) const = 0; /** * Look for files matching a condition. @@ -122,14 +137,14 @@ public: * when the file is not found. */ std::string find_file_or_throw(const std::string& filename, - const std::string& where); + const std::string& where) const; /** * Convenience template wrapper around open_stream to throw an * exception when the file is not found. */ void open_stream_or_throw(const std::string& filename, - std::ifstream& ifs, const std::string& where); + std::ifstream& ifs, const std::string& where) const; }; @@ -158,7 +173,7 @@ private: template<class E> std::string PathSearcher<E>::find_file_or_throw( - const std::string& filename, const std::string& info) + const std::string& filename, const std::string& info) const { std::string fn = find_file(filename, info); if (fn.empty()) { @@ -169,7 +184,7 @@ std::string PathSearcher<E>::find_file_or_throw( template<class E> void PathSearcher<E>::open_stream_or_throw(const std::string& filename, - std::ifstream& ifs, const std::string& info) + std::ifstream& ifs, const std::string& info) const { if (!open_stream(filename, ifs, info)) { throw E(filename, get_search_path_string(), info); -- GitLab