diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/Registry.cpp | 10 | ||||
-rw-r--r-- | src/core/resource/ResourceLocator.cpp | 24 | ||||
-rw-r--r-- | src/plugins/filesystem/FileLocator.cpp | 28 | ||||
-rw-r--r-- | src/plugins/filesystem/FileLocator.hpp | 11 | ||||
-rw-r--r-- | src/plugins/filesystem/SpecialPaths.cpp | 8 |
5 files changed, 55 insertions, 26 deletions
diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp index 9427aed..86665a2 100644 --- a/src/core/Registry.cpp +++ b/src/core/Registry.cpp @@ -70,16 +70,6 @@ bool Registry::locateResource(Resource &resource, const std::string &path, } } - // If this did not work out, retry but use the UNKNOWN type. - if (type != ResourceType::UNKNOWN) { - for (auto &locator : locators) { - if (locator->locate(resource, path, ResourceType::UNKNOWN, - relativeTo)) { - return true; - } - } - } - return false; } } diff --git a/src/core/resource/ResourceLocator.cpp b/src/core/resource/ResourceLocator.cpp index 1b8490d..b19542e 100644 --- a/src/core/resource/ResourceLocator.cpp +++ b/src/core/resource/ResourceLocator.cpp @@ -29,17 +29,28 @@ bool ResourceLocator::locate(Resource &resource, const std::string &path, const ResourceType type, const Resource &relativeTo) const { + // If the locator of the given relative resource is this locator instance, + // use the location specified in the resource, otherwise just use no + // "relativeTo" path. if (&relativeTo.getLocator() == this) { - return doLocate(resource, path, type, relativeTo.getLocation()); + return locate(resource, path, type, relativeTo.getLocation()); } - return doLocate(resource, path, type, ""); + return locate(resource, path, type, ""); } bool ResourceLocator::locate(Resource &resource, const std::string &path, const ResourceType type, const std::string &relativeTo) const { - return doLocate(resource, path, type, relativeTo); + // Try to locate the resource for the specified type, if not found, use + // the "UNKNOWN" type. + if (doLocate(resource, path, type, relativeTo)) { + return true; + } + if (type != ResourceType::UNKNOWN) { + return doLocate(resource, path, ResourceType::UNKNOWN, relativeTo); + } + return false; } std::unique_ptr<std::istream> ResourceLocator::stream( @@ -50,9 +61,10 @@ std::unique_ptr<std::istream> ResourceLocator::stream( /* Class StaticResourceLocator */ -bool StaticResourceLocator::doLocate( - Resource &resource, const std::string &path, const ResourceType type, - const std::string &relativeTo) const +bool StaticResourceLocator::doLocate(Resource &resource, + const std::string &path, + const ResourceType type, + const std::string &relativeTo) const { auto it = resources.find(path); if (it != resources.end()) { diff --git a/src/plugins/filesystem/FileLocator.cpp b/src/plugins/filesystem/FileLocator.cpp index 7bb3a58..9a558eb 100644 --- a/src/plugins/filesystem/FileLocator.cpp +++ b/src/plugins/filesystem/FileLocator.cpp @@ -17,6 +17,10 @@ */ #ifndef NDEBUG +//#define FILELOCATOR_DEBUG_PRINT +#endif + +#ifdef FILELOCATOR_DEBUG_PRINT #include <iostream> #endif @@ -44,6 +48,7 @@ void FileLocator::addPath(const std::string &path, void FileLocator::addSearchPath(const std::string &path, std::set<ResourceType> types) { + // Skip empty or non-existant paths if (path.empty() || !fs::exists(path)) { return; } @@ -51,7 +56,7 @@ void FileLocator::addSearchPath(const std::string &path, // Canonicalize the given path, check whether it exists std::string canonicalPath = fs::canonical(path).generic_string(); -#ifndef NDEBUG +#ifdef FILELOCATOR_DEBUG_PRINT std::cout << "FileLocator: Adding search path " << canonicalPath << std::endl; #endif @@ -99,14 +104,22 @@ void FileLocator::addDefaultSearchPaths() addDefaultSearchPaths(SpecialPaths::getLocalDataDir()); #ifndef NDEBUG addDefaultSearchPaths(SpecialPaths::getDebugDataDir()); - addDefaultSearchPaths(SpecialPaths::getDebugTestdataDir()); #endif } +void FileLocator::addUnittestSearchPath(const std::string &subdir, ResourceType type) +{ + addSearchPath((fs::path{SpecialPaths::getDebugTestdataDir()} / subdir) + .generic_string(), type); +} + bool FileLocator::doLocate(Resource &resource, const std::string &path, const ResourceType type, const std::string &relativeTo) const { +#ifdef FILELOCATOR_DEBUG_PRINT + std::cout << "FileLocator: Searching for \"" << path << "\"" << std::endl; +#endif if (!relativeTo.empty()) { fs::path base(relativeTo); if (fs::exists(base)) { @@ -122,7 +135,7 @@ bool FileLocator::doLocate(Resource &resource, const std::string &path, // If we already found a fitting resource there, use that. if (fs::exists(base)) { std::string location = fs::canonical(base).generic_string(); -#ifndef NDEBUG +#ifdef FILELOCATOR_DEBUG_PRINT std::cout << "FileLocator: Found \"" << path << "\" at " << location << std::endl; #endif @@ -138,16 +151,15 @@ bool FileLocator::doLocate(Resource &resource, const std::string &path, if (it != searchPaths.end()) { const auto &paths = it->second; for (auto it = paths.rbegin(); it != paths.rend(); it++) { -#ifndef NDEBUG - std::cout << "FileLocator: Searching for \"" << path << "\" in " - << *it << std::endl; +#ifdef FILELOCATOR_DEBUG_PRINT + std::cout << "FileLocator: Entering " << *it << std::endl; #endif fs::path p{*it}; p /= path; if (fs::exists(p)) { std::string location = fs::canonical(p).generic_string(); -#ifndef NDEBUG - std::cout << "FileLocator: Found \"" << path << "\" at " +#ifdef FILELOCATOR_DEBUG_PRINT + std::cout << "FileLocator: Found \"" << path << "\" in " << location << std::endl; #endif resource = Resource(true, *this, type, location); diff --git a/src/plugins/filesystem/FileLocator.hpp b/src/plugins/filesystem/FileLocator.hpp index 6d3bbf0..16fa3f7 100644 --- a/src/plugins/filesystem/FileLocator.hpp +++ b/src/plugins/filesystem/FileLocator.hpp @@ -122,6 +122,17 @@ public: void addDefaultSearchPaths(); /** + * Adds a search path for a unit test. This function should not be used + * outside the code in the "test" folder. + * + * @param subdir is the subdirectory in the "testdata" directory that should + * be added. + * @param type is a single type for which the path should be added. + */ + void addUnittestSearchPath(const std::string &subdir, + ResourceType type = ResourceType::UNKNOWN); + + /** * Returns the backing map containing all search paths for a given type. * This is read-only. */ diff --git a/src/plugins/filesystem/SpecialPaths.cpp b/src/plugins/filesystem/SpecialPaths.cpp index 97bdb9c..47ac0f0 100644 --- a/src/plugins/filesystem/SpecialPaths.cpp +++ b/src/plugins/filesystem/SpecialPaths.cpp @@ -59,8 +59,12 @@ std::string SpecialPaths::getDebugDataDir() std::string SpecialPaths::getDebugTestdataDir() { - fs::path debug{OUSIA_DEBUG_DIR}; - return (debug / "testdata").generic_string(); + std::string debug{OUSIA_DEBUG_DIR}; + if (debug.empty()) { + return "./testdata"; + } else { + return (fs::path{debug} / "testdata").generic_string(); + } } } |