Create jsonproc tool
Add custom function hooks to jsonproc Add jsonproc to build_tools.sh Newer g++
This commit is contained in:
Executable
+1
@@ -0,0 +1 @@
|
||||
jsonproc
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
CXX := g++
|
||||
|
||||
CXXFLAGS := -Wall -std=c++11 -O2
|
||||
|
||||
INCLUDES := -I .
|
||||
|
||||
SRCS := jsonproc.cpp
|
||||
|
||||
HEADERS := jsonproc.h inja.hpp nlohmann/json.hpp
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
jsonproc: $(SRCS) $(HEADERS)
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) $(SRCS) -o $@ $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
$(RM) jsonproc jsonproc.exe
|
||||
Executable
+3396
File diff suppressed because it is too large
Load Diff
Executable
+91
@@ -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;
|
||||
}
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
// jsonproc.h
|
||||
|
||||
#ifndef JSONPROC_H
|
||||
#define JSONPROC_H
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
using std::fprintf; using std::exit;
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
#define FATAL_ERROR(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
fprintf(stderr, format, __VA_ARGS__); \
|
||||
exit(1); \
|
||||
} while (0)
|
||||
|
||||
#else
|
||||
|
||||
#define FATAL_ERROR(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
fprintf(stderr, format, ##__VA_ARGS__); \
|
||||
exit(1); \
|
||||
} while (0)
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
#endif // JSONPROC_H
|
||||
Executable
+20842
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user