-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code, add throw error function
- Loading branch information
Showing
9 changed files
with
139 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "inja_renderer.h" | ||
|
||
#include <inja/inja.hpp> | ||
#include <stdexcept> | ||
|
||
#include "tools.h" | ||
|
||
using namespace std; | ||
using namespace std::placeholders; | ||
|
||
inja::json split(const inja::Arguments& args) | ||
{ | ||
if (args.size() != 2) | ||
throw runtime_error("Expected 2 parameters: split(s: string, delimiter: string)"); | ||
auto s = args[0]->get<string>(); | ||
auto delimiter = args[1]->get<string>(); | ||
return stringSplit(s, delimiter); | ||
} | ||
|
||
inja::json error(const inja::Arguments& args) | ||
{ | ||
if (args.size() != 1) | ||
throw runtime_error("Expected 1 parameters: error(message: string)"); | ||
auto message = args[0]->get<string>(); | ||
throw runtime_error(message); | ||
} | ||
|
||
string renderWithInja(const string& tmpl, char** envp) | ||
{ | ||
auto envs = getAllEnvs<inja::json>(envp); | ||
inja::Environment env; | ||
env.add_callback("split", bind(split, _1)); | ||
env.add_callback("error", bind(split, _1)); | ||
return env.render(tmpl, envs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
std::string renderWithInja(const std::string& tmpl, char** envp); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "mstch_renderer.h" | ||
|
||
#include <mstch/mstch.hpp> | ||
|
||
#include "tools.h" | ||
|
||
std::string renderWithMstch(const std::string& tmpl, char** envp) | ||
{ | ||
auto envs = getAllEnvs<mstch::map>(envp); | ||
return mstch::render(tmpl, envs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
std::string renderWithMstch(const std::string& tmpl, char** envp); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "tools.h" | ||
|
||
#include <iostream> | ||
#include <iterator> | ||
|
||
using namespace std; | ||
|
||
KeyValue parseKeyValue(const std::string& s) | ||
{ | ||
auto i = s.find("="); | ||
if (i == std::string::npos) | ||
return { .key = s }; | ||
return { | ||
.key = s.substr(0, i), | ||
.value = s.substr(i + 1), | ||
}; | ||
} | ||
|
||
std::string readAllInput() | ||
{ | ||
cin >> noskipws; | ||
istream_iterator<char> it(cin); | ||
istream_iterator<char> end; | ||
return string(it, end); | ||
} | ||
|
||
vector<string> stringSplit(const string& s, const string& delimiter) | ||
{ | ||
vector<string> res; | ||
if (delimiter.empty()) { | ||
res.push_back(s); | ||
return res; | ||
} | ||
string::size_type prevPos = 0, pos = 0; | ||
while ((pos = s.find(delimiter, pos)) != string::npos) { | ||
res.push_back(s.substr(prevPos, pos - prevPos)); | ||
prevPos = ++pos; | ||
} | ||
res.push_back(s.substr(prevPos, pos - prevPos)); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
std::string readAllInput(); | ||
|
||
struct KeyValue { | ||
std::string key; | ||
std::string value; | ||
}; | ||
|
||
KeyValue parseKeyValue(const std::string& s); | ||
|
||
template <typename Map> | ||
Map getAllEnvs(char** envp) | ||
{ | ||
Map m; | ||
for (char** env = envp; *env != 0; env++) { | ||
auto kv = parseKeyValue(*env); | ||
m[kv.key] = kv.value; | ||
} | ||
return m; | ||
} | ||
|
||
std::vector<std::string> stringSplit(const std::string& s, const std::string& delimiter); |