aboutsummaryrefslogtreecommitdiff
path: root/src/function/read_directory.cc
blob: 9dfc21190d8c4d2e607b226af76eb1e3cd045398 (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
#include "read_directory.h"

#include "support/xerces_string_guard.h"
#include "support/dom/result_node_facade.h"

namespace InputXSLT {

DomDocumentCache::document_ptr FunctionReadDirectory::constructDocument(
	const FilesystemContext&,
	boost::filesystem::path  directoryPath
) const {
	DomDocumentCache::document_ptr domDocument{
		DomDocumentCache::createDocument("content")
	};

	ResultNodeFacade result{domDocument.get(), "directory"};
	result.setAttribute("path", directoryPath.string());

	if ( boost::filesystem::is_directory(directoryPath) ) {
		result.setAttribute("result", "success");

		xercesc::DOMNode* const resultNode = result.getResultElement();

		FilesystemContext::iterate(
			directoryPath,
			[&domDocument, &resultNode](const boost::filesystem::path& p) {
			ResultNodeFacade result(domDocument.get(), resultNode, "entry");

			switch ( boost::filesystem::status(p).type() ) {
				case boost::filesystem::regular_file: {
					result.setAttribute("type",      "file");
					result.setValueNode("name",      p.stem().string());
					result.setValueNode("extension", p.extension().string());

					break;
				};
				case boost::filesystem::directory_file: {
					result.setAttribute("type", "directory");
					result.setValueNode("name", p.filename().string());

					break;
				};
				default: {
					result.setAttribute("type", "misc");
					result.setValueNode("name", p.filename().string());

					break;
				};
			}

			result.setValueNode("full", boost::filesystem::canonical(p).string());
		});
	} else {
		result.setAttribute("result", "error");
	}

	return domDocument;
}

}