summaryrefslogtreecommitdiff
path: root/tools/jsonproc/jsonproc.cpp
diff options
context:
space:
mode:
authorDiegoisawesome <Diegoisawesome@users.noreply.github.com>2019-08-15 16:42:37 -0500
committerGitHub <noreply@github.com>2019-08-15 16:42:37 -0500
commite751d33afb9410d63fc7b5c8da211a8d62eaeb21 (patch)
tree016da807f9f85a9dad91c1b091295d5c68375701 /tools/jsonproc/jsonproc.cpp
parenta5d8669840e5cb411f03929118608cb79ee026ad (diff)
parentf3bd844f5622fad017d1752734ab1e128006ea7b (diff)
Merge pull request #16 from PikalaxALT/master
Update binaries again; add build and install scripts
Diffstat (limited to 'tools/jsonproc/jsonproc.cpp')
-rw-r--r--tools/jsonproc/jsonproc.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/tools/jsonproc/jsonproc.cpp b/tools/jsonproc/jsonproc.cpp
new file mode 100644
index 0000000..efe48f3
--- /dev/null
+++ b/tools/jsonproc/jsonproc.cpp
@@ -0,0 +1,91 @@
+// jsonproc.cpp
+
+#include "jsonproc.h"
+
+#include <map>
+
+#include <string>
+using std::string;
+
+#include <inja.hpp>
+using namespace inja;
+using json = nlohmann::json;
+
+std::map<string, string> customVars;
+
+void set_custom_var(string key, string value)
+{
+ customVars[key] = value;
+}
+
+string get_custom_var(string key)
+{
+ return customVars[key];
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc != 4)
+ FATAL_ERROR("USAGE: jsonproc <json-filepath> <template-filepath> <output-filepath>\n");
+
+ string jsonfilepath = argv[1];
+ string templateFilepath = argv[2];
+ string outputFilepath = argv[3];
+
+ Environment env;
+
+ // Add custom command callbacks.
+ env.add_callback("doNotModifyHeader", 0, [jsonfilepath, templateFilepath](Arguments& args) {
+ return "//\n// DO NOT MODIFY THIS FILE! IT IS AUTO-GENERATED FROM " + jsonfilepath +" and Inja template " + templateFilepath + "\n//\n";
+ });
+
+ env.add_callback("setVar", 2, [=](Arguments& args) {
+ string key = args.at(0)->get<string>();
+ string value = args.at(1)->get<string>();
+ set_custom_var(key, value);
+ return "";
+ });
+
+ env.add_callback("getVar", 1, [=](Arguments& args) {
+ string key = args.at(0)->get<string>();
+ return get_custom_var(key);
+ });
+
+ env.add_callback("concat", 2, [](Arguments& args) {
+ string first = args.at(0)->get<string>();
+ string second = args.at(1)->get<string>();
+ return first + second;
+ });
+
+ env.add_callback("removePrefix", 2, [](Arguments& args) {
+ string rawValue = args.at(0)->get<string>();
+ string prefix = args.at(1)->get<string>();
+ string::size_type i = rawValue.find(prefix);
+ if (i != 0)
+ return rawValue;
+
+ return rawValue.erase(0, prefix.length());
+ });
+
+ // Add custom command callbacks.
+ env.add_callback("removeSuffix", 2, [](Arguments& args) {
+ string rawValue = args.at(0)->get<string>();
+ string suffix = args.at(1)->get<string>();
+ string::size_type i = rawValue.rfind(suffix);
+ if (i == string::npos)
+ return rawValue;
+
+ return rawValue.substr(0, i);
+ });
+
+ try
+ {
+ env.write_with_json_file(templateFilepath, jsonfilepath, outputFilepath);
+ }
+ catch (const std::exception& e)
+ {
+ FATAL_ERROR("JSONPROC_ERROR: %s\n", e.what());
+ }
+
+ return 0;
+}