aboutsummaryrefslogtreecommitdiff
path: root/src/support/filesystem_context.cc
blob: 6ddbf7a30eb4192917b6ad64f0bd2d504d07e316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "filesystem_context.h"

#include "support/xerces_string_guard.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 xalan::Locator* locator):
	path_(boost::filesystem::canonical(
		boost::filesystem::path(
			*XercesStringGuard<char>(locator->getSystemId()) + 7
		).parent_path().string()
	)) { }

FilesystemContext::FilesystemContext(const std::string& path):
	path_(boost::filesystem::canonical(path)) { }

boost::filesystem::path FilesystemContext::resolve(
	const std::string& path) const {
	return absolute(this->path_ / path);
}

boost::filesystem::path FilesystemContext::resolve(
	const xalan::XalanDOMString& path) const {
	return this->resolve(xalanToString(path));
}

void FilesystemContext::iterate(
	const boost::filesystem::path& directory,
	std::function<void(const boost::filesystem::path&)> func
) const {
	if ( 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()) ||
			     boost::filesystem::is_directory(iter->status()) ) {
				func(*iter);
			}
		}
	}
}

void FilesystemContext::iterate(
	const std::string& path,
	std::function<void(const boost::filesystem::path&)> func
) const {
	this->iterate(this->resolve(path), func);
}

void FilesystemContext::iterate(
	const xalan::XalanDOMString& path,
	std::function<void(const boost::filesystem::path&)> func
) const {
	this->iterate(xalanToString(path), func);
}

}