summaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-20 00:53:49 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-20 00:53:49 +0100
commit47311cc8b211a7fef033d744d9eba9f308726ea8 (patch)
tree348726aef17297729233b93b6d7eef86f25c7a78 /src/plugins
parentd836d70ea2352dcf277c6fce91ba1ded3f074b44 (diff)
Refactored stuff surrounding the ResourceLocator class, implemented StaticResourceLocator which can be used for registering static resources (mainly for testing or if certain resources need to be available from the executable)
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/boost/FileLocator.cpp49
-rw-r--r--src/plugins/boost/FileLocator.hpp50
2 files changed, 60 insertions, 39 deletions
diff --git a/src/plugins/boost/FileLocator.cpp b/src/plugins/boost/FileLocator.cpp
index 3db214f..9ab398e 100644
--- a/src/plugins/boost/FileLocator.cpp
+++ b/src/plugins/boost/FileLocator.cpp
@@ -18,46 +18,55 @@
#include "FileLocator.hpp"
+#include <boost/filesystem.hpp>
+
#include <fstream>
namespace ousia {
-void FileLocator::addSearchPath(const boost::filesystem::path &path,
- std::set<ResourceLocator::Type> types)
+void FileLocator::addSearchPath(const std::string &path,
+ std::set<ResourceType> types)
{
+ // Canonicalize the given path
+ std::string canonicalPath =
+ boost::filesystem::canonical(path).generic_string();
+
+ // Insert the path for all given types.
for (auto &type : types) {
- // retrieve the path vector for the given type.
auto it = searchPaths.find(type);
if (it != searchPaths.end()) {
- it->second.push_back(path);
+ it->second.push_back(canonicalPath);
} else {
- std::vector<boost::filesystem::path> v{path};
- searchPaths.insert({type, v});
+ searchPaths.insert({type, {canonicalPath}});
}
}
}
-ResourceLocator::Location FileLocator::locate(const std::string &path,
- const std::string &relativeTo,
- const Type type) const
+bool FileLocator::doLocate(Resource &resource, const std::string &path,
+ const ResourceType type,
+ const std::string &relativeTo) const
{
boost::filesystem::path base(relativeTo);
if (boost::filesystem::exists(base)) {
- // look if 'relativeTo' is a directory already.
+ // Look if 'relativeTo' is a directory already.
if (!boost::filesystem::is_directory(base)) {
- // if not we use the parent directory.
+ // If not we use the parent directory.
base = base.parent_path();
}
- // use the / operator to append the path.
+
+ // Use the / operator to append the path.
base /= path;
- // if we already found a fitting resource there, use that.
+
+ // If we already found a fitting resource there, use that.
if (boost::filesystem::exists(base)) {
std::string location =
boost::filesystem::canonical(base).generic_string();
- return ResourceLocator::Location(true, *this, type, location);
+ resource = Resource(true, *this, type, location);
+ return true;
}
}
- // otherwise look in the search paths.
+
+ // Otherwise look in the search paths.
auto it = searchPaths.find(type);
if (it != searchPaths.end()) {
for (boost::filesystem::path p : it->second) {
@@ -65,17 +74,15 @@ ResourceLocator::Location FileLocator::locate(const std::string &path,
if (boost::filesystem::exists(p)) {
std::string location =
boost::filesystem::canonical(p).generic_string();
- return ResourceLocator::Location(true, *this, type, location);
+ resource = Resource(true, *this, type, location);
+ return true;
}
}
}
- // if we find the resource in none of the search paths we return a location.
- // with the found flag set to false.
- ResourceLocator::Location l(false, *this, type, "");
- return l;
+ return false;
}
-std::unique_ptr<std::istream> FileLocator::stream(
+std::unique_ptr<std::istream> FileLocator::doStream(
const std::string &location) const
{
std::unique_ptr<std::istream> ifs{new std::ifstream(location)};
diff --git a/src/plugins/boost/FileLocator.hpp b/src/plugins/boost/FileLocator.hpp
index 9cb705c..ffe5c8f 100644
--- a/src/plugins/boost/FileLocator.hpp
+++ b/src/plugins/boost/FileLocator.hpp
@@ -19,15 +19,13 @@
#ifndef _OUSIA_FILE_LOCATOR_HPP_
#define _OUSIA_FILE_LOCATOR_HPP_
-#include <core/ResourceLocator.hpp>
+#include <core/resource/Resource.hpp>
+#include <core/resource/ResourceLocator.hpp>
#include <map>
#include <set>
#include <vector>
-#include <boost/filesystem.hpp>
-
-
namespace ousia {
/**
@@ -39,8 +37,25 @@ namespace ousia {
* locations (e.g. online resources, .zip files, etc.).
*/
class FileLocator : public ResourceLocator {
+public:
+ /**
+ * Type alias representing a Res
+ */
+ using SearchPaths = std::map<ResourceType, std::vector<std::string>>;
+
private:
- std::map<ResourceLocator::Type, std::vector<boost::filesystem::path>> searchPaths;
+ /**
+ * Internal variable containing all stored search paths.
+ */
+ SearchPaths searchPaths;
+
+protected:
+ bool doLocate(Resource &resource, const std::string &path,
+ const ResourceType type,
+ const std::string &relativeTo) const override;
+
+ std::unique_ptr<std::istream> doStream(
+ const std::string &location) const override;
public:
FileLocator() : searchPaths() {}
@@ -53,24 +68,23 @@ public:
* resources of the specified types at the given path in the
* future.
*/
- void addSearchPath(const boost::filesystem::path &path,
- std::set<ResourceLocator::Type> types);
+ void addSearchPath(const std::string &path, std::set<ResourceType> types);
+
+ /**
+ * Adds a search path. Implicitly adds the search path for the "unknown"
+ *
+ * @param path is a fully qualified/canonical path to a directory.
+ * @param types is a set of Resource Types. The FileLocator will look for
+ * resources of the specified types at the given path in the
+ * future.
+ */
+ void addSearchPath(const std::string &path);
/**
* Returns the backing map containing all search paths for a given type.
* This is read-only.
*/
- const std::map<ResourceLocator::Type, std::vector<boost::filesystem::path>> &
- getSearchPaths() const
- {
- return searchPaths;
- }
-
- Location locate(const std::string &path, const std::string &relativeTo,
- const Type type) const override;
-
- std::unique_ptr<std::istream> stream(
- const std::string &location) const override;
+ const SearchPaths &getSearchPaths() const { return searchPaths; }
};
}