-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
278 lines (230 loc) · 8.66 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream> // for std::cout
#include <fstream> // for std::ofstream
#include <cxxopts.hpp>
#include <yaml-cpp/yaml.h>
#include "yaml_transform/transform_nodes.hpp"
#include "yaml_transform/transform_with_regexp.hpp"
#include "utils/vector_make_unique.hpp"
#include "utils/failure_mesage.hpp"
/*
via: https://stackoverflow.com/a/12586201/
alternatives: https://www.google.com/search?q=regex+between+curly+brackets
Test cases:
- Input: {text1}, {{text2}}, {{ text3 }}, {{ \} }}, {{ text4 \}\} }}
- Ouput: "text2", " text3 ", " \} ", " text \}\} "
*/
static const RegexpWithPattern RGX_MOUSTACHE(R"(\{\{(.*?)\}\})");
static const RegexpWithPattern RGX_DOUBLE_DOLLAR(R"(\$(\S+)\$)");
static const RegexpWithPattern RGX_DOUBLE_PERCENT(R"(\%(\S+)\%)");
int main(int argc, const char* argv[])
{
std::string input_file_path;
std::string output_file_path = "stdout";
std::string separator = ".";
std::vector<std::string> formats;
std::vector<std::string> regexps;
// -------------------------------------------------------------------------
// Parse command-line options
try
{
cxxopts::Options options(argv[0], "");
// Options output settings
options
.show_positional_help()
.set_width(80)
.set_tab_expansion();
options.add_options("I/O")
("i,input",
"Input yaml file path (or 'stdin'). (required)",
cxxopts::value<std::string>(),
"PATH")
("o,output",
"Output file name (or 'stdout')",
cxxopts::value<std::string>()
->default_value(output_file_path),
"PATH"
);
options.add_options("Parsing settings")
("formats",
"Formats:\n"
"moustache: {{ variable.path }}\n"
"double_dollar: $variable.path$\n"
"double_percent: %variable.path%\n",
cxxopts::value<std::vector<std::string>>()
->default_value("moustache"),
"LIST"
)
("separator",
"Separator between node names",
cxxopts::value<std::string>()->default_value(separator),
"STRING"
)
("regexps",
"Regular expressions to find pattern\n"
"Notice, that them must contain single capture group",
cxxopts::value< std::vector<std::string> >(),
"LIST"
);
options.add_options("Help")
("help", "Print this help");
const auto result = options.parse(argc, argv);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(result.count("help"))
{
std::puts( options.help({"I/O", "Parsing settings", "Help"}).c_str() );
exit(0);
}
if(result.count("input")) // <-- Required
{
input_file_path = result["input"].as<std::string>();
}
else // No input file path specified
{
failure_message(1, "%s\n", "<!> No input file path specified!");
}
if(result.count("output"))
{
output_file_path = result["output"].as<std::string>();
}
if(result.count("formats"))
{
formats = result["formats"].as<std::vector<std::string>>();
utils::make_unique(formats);
}
else
{
/*
This branch is needed, since, unexpectedly the next code:
cxxopts::value<std::vector<std::string>>()
->default_value("moustache"),
not generates at least single-item array of "moustache", in case
when user not specify this option. So we simply, assign default
value manually.
Well... Looking for cxxopts test, I found, that in case of
'default_value' specified, we dont need to check it by calling
'count()', but simply get 'result["option"]' directly. But this
is strange, so here is check-and-manual-set.
*/
formats = {"moustache"};
}
if(result.count("separator"))
{
separator = result["separator"].as<std::string>();
}
if(result.count("regexps"))
{
regexps = result["regexps"].as<std::vector<std::string>>();
utils::make_unique(regexps);
}
}
catch (const cxxopts::OptionException& e)
{
failure_message(1, "<!> Error parsing options: %s\n", e.what());
}
// -------------------------------------------------------------------------
// Making transform functions
std::map<std::string, transform_func_t> transforms;
for(const std::string& format : formats)
{
transform_func_t func = nullptr;
if(format == "moustache")
{
func = [separator](const YAML::Node& root_node, std::string& str) {
transform_with_regexp(root_node, str, RGX_MOUSTACHE, separator);
};
}
else if(format == "double_dollar")
{
func = [separator](const YAML::Node& root_node, std::string& str) {
transform_with_regexp(root_node, str, RGX_DOUBLE_DOLLAR, separator);
};
}
else if(format == "double_percent")
{
func = [separator](const YAML::Node& root_node, std::string& str) {
transform_with_regexp(root_node, str, RGX_DOUBLE_PERCENT, separator);
};
}
else
{
failure_message(1, "<!> Unsupported format: \'%s\'\n", format.c_str());
}
transforms.insert({format, func});
}
for(const std::string& regexp : regexps)
{
const RegexpWithPattern rgx(regexp);
if(!rgx.isValid())
{
failure_message(1, "<!> Regular expression invalid: %s\n", rgx.getPattern().c_str());
}
transform_func_t func = [rgx, separator](const YAML::Node& root_node, std::string& str)
{
transform_with_regexp(root_node, str, rgx, separator);
};
transforms.insert({regexp, func});
}
if(transforms.empty())
{
failure_message(1, "%s\n", "<!> No formats and regexps specified");
}
// -------------------------------------------------------------------------
// Transforming YAML file & output
try
{
// Interpret input as multi-document (works for single-document too)
const std::vector<YAML::Node> documents =
(input_file_path == "stdin")
?
YAML::LoadAll(std::cin)
:
YAML::LoadAllFromFile(input_file_path);
// ---------------------------------------------------------------------
YAML::Emitter emitter;
// Process each 'document' independently
for(const YAML::Node& document : documents)
{
// Transform node multiple times - on each pass called specific transform func
YAML::Node transformed = document; // no need to `YAML::Clone(document);` here
for(const auto& kv: transforms)
{
transformed = transform_nodes_clone(transformed, kv.second);
}
/*
// Alternate (non-working) code, with single 'clone'. Dont works
// in cases with multiple transform patterns in single line.
YAML::Node transformed = YAML::Clone(document);
for(const auto& kv: transforms)
{
transform_nodes(document, transformed, kv.second);
}
*/
// Add processed document into output-emitter
emitter << transformed;
}
const char* output_str = emitter.c_str();
// ---------------------------------------------------------------------
if(output_file_path.empty() || (output_file_path == "stdout")) // Output to stdout
{
std::cout << output_str << std::endl; // With extra trailing new-line
}
else // Output to file
{
std::ofstream fout(output_file_path);
if(fout.is_open())
{
fout << output_str;
fout.close();
}
else
{
failure_message(2, "<!> Could not open file: %s\n", output_file_path.c_str());
}
}
}
catch (const YAML::Exception& e)
{
failure_message(2, "<!> YAML error: %s\n", e.what());
}
return 0;
}