13
13
#include " caliper/common/Log.h"
14
14
#include " caliper/common/util/split.hpp"
15
15
16
+ #include " ../../common/util/parse_util.h"
17
+
16
18
#include " ../../services/Services.h"
17
19
18
20
#include " caliper/ConfigManager.h"
@@ -27,61 +29,14 @@ using namespace util;
27
29
namespace
28
30
{
29
31
30
- inline bool
31
- is_one_of (char c, const char * characters)
32
- {
33
- for (const char * ptr = characters; *ptr != ' \0 ' ; ++ptr)
34
- if (*ptr == c)
35
- return true ;
36
-
37
- return false ;
38
- }
39
-
40
- // / \brief Read next character, ignoring whitespace
41
- char
42
- parse_char (std::istream& is)
43
- {
44
- char ret = ' \0 ' ;
45
-
46
- do {
47
- ret = is.get ();
48
- } while (is.good () && isspace (ret));
49
-
50
- return ret;
51
- }
52
-
53
- // / \brief Parse text from stream, ignoring whitespace, until one of ",()" are found
54
- std::string
55
- parse_word (std::istream& is)
56
- {
57
- std::string ret;
58
-
59
- while (is.good ()) {
60
- auto c = is.get ();
61
-
62
- if (!is.good ())
63
- break ;
64
- if (isspace (c))
65
- continue ;
66
- if (is_one_of (c, " ,()\n " )) {
67
- is.unget ();
68
- break ;
69
- }
70
-
71
- ret.push_back (c);
72
- }
73
-
74
- return ret;
75
- }
76
-
77
32
// / \brief Parse "(arg1, arg2, ...)" argument list, ignoring whitespace
78
33
std::vector<std::string>
79
34
parse_arglist (std::istream& is)
80
35
{
81
36
std::vector<std::string> ret;
82
37
std::string word;
83
38
84
- char c = parse_char (is);
39
+ char c = util::read_char (is);
85
40
86
41
if (!is.good ())
87
42
return ret;
@@ -90,10 +45,10 @@ parse_arglist(std::istream& is)
90
45
is.unget ();
91
46
return ret;
92
47
}
93
-
48
+
94
49
do {
95
- std::string str = parse_word (is);
96
- c = parse_char (is);
50
+ std::string str = util::read_word (is, " ,() " );
51
+ c = util::read_char (is);
97
52
98
53
if (!str.empty () && (c == ' ,' || c == ' )' ))
99
54
ret.push_back (str);
@@ -103,20 +58,20 @@ parse_arglist(std::istream& is)
103
58
is.unget ();
104
59
ret.clear ();
105
60
}
106
-
61
+
107
62
return ret;
108
63
}
109
64
110
65
std::pair< int , std::vector<std::string> >
111
66
parse_functioncall (std::istream& is, const QuerySpec::FunctionSignature* defs)
112
67
{
113
68
// read function name
114
- std::string fname = parse_word (is);
69
+ std::string fname = util::read_word (is, " ,() " );
115
70
116
71
if (fname.empty ())
117
72
return std::make_pair (-1 , std::vector<std::string>());
118
73
119
- // find function among given signatures
74
+ // find function among given signatures
120
75
int retid = 0 ;
121
76
122
77
for ( ; defs && defs[retid].name && (fname != defs[retid].name ); ++retid)
@@ -127,7 +82,7 @@ parse_functioncall(std::istream& is, const QuerySpec::FunctionSignature* defs)
127
82
return std::make_pair (-1 , std::vector<std::string>());
128
83
}
129
84
130
- // read argument list
85
+ // read argument list
131
86
std::vector<std::string> args = parse_arglist (is);
132
87
int argsize = static_cast <int >(args.size ());
133
88
@@ -159,7 +114,7 @@ QueryArgsParser::parse_args(const Args& args)
159
114
160
115
m_error = false ;
161
116
m_error_msg.clear ();
162
-
117
+
163
118
// parse CalQL query (if any)
164
119
165
120
if (args.is_set (" query" )) {
@@ -173,23 +128,23 @@ QueryArgsParser::parse_args(const Args& args)
173
128
} else
174
129
m_spec = p.spec ();
175
130
}
176
-
131
+
177
132
// setup filter
178
133
179
134
if (args.is_set (" select" )) {
180
135
m_spec.filter .selection = QuerySpec::FilterSelection::List;
181
136
m_spec.filter .list = RecordSelector::parse (args.get (" select" ));
182
137
}
183
-
138
+
184
139
// setup attribute selection
185
-
140
+
186
141
if (args.is_set (" attributes" )) {
187
142
m_spec.attribute_selection .selection = QuerySpec::AttributeSelection::List;
188
143
util::split (args.get (" attributes" ), ' ,' , std::back_inserter (m_spec.attribute_selection .list ));
189
144
}
190
-
145
+
191
146
// setup aggregation
192
-
147
+
193
148
if (args.is_set (" aggregate" )) {
194
149
// aggregation ops
195
150
m_spec.aggregation_ops .selection = QuerySpec::AggregationSelection::Default;
@@ -203,20 +158,20 @@ QueryArgsParser::parse_args(const Args& args)
203
158
char c;
204
159
205
160
const QuerySpec::FunctionSignature* defs = Aggregator::aggregation_defs ();
206
-
161
+
207
162
do {
208
163
auto fpair = parse_functioncall (is, defs);
209
164
210
165
if (fpair.first >= 0 )
211
166
m_spec.aggregation_ops .list .emplace_back (defs[fpair.first ], fpair.second );
212
167
213
- c = parse_char (is);
168
+ c = util::read_char (is);
214
169
} while (is.good () && c == ' ,' );
215
170
}
216
171
217
- // aggregation key
172
+ // aggregation key
218
173
m_spec.aggregation_key .selection = QuerySpec::AttributeSelection::Default;
219
-
174
+
220
175
if (args.is_set (" aggregate-key" )) {
221
176
std::string keystr = args.get (" aggregate-key" );
222
177
@@ -228,12 +183,12 @@ QueryArgsParser::parse_args(const Args& args)
228
183
}
229
184
}
230
185
}
231
-
186
+
232
187
// setup sort
233
-
188
+
234
189
if (args.is_set (" sort" )) {
235
190
m_spec.sort .selection = QuerySpec::SortSelection::List;
236
-
191
+
237
192
std::vector<std::string> list;
238
193
util::split (args.get (" sort" ), ' ,' , std::back_inserter (list));
239
194
@@ -242,9 +197,9 @@ QueryArgsParser::parse_args(const Args& args)
242
197
}
243
198
244
199
// setup formatter
245
-
200
+
246
201
for (const QuerySpec::FunctionSignature* fmtsig = FormatProcessor::formatter_defs (); fmtsig && fmtsig->name ; ++fmtsig) {
247
- // see if a formatting option is set
202
+ // see if a formatting option is set
248
203
if (args.is_set (fmtsig->name )) {
249
204
m_spec.format .opt = QuerySpec::FormatSpec::User;
250
205
m_spec.format .formatter = *fmtsig;
@@ -263,11 +218,11 @@ QueryArgsParser::parse_args(const Args& args)
263
218
264
219
return false ;
265
220
}
266
-
221
+
267
222
break ;
268
223
}
269
224
}
270
-
225
+
271
226
return true ;
272
227
}
273
228
0 commit comments