From ea99528bdefdeb6ab0b0b8bc2a87ab52f2b5169c Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 29 Jan 2015 21:31:41 +0100 Subject: further attempts on making the main program work. Had to add a method for handling absolute paths in FileLocator for that. --- src/cli/Main.cpp | 31 +++++++++++++--------- src/plugins/filesystem/FileLocator.cpp | 47 ++++++++++++++++++++++++---------- src/plugins/filesystem/FileLocator.hpp | 9 +++---- 3 files changed, 56 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/cli/Main.cpp b/src/cli/Main.cpp index 92c8caa..2dbeda8 100644 --- a/src/cli/Main.cpp +++ b/src/cli/Main.cpp @@ -136,21 +136,24 @@ int main(int argc, char **argv) } // To comply with standard UNIX conventions the following should be changed: - // TODO: Always write to the working directory (not to the input directory!) // TODO: Allow "-" for input and output files for reading from stdin and // writing to stdout - // TODO: Best use boost::filesystem for path operations + if (inputPath == "-") { + logger.error("Currently no reading from std::in is supported!"); + return ERROR_IN_COMMAND_LINE; + } else{ + inputPath = fs::canonical(inputPath).string(); + } // prepare output path if (!vm.count("output")) { - outputPath = inputPath; - auto pos = outputPath.find_last_of('.'); - if (pos != std::string::npos) { - outputPath.erase(outputPath.begin() + pos + 1, outputPath.end()); - outputPath += format; - std::cout << "Using " << outputPath << " as output path." - << std::endl; - } + // get the input filename. + fs::path in{inputPath}; + // construct a working directory output path. + fs::path outP = fs::canonical("."); + outP /= (in.stem().string() + "." + format); + outputPath = outP.string(); + std::cout << "Using " << outputPath << " as output path." << std::endl; } // TODO: REMOVE diagnostic code. @@ -212,8 +215,12 @@ int main(int argc, char **argv) // write output. html::DemoHTMLTransformer outTransformer; - std::fstream out{outputPath}; - outTransformer.writeHTML(doc.cast(), out); + if (outputPath == "-") { + outTransformer.writeHTML(doc.cast(), std::cout); + } else { + std::fstream out {outputPath}; + outTransformer.writeHTML(doc.cast(), out); + } return SUCCESS; } 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: -- cgit v1.2.3