From 2c358ced7ca10ab37a382477d8b55fdb4e353f44 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Thu, 22 May 2014 21:07:16 +0200 Subject: Replaced std::pair with the more convenient boost::optional<*> * InputXSLT is dependent on boost anyway, so there is no argument to be made to emulate boost::optional using std::pair --- src/function/read_file.cc | 13 ++++--------- src/function/read_xml_file.cc | 13 ++++--------- src/support/include_entity_resolver.cc | 32 +++++++++++++++----------------- src/support/include_entity_resolver.h | 4 +++- 4 files changed, 26 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 85f834d..1a870a6 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -28,18 +28,13 @@ xercesc::DOMDocument* FunctionReadFile::constructDocument( const FilesystemContext& fsContext, const FunctionBase::parameter_tuple& parameters ) { - boost::filesystem::path filePath( - fsContext.resolve(std::get<0>(parameters)) - ); + const std::string& rawPath = std::get<0>(parameters); + boost::filesystem::path filePath(fsContext.resolve(rawPath)); if ( !(boost::filesystem::exists(filePath) && boost::filesystem::is_regular_file(filePath)) ) { - auto resolvedPath = this->include_resolver_->resolve( - std::get<0>(parameters) - ); - - if ( resolvedPath.first ) { - filePath = resolvedPath.second; + if ( auto resolvedPath = this->include_resolver_->resolve(rawPath) ) { + filePath = *resolvedPath; } } diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index b9246e6..4eb44e5 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -38,18 +38,13 @@ xercesc::DOMDocument* FunctionReadXmlFile::constructDocument( const FilesystemContext& fsContext, const FunctionBase::parameter_tuple& parameters ) { - boost::filesystem::path filePath( - fsContext.resolve(std::get<0>(parameters)) - ); + const std::string& rawPath = std::get<0>(parameters); + boost::filesystem::path filePath(fsContext.resolve(rawPath)); if ( !(boost::filesystem::exists(filePath) && boost::filesystem::is_regular_file(filePath)) ) { - auto resolvedPath = this->include_resolver_->resolve( - std::get<0>(parameters) - ); - - if ( resolvedPath.first ) { - filePath = resolvedPath.second; + if ( auto resolvedPath = this->include_resolver_->resolve(rawPath) ) { + filePath = *resolvedPath; } } diff --git a/src/support/include_entity_resolver.cc b/src/support/include_entity_resolver.cc index a00d9e6..68af11a 100644 --- a/src/support/include_entity_resolver.cc +++ b/src/support/include_entity_resolver.cc @@ -8,22 +8,24 @@ namespace { -std::pair extractFilePath(const std::string& rawPath) { - const std::size_t leadingDelimiter = rawPath.find_first_of('['); - const std::size_t closingDelimiter = rawPath.find_last_of(']'); +using InputXSLT::XercesStringGuard; + +boost::optional extractFilePath(const XMLCh* const rawPath) { + const std::string filePath = *XercesStringGuard(rawPath); + const std::size_t leadingDelimiter = filePath.find_first_of('['); + const std::size_t closingDelimiter = filePath.find_last_of(']'); if ( leadingDelimiter != std::string::npos && closingDelimiter != std::string::npos && leadingDelimiter < closingDelimiter ) { - return std::make_pair( - true, - rawPath.substr( + return boost::make_optional( + filePath.substr( leadingDelimiter + 1, closingDelimiter - leadingDelimiter - 1 ) ); } else { - return std::make_pair(false, std::string()); + return boost::optional(); } } @@ -51,14 +53,10 @@ xercesc::InputSource* IncludeEntityResolver::resolveEntity( const XMLCh* const systemId ) { if ( systemId != nullptr ) { - auto filePath = extractFilePath(*XercesStringGuard(systemId)); - - if ( filePath.first ) { - auto resolvedPath = this->resolve(filePath.second); - - if ( resolvedPath.first ) { + if ( auto filePath = extractFilePath(systemId) ) { + if ( auto resolvedPath = this->resolve(*filePath) ) { return new xercesc::LocalFileInputSource( - *XercesStringGuard(resolvedPath.second.string()) + *XercesStringGuard((*resolvedPath).string()) ); } else { return nullptr; @@ -71,7 +69,7 @@ xercesc::InputSource* IncludeEntityResolver::resolveEntity( } } -std::pair IncludeEntityResolver::resolve( +boost::optional IncludeEntityResolver::resolve( const std::string& filePath) { for ( auto&& context : this->path_ ) { const boost::filesystem::path resolvedPath( @@ -80,11 +78,11 @@ std::pair IncludeEntityResolver::resolve( if ( boost::filesystem::exists(resolvedPath) && boost::filesystem::is_regular_file(resolvedPath) ) { - return std::make_pair(true, resolvedPath); + return boost::make_optional(resolvedPath); } } - return std::make_pair(false, boost::filesystem::path()); + return boost::optional(); } } diff --git a/src/support/include_entity_resolver.h b/src/support/include_entity_resolver.h index b87e10f..1a3bbc1 100644 --- a/src/support/include_entity_resolver.h +++ b/src/support/include_entity_resolver.h @@ -4,6 +4,8 @@ #include #include +#include "boost/optional.hpp" + #include #include @@ -20,7 +22,7 @@ class IncludeEntityResolver : public xercesc::EntityResolver { const XMLCh* const ); - std::pair resolve(const std::string&); + boost::optional resolve(const std::string&); private: std::vector path_; -- cgit v1.2.3