|
| 1 | +#include <jinja2cpp/filesystem_handler.h> |
| 2 | + |
| 3 | +#include <boost/filesystem/path.hpp> |
| 4 | + |
| 5 | +#include <sstream> |
| 6 | +#include <fstream> |
| 7 | + |
| 8 | +namespace jinja2 |
| 9 | +{ |
| 10 | + |
| 11 | +using TargetFileStream = boost::variant<CharFileStreamPtr*, WCharFileStreamPtr*>; |
| 12 | + |
| 13 | +struct FileContentConverter : public boost::static_visitor<void> |
| 14 | +{ |
| 15 | + void operator() (const std::string& content, CharFileStreamPtr* sPtr) const |
| 16 | + { |
| 17 | + sPtr->reset(new std::istringstream(content)); |
| 18 | + } |
| 19 | + |
| 20 | + void operator() (const std::wstring& content, WCharFileStreamPtr* sPtr) const |
| 21 | + { |
| 22 | + sPtr->reset(new std::wistringstream(content)); |
| 23 | + } |
| 24 | + void operator() (const std::wstring& content, CharFileStreamPtr* sPtr) const |
| 25 | + { |
| 26 | +// CharFileStreamPtr stream(new std::istringstream(content), [](std::istream* s) {delete static_cast<std::istringstream>(s);}); |
| 27 | +// std::swap(*sPtr, stream); |
| 28 | + } |
| 29 | + |
| 30 | + void operator() (const std::string& content, WCharFileStreamPtr* sPtr) const |
| 31 | + { |
| 32 | +// WCharFileStreamPtr stream(new std::wistringstream(content), [](std::wistream* s) {delete static_cast<std::wistringstream>(s);}); |
| 33 | +// std::swap(*sPtr, stream); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +void MemoryFileSystem::AddFile(std::string fileName, std::string fileContent) |
| 38 | +{ |
| 39 | + m_filesMap[std::move(fileName)] = FileContent(std::move(fileContent)); |
| 40 | +} |
| 41 | + |
| 42 | +void MemoryFileSystem::AddFile(std::string fileName, std::wstring fileContent) |
| 43 | +{ |
| 44 | + m_filesMap[std::move(fileName)] = FileContent(std::move(fileContent)); |
| 45 | +} |
| 46 | + |
| 47 | +CharFileStreamPtr MemoryFileSystem::OpenStream(const std::string& name) const |
| 48 | +{ |
| 49 | + CharFileStreamPtr result(nullptr, [](std::istream* s) {delete static_cast<std::istringstream*>(s);}); |
| 50 | + auto p = m_filesMap.find(name); |
| 51 | + if (p == m_filesMap.end()) |
| 52 | + return result; |
| 53 | + |
| 54 | + TargetFileStream targetStream(&result); |
| 55 | + boost::apply_visitor(FileContentConverter(), p->second, targetStream); |
| 56 | + |
| 57 | + return result; |
| 58 | +} |
| 59 | + |
| 60 | +WCharFileStreamPtr MemoryFileSystem::OpenWStream(const std::string& name) const |
| 61 | +{ |
| 62 | + WCharFileStreamPtr result(nullptr, [](std::wistream* s) {delete static_cast<std::wistringstream*>(s);}); |
| 63 | + auto p = m_filesMap.find(name); |
| 64 | + if (p == m_filesMap.end()) |
| 65 | + return result; |
| 66 | + |
| 67 | + TargetFileStream targetStream(&result); |
| 68 | + boost::apply_visitor(FileContentConverter(), p->second, targetStream); |
| 69 | + |
| 70 | + return result; |
| 71 | +} |
| 72 | + |
| 73 | +RealFileSystem::RealFileSystem(std::string rootFolder) |
| 74 | + : m_rootFolder(rootFolder) |
| 75 | +{ |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +CharFileStreamPtr RealFileSystem::OpenStream(const std::string& name) const |
| 80 | +{ |
| 81 | + boost::filesystem::path root(m_rootFolder); |
| 82 | + root /= name; |
| 83 | + const auto& filePath = root.native(); |
| 84 | + |
| 85 | + CharFileStreamPtr result(new std::ifstream(filePath), [](std::istream* s) {delete static_cast<std::ifstream*>(s);}); |
| 86 | + if (result->good()) |
| 87 | + return result; |
| 88 | + |
| 89 | + return CharFileStreamPtr(nullptr, [](std::istream* s){}); |
| 90 | +} |
| 91 | + |
| 92 | +WCharFileStreamPtr RealFileSystem::OpenWStream(const std::string& name) const |
| 93 | +{ |
| 94 | + boost::filesystem::path root(m_rootFolder); |
| 95 | + root /= name; |
| 96 | + const auto& filePath = root.native(); |
| 97 | + |
| 98 | + WCharFileStreamPtr result(new std::wifstream(filePath), [](std::wistream* s) {delete static_cast<std::wifstream*>(s);}); |
| 99 | + if (result->good()) |
| 100 | + return result; |
| 101 | + |
| 102 | + return WCharFileStreamPtr(nullptr, [](std::wistream* s){;}); |
| 103 | +} |
| 104 | + |
| 105 | +} // jinja2 |
0 commit comments