-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparam.cpp
356 lines (289 loc) · 9.48 KB
/
param.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// --------------------------------------------------------------------
//
// This file is part of Luna.
//
// LUNA is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Luna is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Luna. If not, see <http://www.gnu.org/licenses/>.
//
// Please see LICENSE.txt for more details.
//
// --------------------------------------------------------------------
#include "eval.h"
#include "luna.h"
extern logger_t logger;
extern writer_t writer;
extern freezer_t freezer;
//
// param_t
//
void param_t::add( const std::string & option , const std::string & value )
{
// set key=value pairs to opt[]
if ( option == "" ) return;
// we're assuming this is on the same luna command line, which makes
// no sense to have multiple versions
// two special cases:
// 1) if key+=value, then ","-append to any exists list
// 2) in API mode, allow key to already exist
const bool append_mode = option[ option.size() - 1 ] == '+';
if ( append_mode )
{
const std::string option1 = option.substr( 0 , option.size() - 1 );
if ( option1 == "" ) return;
if ( opt.find( option1 ) == opt.end() )
opt[ option1 ] = value;
else
opt[ option1 ] = opt[ option1 ] + "," + value;
return;
}
// else check no doubles unless in API mode
if ( ! globals::api_mode )
if ( opt.find( option ) != opt.end() )
Helper::halt( option + " parameter specified twice, only one value would be retained" );
opt[ option ] = value;
}
void param_t::add_hidden( const std::string & option , const std::string & value )
{
add( option , value );
hidden.insert( option );
}
int param_t::size() const
{
// handle hidden things...
return opt.size() - hidden.size();
}
void param_t::parse( const std::string & s )
{
std::vector<std::string> tok = Helper::quoted_parse( s , "=" );
if ( tok.size() == 2 ) add( tok[0] , tok[1] );
else if ( tok.size() == 1 ) add( tok[0] , "__null__" );
else // ignore subsequent '=' signs in 'value' (i.e. key=value=2 is 'okay', means "value=2" is set to 'key')
{
std::string v = tok[1];
for (int i=2;i<tok.size();i++) v += "=" + tok[i];
add( tok[0] , v );
}
}
void param_t::update( const std::string & id , const std::string & wc )
{
// replace all instances of 'globals::indiv_wildcard' with 'id'
// for all values
// TODO: amend this so that we can edit keys (or generic text)
// with variables and includes; currently, only the values of keys
// e.g. for x=y, only y can be a variable/include
std::map<std::string,std::string>::iterator ii = opt.begin();
while ( ii != opt.end() )
{
std::string v = ii->second;
bool changed = false;
// 1. replace indiv wildcard (e.g. ^) with this person's ID
// nb. this will also happen via ${id} which is a special, automatic individual-level
// variable
while ( v.find( wc ) != std::string::npos )
{
int p = v.find( wc );
v = v.substr( 0 , p ) + id + v.substr(p+1);
changed = true;
}
// 2. for any @{includes}, insert contents of file (comma-delimited)
if ( Helper::swap_in_includes( &v ) )
changed = true;
//
// needs updating?
//
if ( changed || v != ii->second )
ii->second = v;
++ii;
}
}
void param_t::clear()
{
opt.clear();
hidden.clear();
}
bool param_t::has(const std::string & s ) const
{
return opt.find(s) != opt.end();
}
bool param_t::empty(const std::string & s ) const
{
if ( ! has( s ) ) return true; // no key
return opt.find( s )->second == "__null__";
}
bool param_t::yesno(const std::string & s ) const
{
if ( ! has( s ) ) return false;
return Helper::yesno( opt.find( s )->second ) ;
}
std::string param_t::value( const std::string & s , const bool uppercase ) const
{
if ( has( s ) )
return uppercase ?
Helper::remove_all_quotes( Helper::toupper( opt.find( s )->second ) )
: Helper::remove_all_quotes( opt.find( s )->second );
else
return "";
}
bool param_t::single() const
{
return size() == 1;
}
std::string param_t::single_value() const
{
if ( ! single() ) Helper::halt( "no single value" );
std::map<std::string,std::string>::const_iterator ii = opt.begin();
while ( ii != opt.end() )
{
if ( hidden.find( ii->first ) == hidden.end() )
return Helper::remove_all_quotes( ii->first );
++ii;
}
return ""; // should not happen
}
std::string param_t::single_pair( std::string * value ) const
{
if ( ! single() ) Helper::halt( "no single value/pair" );
std::map<std::string,std::string>::const_iterator ii = opt.begin();
while ( ii != opt.end() )
{
if ( hidden.find( ii->first ) == hidden.end() )
{
*value = Helper::remove_all_quotes( ii->second );
return Helper::remove_all_quotes( ii->first );
}
++ii;
}
// should not happen
*value = "";
return "";
}
std::string param_t::requires( const std::string & s , const bool uppercase ) const
{
if ( ! has(s) ) Helper::halt( "command requires parameter " + s );
return value(s, uppercase );
}
int param_t::requires_int( const std::string & s ) const
{
if ( ! has(s) ) Helper::halt( "command requires parameter " + s );
int r;
if ( ! Helper::str2int( value(s) , &r ) )
Helper::halt( "command requires parameter " + s + " to have an integer value" );
return r;
}
double param_t::requires_dbl( const std::string & s ) const
{
if ( ! has(s) ) Helper::halt( "command requires parameter " + s );
double r;
if ( ! Helper::str2dbl( value(s) , &r ) )
Helper::halt( "command requires parameter " + s + " to have a numeric value" );
return r;
}
std::string param_t::dump( const std::string & indent , const std::string & delim ) const
{
std::map<std::string,std::string>::const_iterator ii = opt.begin();
int sz = opt.size();
int cnt = 1;
std::stringstream ss;
while ( ii != opt.end() )
{
if ( ii->second != "__null__" )
ss << indent << ii->first << "=" << ii->second;
else
ss << indent << ii->first ;
if ( cnt != sz )
ss << delim;
++cnt;
++ii;
}
return ss.str();
}
std::set<std::string> param_t::strset( const std::string & k , const std::string delim , const bool uppercase ) const
{
std::set<std::string> s;
if ( ! has(k) ) return s;
std::vector<std::string> tok = Helper::quoted_parse( value(k , uppercase ) , delim );
for (int i=0;i<tok.size();i++)
s.insert( Helper::unquote( tok[i]) );
return s;
}
std::set<std::string> param_t::strset_xsigs( const std::string & k , const std::string delim , const bool uppercase ) const
{
std::set<std::string> s;
if ( ! has(k) ) return s;
const std::string t = Helper::incexc( Helper::xsigs( value(k,uppercase) ) );
std::vector<std::string> tok = Helper::quoted_parse( t , delim );
for (int i=0;i<tok.size();i++)
s.insert( Helper::unquote( tok[i]) );
return s;
}
std::vector<std::string> param_t::strvector( const std::string & k , const std::string delim , const bool uppercase ) const
{
std::vector<std::string> s;
if ( ! has(k) ) return s;
std::vector<std::string> tok = Helper::quoted_parse( value(k,uppercase) , delim );
for (int i=0;i<tok.size();i++)
s.push_back( Helper::unquote( tok[i]) );
return s;
}
// embed [x][y] expansion in strvector, as well as [x] & [-x] inc/exc
std::vector<std::string> param_t::strvector_xsigs( const std::string & k , const std::string delim , const bool uppercase ) const
{
std::vector<std::string> s;
if ( ! has(k) ) return s;
// first get string and process for xsigs, then tokenize
const std::string t = Helper::incexc( Helper::xsigs( value(k,uppercase) ) );
// std::cout << "\n\nt [" << t << "]\n";
std::vector<std::string> tok = Helper::quoted_parse( t , delim );
for (int i=0;i<tok.size();i++)
s.push_back( Helper::unquote( tok[i]) );
return s;
}
std::vector<double> param_t::dblvector( const std::string & k , const std::string delim ) const
{
std::vector<double> s;
if ( ! has(k) ) return s;
std::vector<std::string> tok = Helper::quoted_parse( value(k) , delim );
for (int i=0;i<tok.size();i++)
{
std::string str = Helper::unquote( tok[i]);
double d = 0;
if ( ! Helper::str2dbl( str , &d ) ) Helper::halt( "Option " + k + " requires a double value(s)" );
s.push_back(d);
}
return s;
}
std::vector<int> param_t::intvector( const std::string & k , const std::string delim ) const
{
std::vector<int> s;
if ( ! has(k) ) return s;
std::vector<std::string> tok = Helper::quoted_parse( value(k) , delim );
for (int i=0;i<tok.size();i++)
{
std::string str = Helper::unquote( tok[i]);
int d = 0;
if ( ! Helper::str2int( str , &d ) ) Helper::halt( "Option " + k + " requires an integer value(s)" );
s.push_back(d);
}
return s;
}
std::set<std::string> param_t::keys() const
{
std::set<std::string> s;
std::map<std::string,std::string>::const_iterator ii = opt.begin();
while ( ii != opt.end() )
{
s.insert( ii->first );
++ii;
}
return s;
}