Skip to content

Commit 5a9e13b

Browse files
fixed exception type, added conversion app
1 parent 4143a7d commit 5a9e13b

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ project(JSE
3434
option(JSE_WITH_SANITIZERS "Enable sanitizers in compilation targets" OFF)
3535
# Misc.
3636
option(JSE_WITH_TESTS "Build unit-tests" ${JSE_TOPLEVEL_PROJECT})
37+
option(JSE_WITH_APP "Compiles applications" ${JSE_TOPLEVEL_PROJECT})
3738

3839
include(CMakeDependentOption)
3940

@@ -121,3 +122,18 @@ if(JSE_WITH_TESTS)
121122

122123
add_subdirectory(tests)
123124
endif()
125+
126+
################################################################################
127+
# Applications
128+
################################################################################
129+
130+
if(JSE_WITH_APP)
131+
132+
add_executable(jse_app src/main.cpp)
133+
134+
target_compile_options(jse_app PRIVATE "-rdynamic")
135+
target_link_libraries(jse_app PUBLIC
136+
jse::jse
137+
)
138+
139+
endif()

data/rules_04.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"pointer": "/",
4+
"type": "include",
5+
"spec_file": "rules_02.json"
6+
},
7+
{
8+
"pointer": "/object1",
9+
"type": "include",
10+
"spec_file": "rules_02.json"
11+
}
12+
]

src/jse/jse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ namespace jse
138138
if (!replaced)
139139
{
140140
string pointer = rule.at("pointer");
141-
throw("Failed to replace the include rule: " + pointer);
141+
throw std::runtime_error("Failed to replace the include rule: " + pointer);
142142
assert(replaced == true);
143143
}
144144
}
@@ -148,7 +148,7 @@ namespace jse
148148
current = enriched;
149149
}
150150

151-
throw("Reached maximal 10 levels of include recursion.");
151+
throw std::runtime_error("Reached maximal 10 levels of include recursion.");
152152

153153
}
154154

src/main.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
#include <jse/jse.h>
4+
#include <string>
5+
#include <vector>
6+
#include <fstream>
7+
8+
9+
using namespace std;
10+
using namespace jse;
11+
12+
int main(int argc, char *argv[]) {
13+
try
14+
{
15+
16+
if (argc < 3) {
17+
cerr << "This utility recursively replaces the include rules with their corresponding rule files." << endl;
18+
cerr << "Usage: " << argv[0] << " input.json output.json folder1 folder2 ..." << endl;
19+
return EXIT_FAILURE;
20+
}
21+
22+
// First parameter is the input json
23+
string sinput = argv[1];
24+
25+
// Second parameter is the output json
26+
string soutput = argv[2];
27+
28+
// Other parameters are optional include directories
29+
// where the parser will look for rule files
30+
vector<std::string> arg;
31+
for (size_t i=3;i<argc;++i)
32+
arg.push_back(string(argv[i]));
33+
34+
// Initialize JSE
35+
JSE jse;
36+
jse.include_directories = arg;
37+
38+
// Load the input json
39+
std::ifstream input_f(sinput);
40+
json rules = json::parse(input_f);
41+
42+
// Try to replace the includes, might throw exceptions
43+
json new_rules = jse.inject_include(rules);
44+
45+
// Write the new rule file
46+
std::ofstream file(soutput);
47+
file << new_rules;
48+
49+
return EXIT_SUCCESS;
50+
}
51+
catch(const std::exception &exc)
52+
{
53+
std::cout << exc.what() << std::endl;
54+
return EXIT_FAILURE;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)