-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.cpp
387 lines (307 loc) · 7.36 KB
/
configuration.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
/*
* configuration.cpp
* PhoenixSim
*
* Created by Johnnie Chan on 7/18/11.
* Copyright 2011 Johnnie Chan. All rights reserved.
*
*/
#include "configuration.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
using namespace PhoenixSim;
Configuration::Configuration()
:currentSection("none")
{
}
Configuration::~Configuration()
{
}
bool Configuration::ParseFile(string fileName)
{
ifstream iniFile;
iniFile.open(fileName.c_str(), ios::in);
int lineNumber = 0;
string lineToParse;
while(!iniFile.eof())
{
getline(iniFile, lineToParse);
lineNumber++;
//std::cout<<lineNumber<<": ";
//std::cout<<lineToParse<<endl;
ParseLine(lineToParse.substr(0, lineToParse.find('#')));
}
iniFile.close();
std::cout<<(*this)<<endl;
std::cout<<"# of runs: "<<NumOfRuns()<<endl;
return true;
}
bool Configuration::ParseLine(std::string configLine)
{
vector<string> strings;
if(!IsBlank(configLine))
{
if(IsSectionDeclaration(configLine))
{
strings = GetSectionFields(configLine);
if(strings.empty())
{
}
else if(strings[0] == "General")
{
currentSection = "General";
}
}
else if(IsValueAssignment(configLine))
{
if(currentSection == "none")
{
throw "No section assigned yet";
}
else if(currentSection == "General")
{
pair<vector<string>, AbstractConfigValue*> args = GetAssignmentArguments(configLine);
args.second->runMultipler["General"] = 0;
parameters["General"].insert(GetAssignmentArguments(configLine));
}
}
else
{
throw "Unable to determine line syntax";
}
}
else
{
}
return true;
}
bool Configuration::IsBlank(std::string line)
{
size_t firstCharPos = line.find_first_not_of(" \t\n\r");
return firstCharPos == string::npos;
}
bool Configuration::IsSectionDeclaration(std::string line)
{
size_t firstCharPos = line.find_first_not_of(" \t\n\r");
return firstCharPos != string::npos && '[' == line[firstCharPos];
}
vector<string> Configuration::GetSectionFields(std::string line)
{
vector<string> fields;
size_t begin, next, end;
begin = line.find('[');
end = line.find(']');
begin = line.find_first_not_of(" \t", begin + 1);
while(begin < end)
{
next = line.find_first_of(" \t]", begin);
fields.push_back(line.substr(begin, next - begin));
begin = next + 1;
}
return fields;
}
bool Configuration::IsValueAssignment(std::string line)
{
return line.find('=') != string::npos && line.find('=') == line.rfind('=');
}
pair< std::vector<std::string>, AbstractConfigValue*> Configuration::GetAssignmentArguments(std::string line)
{
pair< std::vector<std::string>, AbstractConfigValue*> returnPair;
returnPair.first = ParseName(line.substr(0,line.find('=')));
returnPair.second = ParseValue(line.substr(line.find('=') + 1, string::npos));
return returnPair;
}
vector<string> Configuration::ParseName(std::string fullName)
{
vector<string> names;
size_t begin, next, end;
begin = fullName.find_first_not_of(" \t");
end = fullName.find_last_not_of(" \t");
while(begin < end)
{
next = fullName.find('.', begin);
if(next > end)
{
next = end + 1;
}
names.push_back(fullName.substr(begin, next - begin));
if(names.back().empty())
{
throw "blank name not allowed";
}
begin = next + 1;
}
return names;
}
AbstractConfigValue* Configuration::ParseValue(string fullValue)
{
AbstractConfigValue* configVal = NULL;
size_t begin, next, end;
begin = fullValue.find_first_not_of(" \t");
end = fullValue.find_last_not_of(" \t");
next = fullValue.find_first_of("+-1234567890.$\"");
if(next == begin)
{
if(fullValue[begin] == '"')
{
configVal = new ConfigString;
((ConfigString*)configVal)->data.push_back(ParseString(fullValue));
}
else if(fullValue[begin] == '$')
{
configVal = ParseArray(fullValue);
}
else // probably a number
{
configVal = new ConfigDouble;
((ConfigDouble*)configVal)->data.push_back(ParseDouble(fullValue));
}
}
else
{
throw "error parsing value";
}
return configVal;
}
string Configuration::ParseString(string value)
{
size_t begin, next;
begin = value.find('"') + 1;
next = value.find('"', begin);
return value.substr(begin, next - begin);
}
double Configuration::ParseDouble(string value)
{
return atof(value.c_str());
}
AbstractConfigValue* Configuration::ParseArray(string fullValue)
{
size_t begin, next, end;
ConfigType arrayType = CONFIG_NONE;
AbstractConfigValue *array = NULL;
begin = fullValue.find_first_not_of(" \t");
end = fullValue.find_last_not_of(" \t");
next = fullValue.find('$');
string name;
if(begin != next)
{
throw "improper declaration of array";
}
begin = next + 1;
next = fullValue.find('{');
name = fullValue.substr(begin, next - begin);
begin = fullValue.find_first_not_of(" \t", next + 1);
if(end != fullValue.find('}', begin))
{
throw "incorrect array termination";
}
std::cout<<"loop:"<<endl;
while(begin < end)
{
std::cout<<"begin:"<<begin<<" ";
next = fullValue.find_first_of(",}", begin);
std::cout<<"next:"<<next<<" ";
if(fullValue[begin] == '"')
{
if(arrayType == CONFIG_NONE)
{
arrayType = CONFIG_STRING;
array = new ConfigString;
}
if(arrayType == CONFIG_STRING)
{
((ConfigString*)array)->data.push_back(ParseString(fullValue.substr(begin, next - begin)));
}
else
{
throw "varying type in array";
}
}
else if(begin == fullValue.find_first_of("+-1234567890.", begin))
{
if(arrayType == CONFIG_NONE)
{
arrayType = CONFIG_DOUBLE;
array = new ConfigDouble;
}
if(arrayType == CONFIG_DOUBLE)
{
((ConfigDouble*)array)->data.push_back(ParseDouble(fullValue.substr(begin, next - begin)));
}
else
{
throw "varying type in array";
}
}
else {
throw "incorrect format for array element";
}
begin = fullValue.find_first_not_of(" \t", next + 1);
}
return array;
}
ostream& PhoenixSim::operator<<(ostream& os, const Configuration &v)
{
os<<"Parameters List"<<endl;
for(map<string, map<vector<string>, AbstractConfigValue* > >::const_iterator it = v.parameters.begin(); it != v.parameters.end(); it++)
{
os<<"\t"<<it->first<<endl;
for(map<vector<string>, AbstractConfigValue* >::const_iterator sub_it = it->second.begin(); sub_it != it->second.end(); sub_it++)
{
os<<"\t\t";
for(int i = 0; i < sub_it->first.size(); i++)
{
os<<sub_it->first[i];
if(i + 1 == sub_it->first.size())
{
os<<"=";
}
else
{
os<<".";
}
}
if(sub_it->second != NULL)
{
os<<"${";
switch(sub_it->second->type)
{
case CONFIG_DOUBLE:
for(int i = 0; i < sub_it->second->GetSize(); i++)
{
os<<static_cast<ConfigDouble*>(sub_it->second)->data[i];
if(i + 1 != sub_it->second->GetSize())
{
os<<",";
}
}
break;
case CONFIG_STRING:
for(int i = 0; i < sub_it->second->GetSize(); i++)
{
os<<static_cast<ConfigString*>(sub_it->second)->data[i];
if(i + 1 != sub_it->second->GetSize())
{
os<<",";
}
}
break;
}
os<<"}";
}
os<<"\n";
}
}
return os;
}
int Configuration::NumOfRuns(string config)
{
int count = 1;
for(map<vector<string>, AbstractConfigValue* >::const_iterator it = parameters[config].begin(); it != parameters[config].end(); it++)
{
count *= it->second->GetSize();
}
return count;
}