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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#include <jinja2cpp/filesystem_handler.h>
#include <jinja2cpp/string_helpers.h>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <sstream>
#include <fstream>
namespace jinja2
{
using TargetFileStream = std::variant<CharFileStreamPtr*, WCharFileStreamPtr*>;
struct FileContentConverter
{
void operator() (const std::string& content, CharFileStreamPtr* sPtr) const
{
sPtr->reset(new std::istringstream(content));
}
void operator() (const std::wstring& content, WCharFileStreamPtr* sPtr) const
{
sPtr->reset(new std::wistringstream(content));
}
void operator() (const std::wstring&, CharFileStreamPtr*) const
{
// CharFileStreamPtr stream(new std::istringstream(content), [](std::istream* s) {delete static_cast<std::istringstream>(s);});
// std::swap(*sPtr, stream);
}
void operator() (const std::string&, WCharFileStreamPtr*) const
{
// WCharFileStreamPtr stream(new std::wistringstream(content), [](std::wistream* s) {delete static_cast<std::wistringstream>(s);});
// std::swap(*sPtr, stream);
}
};
void MemoryFileSystem::AddFile(std::string fileName, std::string fileContent)
{
m_filesMap[std::move(fileName)] = FileContent{std::move(fileContent), {}};
}
void MemoryFileSystem::AddFile(std::string fileName, std::wstring fileContent)
{
m_filesMap[std::move(fileName)] = FileContent{ {}, std::move(fileContent) };
}
CharFileStreamPtr MemoryFileSystem::OpenStream(const std::string& name) const
{
CharFileStreamPtr result(nullptr, [](std::istream* s) {delete static_cast<std::istringstream*>(s);});
auto p = m_filesMap.find(name);
if (p == m_filesMap.end())
return result;
auto& content = p->second;
if (!content.narrowContent && !content.wideContent)
return result;
if (!content.narrowContent)
content.narrowContent = ConvertString<std::string>(content.wideContent.value());
result.reset(new std::istringstream(content.narrowContent.value()));
return result;
}
WCharFileStreamPtr MemoryFileSystem::OpenWStream(const std::string& name) const
{
WCharFileStreamPtr result(nullptr, [](std::wistream* s) {delete static_cast<std::wistringstream*>(s);});
auto p = m_filesMap.find(name);
if (p == m_filesMap.end())
return result;
auto& content = p->second;
if (!content.narrowContent && !content.wideContent)
return result;
if (!content.wideContent)
content.wideContent = ConvertString<std::wstring>(content.narrowContent.value());
result.reset(new std::wistringstream(content.wideContent.value()));
return result;
}
std::optional<std::chrono::system_clock::time_point> MemoryFileSystem::GetLastModificationDate(const std::string&) const
{
return std::optional<std::chrono::system_clock::time_point>();
}
bool MemoryFileSystem::IsEqual(const IComparable& other) const
{
auto* ptr = dynamic_cast<const MemoryFileSystem*>(&other);
if (!ptr)
return false;
return m_filesMap == ptr->m_filesMap;
}
RealFileSystem::RealFileSystem(std::string rootFolder)
: m_rootFolder(std::move(rootFolder))
{
}
std::string RealFileSystem::GetFullFilePath(const std::string& name) const
{
boost::filesystem::path root(m_rootFolder);
root /= name;
return root.string();
}
CharFileStreamPtr RealFileSystem::OpenStream(const std::string& name) const
{
auto filePath = GetFullFilePath(name);
CharFileStreamPtr result(new std::ifstream(filePath), [](std::istream* s) {delete static_cast<std::ifstream*>(s);});
if (result->good())
return result;
return CharFileStreamPtr(nullptr, [](std::istream*){});
}
WCharFileStreamPtr RealFileSystem::OpenWStream(const std::string& name) const
{
auto filePath = GetFullFilePath(name);
WCharFileStreamPtr result(new std::wifstream(filePath), [](std::wistream* s) {delete static_cast<std::wifstream*>(s);});
if (result->good())
return result;
return WCharFileStreamPtr(nullptr, [](std::wistream*){;});
}
std::optional<std::chrono::system_clock::time_point> RealFileSystem::GetLastModificationDate(const std::string& name) const
{
boost::filesystem::path root(m_rootFolder);
root /= name;
auto modify_time = boost::filesystem::last_write_time(root);
return std::chrono::system_clock::from_time_t(modify_time);
}
CharFileStreamPtr RealFileSystem::OpenByteStream(const std::string& name) const
{
auto filePath = GetFullFilePath(name);
CharFileStreamPtr result(new std::ifstream(filePath, std::ios_base::binary), [](std::istream* s) {delete static_cast<std::ifstream*>(s);});
if (result->good())
return result;
return CharFileStreamPtr(nullptr, [](std::istream*){});
}
bool RealFileSystem::IsEqual(const IComparable& other) const
{
auto* ptr = dynamic_cast<const RealFileSystem*>(&other);
if (!ptr)
return false;
return m_rootFolder == ptr->m_rootFolder;
}
} // namespace jinja2
|