-
Notifications
You must be signed in to change notification settings - Fork 9
/
RtConfig.cpp
552 lines (450 loc) · 14 KB
/
RtConfig.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*=========================================================================
* RtConfig.cpp defines a class that controls configuration of a a
* real-time fMRI session.
*
* Copyright 2007-2013, the MURFI dev team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include"RtConfig.h"
#include"ace/Get_Opt.h"
#include"tinyxml/tinyxml.h"
#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<cstdlib>
typedef pair<string, string> StringPair;
const RtConfigVal RtConfig::unset;
//*** constructors/destructors ***//
// copy constructor (called often)
RtConfig::RtConfig(const RtConfig &other)
: parms(other.parms) {
// nothing to do here
}
// destructor
RtConfig::~RtConfig() {
// nothing to do here
}
//*** config loading routines ***//
// parse xml config file
// out: true (success) or false
bool RtConfig::parseConfigFile(const string &filename) {
ACE_TRACE(("RtConfig::parseConfigFile"));
setDefaults();
string study_name;
if (isSet("study:subject:name")) {
study_name = get("study:subject:name").str();
}
string subjects_dir;
if (isSet("study:subjectsDir")) {
subjects_dir = get("study:subjectsDir").str();
}
// try to read file
if(!parms.LoadFile(filename)) {
cout << parms.ErrorDesc() << endl;
parms.Clear();
parms.ClearError();
return false;
}
if (!study_name.empty()) {
set("study:subject:name", study_name);
}
if (!subjects_dir.empty()) {
set("study:subjectsDir", subjects_dir);
}
return validateConfig();
}
// parse xml config string
// out: true (success) or false
bool RtConfig::parseConfigStr(const string &xml) {
ACE_TRACE(("RtConfig::parseConfigStr"));
setDefaults();
// try to parse string
parms.Parse(xml.c_str());
if(parms.Error()) {
cout << parms.ErrorDesc() << endl;
parms.Clear();
parms.ClearError();
return false;
}
return validateConfig();
}
//*** config get/set parms ***/
// get a parm value.
// in
// name is a ':' delimited string spcifying a path into the XML configuration
// for example processor:module:name
// out
// string representing the value
RtConfigVal RtConfig::get(const char *name) {
return get(string(name));
}
// get a parm value. start searching from a specified node
// in
// name is a ':' delimited string spcifying a path into the XML configuration
// for example processor:module:name
// node is a xml node to start looking at
// out
// string representing the value
RtConfigVal RtConfig::get(const string &name, TiXmlNode *node) {
ACE_TRACE((ACE_TEXT("RtConfig::get(string, node)")));
// if no node specified, make it the top level
if(node == NULL) {
node = &parms;
}
// make sure we are an element if we are not text
if(node->Type() != TiXmlNode::ELEMENT && node != &parms) {
return unset;
}
TiXmlElement *elmt = (TiXmlElement*) node;
// find the next ':'
size_t delind = name.find(':');
string childName(name.substr(0,delind));
string rest, optionName;
if(delind == string::npos) { // found no more delim cases, try to return val
const char *val;
// if no children found, check attributes if there are no more ':'s
if(node != &parms && NULL != (val = elmt->Attribute(childName.c_str()))) {
return RtConfigVal(val);
}
// search children for text elements to return
TiXmlNode *text = 0;
// find either a node with the child name or an "option" with the right
// "name" attribute
while((text = elmt->IterateChildren(childName, text))) {
if(text->Type() == TiXmlNode::TEXT) {
return RtConfigVal(text->ValueStr());
}
}
while((text = elmt->IterateChildren("option", text))) {
if(text->ToElement() != NULL
&& TIXML_SUCCESS
== text->ToElement()->QueryValueAttribute("name",&optionName)
&& optionName == childName) {
return RtConfigVal(text->ToElement()->GetText());
}
}
}
else if(delind == name.length()-1) {
// we havent taken all delimiters, but there is no more remaining name,
// so look for text children
TiXmlNode *me;
while((me = node->IterateChildren(node->FirstChild()))) {
if(me->Type() == TiXmlNode::TEXT) {
return RtConfigVal(me->ValueStr());
}
}
return unset;
}
else {
rest = name.substr(delind+1);
}
// look for child with name, or a 'module', or "option"
// child with correct "name" attribute
TiXmlNode *child = 0;
while((child = elmt->IterateChildren(childName, child))) {
string val = get(rest,child);
// TODO figure out what to do if we're empty (could mean to be empty)
if(!val.empty()) {
return RtConfigVal(val);
}
}
while((child = elmt->IterateChildren("module", child))) {
if(child->ToElement() != NULL
&& TIXML_SUCCESS
== child->ToElement()->QueryValueAttribute("name",&optionName)
&& optionName == childName) {
string val = get(rest,child);
// TODO figure out what to do if we're empty (could mean to be empty)
if(!val.empty()) {
return RtConfigVal(val);
}
}
}
while((child = elmt->IterateChildren("option", child))) {
if(child->ToElement() != NULL
&& TIXML_SUCCESS
== child->ToElement()->QueryValueAttribute("name",&optionName)
&& optionName == childName) {
string val = get(rest,child);
// TODO figure out what to do if we're empty (could mean to be empty)
if(!val.empty()) {
return RtConfigVal(val);
}
}
}
return unset;
}
// get an xml node in the config. start searching from a specified node
// in
// name is a ':' delimited str spcifying a path into the XML configuration
// for example preprocessor:module:name
// node is a xml node to start looking at
// out
// node representing the value, or NULL if none does
TiXmlNode *RtConfig::getNode(const string &name, TiXmlNode *node) {
ACE_TRACE((ACE_TEXT("RtConfig::get(string, node)")));
// if no node specified, make it the top level
if(node == NULL) {
node = &parms;
}
// make sure we are an element if we are not text
if(node->Type() != TiXmlNode::ELEMENT && node != &parms) {
return NULL;
}
// if the name string is empty we return the current node
if(name.empty()) {
return node;
}
TiXmlElement *elmt = (TiXmlElement*) node;
// find the next ':'
size_t delind = name.find(':');
string childName(name.substr(0,delind));
string rest;
if(delind == string::npos) {
// found no more delim cases, try to return a text child
TiXmlNode *text = 0;
while((text = elmt->IterateChildren(childName, text))) {
if(text->Type() == TiXmlNode::TEXT) {
return text;
}
}
}
else if(delind == name.length()-1) {
// we havent taken all delimiters, but there is no more remaining name,
// so look for text children of any name
TiXmlNode *me;
while((me = node->IterateChildren(node->FirstChild()))) {
if(me->Type() == TiXmlNode::TEXT) {
return me;
}
}
return NULL;
}
else {
rest = name.substr(delind+1);
}
// look though each child with name, return the first non NULL
bool searchAllChildren = (childName == "*");
TiXmlNode *child = 0;
while((child = (searchAllChildren
? elmt->IterateChildren(child)
: elmt->IterateChildren(childName, child)
))) {
TiXmlNode *ret = getNode(rest,child);
if(ret != NULL) {
return ret;
}
}
return NULL;
}
// get all the option text values for the processing modules with a
// specified name.
//
// in
// module_name is a string representing the module name attribute.
// option_name is a string representing the option name attribute required.
// out
// a list of the matching subnode option names.
vector<string> RtConfig::getProcessingModuleNames(const string &module_name,
const string &option_name) {
vector<string> matches;
TiXmlNode* node = getNode("processor");
if (node == NULL) {
return matches;
}
TiXmlNode *child = NULL;
while((child = node->ToElement()->IterateChildren(child))) {
TiXmlElement *el = child->ToElement();
if (el == NULL) {
continue;
}
string name;
el->QueryValueAttribute("name",&name);
if (name != module_name) {
continue;
}
TiXmlNode *subchild = NULL;
while((subchild = el->IterateChildren("option", subchild))) {
TiXmlElement *subel = subchild->ToElement();
if (subel == NULL) {
continue;
}
string subname;
subel->QueryValueAttribute("name",&subname);
if (subname != option_name) {
continue;
}
matches.push_back(subchild->FirstChild()->ValueStr());
break;
}
}
return matches;
}
// determine if there is a value set for a particular config variable
// in
// the variable name to check
// out
// true if the var has been set
bool RtConfig::isSet(const string &name) {
return get(name,&parms).isSet();
}
// sets a parm value starting from a specified xml node
// children are created appropritately
// in
// name: ':' delimited string representing the xml node attribute to set
// value: the value to set the attribute to
// node: the xml node to start from
// out: success or failure
bool RtConfig::set(const string &name, const string &value,
TiXmlNode *node) {
if(node == NULL) {
return "";
}
size_t nextdelim = name.find(':');
TiXmlElement *ele = (TiXmlElement*) node;
// check if we have consumed all of the ':' things in the name
if(nextdelim == string::npos) {
if(node == &parms) { // if top level add an element with text
TiXmlElement newel(name);
TiXmlText txt(value);
newel.InsertEndChild(txt);
node->InsertEndChild(newel);
}
else { // otherwise add an attribute to the existing node
ele->SetAttribute(name,value);
}
return true; // termination condition
}
// find the node with the next name piece
string nextNode = name.substr(0,nextdelim);
string nextName = name.substr(nextdelim+1);
// get the child with the appropriate name, or create it
TiXmlNode *child = ele->FirstChild(nextNode);
if(child == NULL) {
child = new TiXmlElement(nextNode);
node->LinkEndChild(child);
}
// recursive set
return set(nextName, value, child);
}
// print a parm value
void RtConfig::print(const char *name) {
if(name == NULL) {
cout << "name is null";
return;
}
cout << get(name).str();
}
//*** private functions ***//
// for conversion of string types to other types
template <class T> inline bool RtConfig::convert(T &t, const string& s) {
istringstream iss(s);
return !(iss >> dec >> t).fail();
}
// print the name/value pairs to the screen
void RtConfig::dumpConfig(ostream &os) {
dumpNode(&parms, os);
}
//**************************************************//
// tinyxml support stuff
// some taken from the tinyXml examples
//
// get the attribute with a certain name
TiXmlAttribute *RtConfig::getElementAttribute(TiXmlElement *elmt,
const string &name) {
if(elmt == NULL) {
return NULL;
}
TiXmlAttribute* attr = elmt->FirstAttribute();
while(attr) {
if(name == attr->Name()) {
return attr;
}
}
return NULL;
}
// build a map between attribute names and values
map<string,string> RtConfig::getAttributeMap(TiXmlElement &ele) {
map<string,string> attrMap;
// find all attributes
for(TiXmlAttribute *attr = ele.FirstAttribute(); attr; attr = attr->Next()) {
string name = attr->Name();
attrMap[name] = attr->ValueStr();
}
return attrMap;
}
// get the indent for printing
const char *RtConfig::getIndent(unsigned int numIndents) {
static const char *pINDENT=" + ";
static const unsigned int LENGTH=strlen(pINDENT);
unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
if( n > LENGTH ) n = LENGTH;
return &pINDENT[LENGTH-n];
}
// same as getIndent but no "+" at the end
const char * RtConfig::getIndentAlt(unsigned int numIndents) {
static const char *pINDENT=" ";
static const unsigned int LENGTH=strlen(pINDENT);
unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
if(n > LENGTH) n = LENGTH;
return &pINDENT[LENGTH-n];
}
int RtConfig::dumpNodeAttribs(TiXmlElement* pElement, ostream &os,
unsigned int indent) {
if(!pElement) return 0;
TiXmlAttribute* pAttrib=pElement->FirstAttribute();
int i=0;
const char* pIndent=getIndent(indent);
while(pAttrib) {
os << pIndent << pAttrib->Name() << '=' << pAttrib->ValueStr() << endl;
i++;
pAttrib=pAttrib->Next();
}
return i;
}
void RtConfig::dumpNode(TiXmlNode* pParent, ostream &os,
unsigned int indent) {
if(!pParent) return;
TiXmlNode* pChild;
TiXmlText* pText;
int t = pParent->Type();
// ignore certain types
if(t == TiXmlNode::COMMENT
|| t == TiXmlNode::UNKNOWN
|| t == TiXmlNode::DECLARATION) {
return;
}
os << getIndent(indent);
switch(t) {
case TiXmlNode::DOCUMENT:
//
break;
case TiXmlNode::ELEMENT:
os << pParent->Value() << endl;
dumpNodeAttribs(pParent->ToElement(), os, indent+1);
break;
case TiXmlNode::TEXT:
pText = pParent->ToText();
os << pText->Value() << endl;;
break;
default:
break;
}
for(pChild = pParent->FirstChild(); pChild != 0;
pChild = pChild->NextSibling()) {
dumpNode(pChild, os, indent+1);
}
}