aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MemDriverWeb/MemDriverWeb.cpp15
-rw-r--r--MemDriverWeb/MemDriverWeb.vcxproj1
-rw-r--r--MemDriverWeb/MemDriverWeb.vcxproj.filters3
-rw-r--r--MemDriverWeb/minitmpl.cpp19
-rw-r--r--MemDriverWeb/minitmpl.h17
-rw-r--r--MemDriverWeb/pch.h1
-rw-r--r--MemDriverWeb/www.h7
7 files changed, 47 insertions, 16 deletions
diff --git a/MemDriverWeb/MemDriverWeb.cpp b/MemDriverWeb/MemDriverWeb.cpp
index cf0e07e..5e9dfc5 100644
--- a/MemDriverWeb/MemDriverWeb.cpp
+++ b/MemDriverWeb/MemDriverWeb.cpp
@@ -9,21 +9,28 @@ using httplib::Response;
static const char *host = "127.0.0.1";
static const int port = 8080;
-static const std::string header = DEFAULT_HEADER;
-static const std::string footer = DEFAULT_FOOTER;
+static const std::string template_content = DEFAULT_TEMPLATE;
static void page_root(const Request &req, Response &res)
{
TemplateString ss;
- ss << header << footer;
- res.set_content(ss.str(), "text/html");
+ ss << template_content;
+ res.set_content(ss.doTemplateStr(), "text/html");
+}
+
+static std::string& template_test_cb(std::string &out)
+{
+ out.append("--- TEST ---");
+ return out;
}
int main()
{
httplib::Server httpServer;
+ TemplateString::registerTemplateCallback("<% CONTENT %>", template_test_cb);
+
std::cout << "Starting WebServer on " << host << ":" << port << "\n";
httpServer.Get("/", page_root);
diff --git a/MemDriverWeb/MemDriverWeb.vcxproj b/MemDriverWeb/MemDriverWeb.vcxproj
index 00d5a8b..a545fd4 100644
--- a/MemDriverWeb/MemDriverWeb.vcxproj
+++ b/MemDriverWeb/MemDriverWeb.vcxproj
@@ -162,6 +162,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="MemDriverWeb.cpp" />
+ <ClCompile Include="minitmpl.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
diff --git a/MemDriverWeb/MemDriverWeb.vcxproj.filters b/MemDriverWeb/MemDriverWeb.vcxproj.filters
index 5874c87..040e7bd 100644
--- a/MemDriverWeb/MemDriverWeb.vcxproj.filters
+++ b/MemDriverWeb/MemDriverWeb.vcxproj.filters
@@ -35,5 +35,8 @@
<ClCompile Include="MemDriverWeb.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="minitmpl.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
</Project> \ No newline at end of file
diff --git a/MemDriverWeb/minitmpl.cpp b/MemDriverWeb/minitmpl.cpp
new file mode 100644
index 0000000..3d7c34c
--- /dev/null
+++ b/MemDriverWeb/minitmpl.cpp
@@ -0,0 +1,19 @@
+#include "pch.h"
+#include "minitmpl.h"
+
+std::map<std::string, template_cb> TemplateString::template_callbacks;
+
+std::string TemplateString::doTemplateStr() {
+ size_t pos = 0;
+ in_cache = str();
+
+ out_cache.clear();
+ for (auto& key : template_callbacks) {
+ while ((pos = in_cache.find(key.first, pos)) != std::string::npos) {
+ in_cache.replace(pos, key.first.length(), key.second(out_cache));
+ pos += out_cache.length();
+ }
+ }
+
+ return in_cache;
+} \ No newline at end of file
diff --git a/MemDriverWeb/minitmpl.h b/MemDriverWeb/minitmpl.h
index 446930a..426579c 100644
--- a/MemDriverWeb/minitmpl.h
+++ b/MemDriverWeb/minitmpl.h
@@ -1,17 +1,18 @@
#pragma once
+#include <map>
#include <sstream>
+typedef std::function<std::string&(std::string&)> template_cb;
+
class TemplateString : public std::stringstream
{
public:
- std::string& doTemplateStr(const std::string& search, const std::string& replace) {
- size_t pos = 0;
- std::string s = this->str();
- while ((pos = s.find(search, pos)) != std::string::npos) {
- s.replace(pos, search.length(), replace);
- pos += replace.length();
- }
- return s;
+ static void registerTemplateCallback(const char *variable, template_cb cb) {
+ TemplateString::template_callbacks[std::string(variable)] = cb;
}
+ std::string doTemplateStr();
+private:
+ std::string in_cache, out_cache;
+ static std::map<std::string, template_cb> template_callbacks;
}; \ No newline at end of file
diff --git a/MemDriverWeb/pch.h b/MemDriverWeb/pch.h
index a4977f6..314f3f7 100644
--- a/MemDriverWeb/pch.h
+++ b/MemDriverWeb/pch.h
@@ -2,5 +2,6 @@
#define PCH_H
#include "httplib.h"
+#include "minitmpl.h"
#endif //PCH_H
diff --git a/MemDriverWeb/www.h b/MemDriverWeb/www.h
index 79c1116..48d2fbb 100644
--- a/MemDriverWeb/www.h
+++ b/MemDriverWeb/www.h
@@ -1,6 +1,6 @@
#pragma once
-#define DEFAULT_HEADER \
+#define DEFAULT_TEMPLATE \
"<!DOCTYPE html>\n<html lang = \"en\">\n<head>\n" \
"<title>Bootstrap Example</title>\n" \
"<meta charset = \"utf-8\"><meta name = \"viewport\" content = \"width=device-width, initial-scale=1\">\n" \
@@ -17,7 +17,6 @@
"</li>\n<li class = \"nav-item\">\n" \
"<a class = \"nav-link\" href = \"#\">Link 3 </a>\n" \
"</li>\n</ul>\n</nav>\n<br>\n\n" \
-"<div class = \"container-fluid\">\n\n";
-
-#define DEFAULT_FOOTER \
+"<div class = \"container-fluid\">\n\n" \
+"<% CONTENT %>\n\n" \
"</div>\n\n</body>\n</html>\n\n"; \ No newline at end of file