diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/filesystem/FileLocator.cpp | 47 | ||||
-rw-r--r-- | src/plugins/filesystem/FileLocator.hpp | 9 |
2 files changed, 37 insertions, 19 deletions
diff --git a/src/plugins/filesystem/FileLocator.cpp b/src/plugins/filesystem/FileLocator.cpp index 356394e..822c9fe 100644 --- a/src/plugins/filesystem/FileLocator.cpp +++ b/src/plugins/filesystem/FileLocator.cpp @@ -101,6 +101,11 @@ void FileLocator::addDefaultSearchPaths() #ifndef NDEBUG addDefaultSearchPaths(SpecialPaths::getDebugDataDir()); #endif + // also add working directory. + addSearchPath(".", {ResourceType::UNKNOWN, ResourceType::DOMAIN_DESC, + ResourceType::TYPESYSTEM, ResourceType::DOCUMENT, + ResourceType::ATTRIBUTES, ResourceType::STYLESHEET, + ResourceType::SCRIPT, ResourceType::DATA}); } void FileLocator::addUnittestSearchPath(const std::string &subdir, @@ -111,6 +116,20 @@ void FileLocator::addUnittestSearchPath(const std::string &subdir, type); } +static bool checkPath(Resource &resource, const ResourceLocator &locator, + fs::path p, const ResourceType type) +{ + if (fs::exists(p) && fs::is_regular_file(p)) { + std::string location = fs::canonical(p).generic_string(); +#ifdef FILELOCATOR_DEBUG_PRINT + std::cout << "FileLocator: Found at " << location << std::endl; +#endif + resource = Resource(true, locator, type, location); + return true; + } + return false; +} + bool FileLocator::doLocate(Resource &resource, const std::string &path, const ResourceType type, const std::string &relativeTo) const @@ -131,13 +150,7 @@ bool FileLocator::doLocate(Resource &resource, const std::string &path, base /= path; // If we already found a fitting resource there, use that. - if (fs::exists(base) && fs::is_regular_file(base)) { - std::string location = fs::canonical(base).generic_string(); -#ifdef FILELOCATOR_DEBUG_PRINT - std::cout << "FileLocator: Found \"" << path << "\" at " - << location << std::endl; -#endif - resource = Resource(true, *this, type, location); + if (checkPath(resource, *this, base, type)) { return true; } } @@ -148,6 +161,18 @@ bool FileLocator::doLocate(Resource &resource, const std::string &path, return false; } + // If the path is an absolute path, look at this exact point. + { + fs::path p{path}; + if (p.is_absolute()) { + if (checkPath(resource, *this, p, type)) { + return true; + } else { + return false; + } + } + } + // Otherwise look in the search paths, search backwards, last defined search // paths have a higher precedence auto it = searchPaths.find(type); @@ -159,13 +184,7 @@ bool FileLocator::doLocate(Resource &resource, const std::string &path, #endif fs::path p{*it}; p /= path; - if (fs::exists(p) && fs::is_regular_file(p)) { - std::string location = fs::canonical(p).generic_string(); -#ifdef FILELOCATOR_DEBUG_PRINT - std::cout << "FileLocator: Found \"" << path << "\" in " - << location << std::endl; -#endif - resource = Resource(true, *this, type, location); + if (checkPath(resource, *this, p, type)) { return true; } } diff --git a/src/plugins/filesystem/FileLocator.hpp b/src/plugins/filesystem/FileLocator.hpp index 16fa3f7..1c3e494 100644 --- a/src/plugins/filesystem/FileLocator.hpp +++ b/src/plugins/filesystem/FileLocator.hpp @@ -38,12 +38,11 @@ namespace ousia { /** - * A ResourceLocator is a class able to locate resources in some way, usually - * on the hard drive. + * @file FileLocator.hpp + * + * This is a FileLocator employing the boost path class for file finding. * - * We specify this as an abstract superclass to have an interface layer between - * the program core and possible future extensions in terms of resource - * locations (e.g. online resources, .zip files, etc.). + * @author Benjamin Paassen - bpaassen@techfak.uni-bielefeld.de */ class FileLocator : public ResourceLocator { public: |