From 7142544d43b431df44d34921b0f3012fa1e0137d Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Mon, 21 Apr 2014 21:47:20 +0200 Subject: Implemented basic external directory traversal function * _read-directory_ lists all files in a given directory ** currently text-only output, xml planned * improved FilesystemContext path resolution (relative path is fully resolved by boost::filesystem) --- src/support/filesystem_context.cc | 49 ++++++++++++++++++++++++++++++++------- src/support/filesystem_context.h | 6 +++++ 2 files changed, 47 insertions(+), 8 deletions(-) (limited to 'src/support') diff --git a/src/support/filesystem_context.cc b/src/support/filesystem_context.cc index d3f9614..d0813c2 100644 --- a/src/support/filesystem_context.cc +++ b/src/support/filesystem_context.cc @@ -1,24 +1,57 @@ #include "filesystem_context.h" +namespace { + +inline std::string xalanToString(const xalan::XalanDOMString& text) { + xalan::CharVectorType castHelper; + text.transcode(castHelper); + + return std::string( + castHelper.begin(), + castHelper.end() - 1 + ); +} + +} + namespace InputXSLT { FilesystemContext::FilesystemContext(const std::string& path): - path_(path) { } + path_(canonical(boost::filesystem::path(path))) { } boost::filesystem::path FilesystemContext::resolve( const std::string& path) const { - return boost::filesystem::path(this->path_ / path); + return canonical(this->path_ / path); } boost::filesystem::path FilesystemContext::resolve( const xalan::XalanDOMString& path) const { - xalan::CharVectorType castHelper; - path.transcode(castHelper); + return this->resolve(xalanToString(path)); +} - return this->resolve(std::string( - castHelper.begin(), - castHelper.end() - )); +void FilesystemContext::iterate( + const std::string& path, + std::function func +) const { + const boost::filesystem::path directory(this->resolve(path)); + + if ( boost::filesystem::exists(directory) && + boost::filesystem::is_directory(directory) ) { + for ( boost::filesystem::directory_iterator iter(directory); + iter != boost::filesystem::directory_iterator(); + ++iter ) { + if ( boost::filesystem::is_regular_file(iter->status()) ) { + func(*iter); + } + } + } +} + +void FilesystemContext::iterate( + const xalan::XalanDOMString& path, + std::function func +) const { + this->iterate(xalanToString(path), func); } } diff --git a/src/support/filesystem_context.h b/src/support/filesystem_context.h index d5836f1..cb3edd4 100644 --- a/src/support/filesystem_context.h +++ b/src/support/filesystem_context.h @@ -6,6 +6,7 @@ #include "boost/filesystem.hpp" #include +#include #include "common.h" @@ -18,6 +19,11 @@ class FilesystemContext { boost::filesystem::path resolve(const std::string&) const; boost::filesystem::path resolve(const xalan::XalanDOMString&) const; + void iterate(const std::string&, + std::function) const; + void iterate(const xalan::XalanDOMString&, + std::function) const; + private: const boost::filesystem::path path_; -- cgit v1.2.3