-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRegexParamNameAdaptor.cpp
More file actions
321 lines (266 loc) Β· 11.1 KB
/
RegexParamNameAdaptor.cpp
File metadata and controls
321 lines (266 loc) Β· 11.1 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// MLB Utility Library Module File
// ////////////////////////////////////////////////////////////////////////////
/*
File Name : %M%
File Version : %I%
Last Extracted : %D% %T%
Last Updated : %E% %U%
File Description : Implementation of functions which provide regex-based
adaptors for command line parameter parsing.
Revision History : 2006-10-25 --- Creation.
Michael L. Brock
Copyright Michael L. Brock 2006 - 2018.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// Required include files...
// ////////////////////////////////////////////////////////////////////////////
#include <Utility/RegexParamNameAdaptor.hpp>
#include <Utility/ParseCmdLineArg.hpp>
#include <Utility/PosixCRegex.hpp>
// ////////////////////////////////////////////////////////////////////////////
namespace MLB {
namespace Utility {
namespace {
// ////////////////////////////////////////////////////////////////////////////
const char MyPrivateUnlikelyParameterName[] =
"846B38B0-FCE6-472A-A636-74ED612C674C"
"--"
"7CD502BF-3D22-4ADF-9B2A-CD45D307D762"
"--"
"886DF695-DE04-4B06-96BC-1A5331B51D13"
"--"
"23539B89-76F1-4AFA-8888-636B85EFE7F4";
// ////////////////////////////////////////////////////////////////////////////
} // Anonymous namespace
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const PosixCRegexWrapper ¶m_pattern,
const char *this_param)
{
return(param_pattern.RegexSearch(this_param));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const PosixCRegexWrapper ¶m_pattern,
const std::string &this_param)
{
return(RegexParamName(param_pattern, this_param.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const char *param_pattern, const char *this_param)
{
return(RegexParamName(PosixCRegexWrapper(param_pattern,
boost::REG_PERL | boost::REG_ICASE), this_param));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const std::string ¶m_pattern,
const std::string &this_param)
{
return(RegexParamName(param_pattern.c_str(), this_param.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const char *param_pattern, unsigned int ¤t_index,
int argc, char **argv)
{
ParseCmdLineArg::CheckCmdLineSource(current_index, argc, argv);
return(RegexParamName(param_pattern, argv[current_index]));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const std::string ¶m_pattern,
unsigned int ¤t_index, int argc, char **argv)
{
return(RegexParamName(param_pattern.c_str(), current_index, argc, argv));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const std::vector<std::string> &pattern_list,
const char *this_param)
{
std::vector<std::string>::const_iterator iter_b(pattern_list.begin());
std::vector<std::string>::const_iterator iter_e(pattern_list.end());
for ( ; iter_b != iter_e; ++iter_b) {
if (RegexParamName(PosixCRegexWrapper(*iter_b,
boost::REG_PERL | boost::REG_ICASE), this_param))
return(true);
}
return(false);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const std::vector<std::string> &pattern_list,
const std::string &this_param)
{
return(RegexParamName(pattern_list, this_param.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool RegexParamName(const std::vector<std::string> &pattern_list,
unsigned int ¤t_index, int argc, char **argv)
{
ParseCmdLineArg::CheckCmdLineSource(current_index, argc, argv);
return(RegexParamName(pattern_list, argv[current_index]));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const PosixCRegexWrapper ¶m_pattern,
const char *this_param)
{
return((RegexParamName(param_pattern, this_param)) ? this_param :
MyPrivateUnlikelyParameterName);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const PosixCRegexWrapper ¶m_pattern,
const std::string &this_param)
{
return(RegexParamNameAdaptor(param_pattern, this_param.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const char *param_pattern,
const char *this_param)
{
return((RegexParamName(param_pattern, this_param)) ? this_param :
MyPrivateUnlikelyParameterName);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const std::string ¶m_pattern,
const std::string &this_param)
{
return(RegexParamNameAdaptor(param_pattern.c_str(), this_param.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const char *param_pattern,
unsigned int ¤t_index, int argc, char **argv)
{
return((RegexParamName(param_pattern, current_index, argc, argv)) ?
argv[current_index] : MyPrivateUnlikelyParameterName);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const std::string ¶m_pattern,
unsigned int ¤t_index, int argc, char **argv)
{
return(RegexParamNameAdaptor(param_pattern.c_str(), current_index, argc,
argv));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const std::vector<std::string> &pattern_list,
const char *this_param)
{
return((RegexParamName(pattern_list, this_param)) ? this_param :
MyPrivateUnlikelyParameterName);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const std::vector<std::string> &pattern_list,
const std::string &this_param)
{
return(RegexParamNameAdaptor(pattern_list, this_param.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
const char *RegexParamNameAdaptor(const std::vector<std::string> &pattern_list,
unsigned int ¤t_index, int argc, char **argv)
{
return((RegexParamName(pattern_list, current_index, argc, argv)) ?
argv[current_index] : MyPrivateUnlikelyParameterName);
}
// ////////////////////////////////////////////////////////////////////////////
} // namespace Utility
} // namespace MLB
#include <Utility/InlineContainer.hpp>
#include <Utility/StringSupport.hpp>
#ifdef TEST_MAIN
// ////////////////////////////////////////////////////////////////////////////
int TEST_ParseCmdLineRegex_1(int argc, char **argv)
{
using namespace MLB::Utility;
int return_code = EXIT_SUCCESS;
try {
PosixCRegexWrapper my_regex(
"^\\-(CME(_)*)*PREAMBLE(_)*SEQ(UENCE)*((_)*NUM(B(ER)*)*)*$",
boost::REG_PERL | boost::REG_ICASE);
for (int count_1 = 1; count_1 < argc; ++count_1) {
std::cout << std::setw(4) << count_1 << ": [" << argv[count_1] <<
"] = " << std::flush;
try {
std:: cout << (RegexParamName(my_regex, argv[count_1]) ?
"" : "NOT ") << "MATCHED" << std::endl;
}
catch (const std::exception &except) {
std::cout << "ERROR: " << except.what() << std::endl;
}
}
}
catch (const std::exception &except) {
std::cerr << std::endl << "ERROR IN REGEX PARSE TEST: " <<
except.what() << std::endl;
return_code = EXIT_FAILURE;
}
std::cout << PadLeft("", 79, '-') << std::endl;
return(return_code);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
int TEST_ParseCmdLineRegex_2(int argc, char **argv)
{
using namespace MLB::Utility;
int return_code = EXIT_SUCCESS;
try {
std::vector<std::string> pattern_list(
MLB::Utility::MakeInlineVector<std::string>
("^\\-(CME(_)*)*PREAMBLE(_)*SEQ(UENCE)*((_)*NUM(B(ER)*)*)*$")
("^\\-(CME(_)*)*PREAMBLE(_)*SEQ(UENCE)*((_)*NUM(B(ER)*)*)*X$"));
for (int count_1 = 1; count_1 < argc; ++count_1) {
std::cout << std::setw(4) << count_1 << ": [" << argv[count_1] <<
"] = " << std::flush;
try {
std:: cout << (RegexParamName(pattern_list, argv[count_1]) ?
"" : "NOT ") << "MATCHED" << std::endl;
}
catch (const std::exception &except) {
std::cout << "ERROR: " << except.what() << std::endl;
}
}
}
catch (const std::exception &except) {
std::cerr << std::endl << "ERROR IN REGEX PARSE TEST: " <<
except.what() << std::endl;
return_code = EXIT_FAILURE;
}
std::cout << PadLeft("", 79, '-') << std::endl;
return(return_code);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
int return_code = EXIT_SUCCESS;
try {
if (TEST_ParseCmdLineRegex_1(argc, argv) != EXIT_SUCCESS)
return_code = EXIT_FAILURE;
if (TEST_ParseCmdLineRegex_2(argc, argv) != EXIT_SUCCESS)
return_code = EXIT_FAILURE;
}
catch (const std::exception &except) {
std::cerr << std::endl << "ERROR: " << except.what() << std::endl;
return_code = EXIT_FAILURE;
}
return(return_code);
}
// ////////////////////////////////////////////////////////////////////////////
#endif // #ifdef TEST_MAIN