-
Notifications
You must be signed in to change notification settings - Fork 184
/
PHFlag.cc
449 lines (412 loc) · 12.1 KB
/
PHFlag.cc
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
#include "PHFlag.h"
// boost stacktrace header causes a shadow warning
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#include <boost/stacktrace.hpp>
#pragma GCC diagnostic pop
#include <fstream>
#include <iostream>
#include <map>
#include <utility> // for pair
const std::string
PHFlag::get_StringFlag(const std::string &name) const
{
std::map<std::string, std::string>::const_iterator iter = m_StringFlagMap.find(name);
if (iter != m_StringFlagMap.end())
{
return iter->second;
}
std::cout << "PHFlag::getString: ERROR Unknown character Flag " << name << std::endl;
PrintStackTrace();
std::cout << "The following flags are implemented: " << std::endl;
Print();
return "";
}
const std::string
PHFlag::get_StringFlag(const std::string &name, const std::string &defaultval)
{
std::map<std::string, std::string>::const_iterator iter = m_StringFlagMap.find(name);
if (iter != m_StringFlagMap.end())
{
return iter->second;
}
else
{
set_StringFlag(name, defaultval);
return get_StringFlag(name);
}
}
void PHFlag::set_StringFlag(const std::string &name, const std::string &charstr)
{
m_StringFlagMap[name] = charstr;
return;
}
double PHFlag::get_DoubleFlag(const std::string &name) const
{
std::map<std::string, double>::const_iterator iter = m_DoubleFlagMap.find(name);
if (iter != m_DoubleFlagMap.end())
{
return iter->second;
}
std::cout << "PHFlag::getFlag: ERROR Unknown Double Flag " << name << std::endl;
PrintStackTrace();
std::cout << "The following flags are implemented: " << std::endl;
Print();
return 0.0;
}
double PHFlag::get_DoubleFlag(const std::string &name, const double defaultval)
{
std::map<std::string, double>::const_iterator iter = m_DoubleFlagMap.find(name);
if (iter != m_DoubleFlagMap.end())
{
return iter->second;
}
else
{
set_DoubleFlag(name, defaultval);
return get_DoubleFlag(name);
}
}
void PHFlag::set_DoubleFlag(const std::string &name, const double iflag)
{
m_DoubleFlagMap[name] = iflag;
return;
}
float PHFlag::get_FloatFlag(const std::string &name) const
{
std::map<std::string, float>::const_iterator iter = m_FloatFlagMap.find(name);
if (iter != m_FloatFlagMap.end())
{
return iter->second;
}
std::cout << "PHFlag::getFlag: ERROR Unknown Float Flag " << name << std::endl;
PrintStackTrace();
std::cout << "The following flags are implemented: " << std::endl;
Print();
return 0.0;
}
float PHFlag::get_FloatFlag(const std::string &name, const float defaultval)
{
std::map<std::string, float>::const_iterator iter = m_FloatFlagMap.find(name);
if (iter != m_FloatFlagMap.end())
{
return iter->second;
}
else
{
set_FloatFlag(name, defaultval);
return get_FloatFlag(name);
}
}
void PHFlag::set_FloatFlag(const std::string &name, const float iflag)
{
m_FloatFlagMap[name] = iflag;
return;
}
int PHFlag::get_IntFlag(const std::string &name) const
{
std::map<std::string, int>::const_iterator iter = m_IntFlagMap.find(name);
if (iter != m_IntFlagMap.end())
{
return iter->second;
}
std::cout << "PHFlag::getFlag: ERROR Unknown Int Flag " << name << std::endl;
PrintStackTrace();
std::cout << "The following flags are implemented: " << std::endl;
Print();
return 0;
}
int PHFlag::get_IntFlag(const std::string &name, int defaultval)
{
std::map<std::string, int>::const_iterator iter = m_IntFlagMap.find(name);
if (iter != m_IntFlagMap.end())
{
return iter->second;
}
else
{
set_IntFlag(name, defaultval);
return get_IntFlag(name);
}
}
void PHFlag::set_IntFlag(const std::string &name, const int iflag)
{
m_IntFlagMap[name] = iflag;
return;
}
uint64_t PHFlag::get_uint64Flag(const std::string &name) const
{
std::map<std::string, uint64_t>::const_iterator iter = m_UInt64FlagMap.find(name);
if (iter != m_UInt64FlagMap.end())
{
return iter->second;
}
std::cout << "PHFlag::getFlag: ERROR Unknown uint64 Flag " << name << std::endl;
PrintStackTrace();
std::cout << "The following flags are implemented: " << std::endl;
Print();
return 0;
}
uint64_t PHFlag::get_uint64Flag(const std::string &name, uint64_t defaultval)
{
std::map<std::string, uint64_t>::const_iterator iter = m_UInt64FlagMap.find(name);
if (iter != m_UInt64FlagMap.end())
{
return iter->second;
}
else
{
set_uint64Flag(name, defaultval);
return get_uint64Flag(name);
}
}
void PHFlag::set_uint64Flag(const std::string &name, const uint64_t iflag)
{
m_UInt64FlagMap[name] = iflag;
return;
}
void PHFlag::Print() const
{
PrintIntFlags();
Printuint64Flags();
PrintFloatFlags();
PrintDoubleFlags();
PrintStringFlags();
return;
}
void PHFlag::PrintIntFlags() const
{
// loop over the map and print out the content (name and location in memory)
std::cout << std::endl
<< "Integer Flags:" << std::endl;
std::map<std::string, int>::const_iterator intiter;
for (intiter = m_IntFlagMap.begin(); intiter != m_IntFlagMap.end(); ++intiter)
{
std::cout << intiter->first << " is " << intiter->second << std::endl;
}
return;
}
void PHFlag::Printuint64Flags() const
{
// loop over the map and print out the content (name and location in memory)
std::cout << std::endl
<< "uint64 Flags:" << std::endl;
std::map<std::string, uint64_t>::const_iterator intiter;
for (intiter = m_UInt64FlagMap.begin(); intiter != m_UInt64FlagMap.end(); ++intiter)
{
std::cout << intiter->first << " is " << intiter->second << std::endl;
}
return;
}
void PHFlag::PrintDoubleFlags() const
{
// loop over the map and print out the content (name and location in memory)
std::cout << std::endl
<< "Double Flags:" << std::endl;
std::map<std::string, double>::const_iterator doubleiter;
for (doubleiter = m_DoubleFlagMap.begin(); doubleiter != m_DoubleFlagMap.end(); ++doubleiter)
{
std::cout << doubleiter->first << " is " << doubleiter->second << std::endl;
}
return;
}
void PHFlag::PrintFloatFlags() const
{
// loop over the map and print out the content (name and location in memory)
std::cout << std::endl
<< "Float Flags:" << std::endl;
std::map<std::string, float>::const_iterator floatiter;
for (floatiter = m_FloatFlagMap.begin(); floatiter != m_FloatFlagMap.end(); ++floatiter)
{
std::cout << floatiter->first << " is " << floatiter->second << std::endl;
}
return;
}
void PHFlag::PrintStringFlags() const
{
// loop over the map and print out the content (name and location in memory)
std::cout << std::endl
<< "String Flags:" << std::endl;
std::map<std::string, std::string>::const_iterator chariter;
for (chariter = m_StringFlagMap.begin(); chariter != m_StringFlagMap.end(); ++chariter)
{
std::cout << chariter->first << " is " << chariter->second << std::endl;
}
return;
}
int PHFlag::FlagExist(const std::string &name) const
{
std::map<std::string, int>::const_iterator iter = m_IntFlagMap.find(name);
if (iter != m_IntFlagMap.end())
{
return 1;
}
std::map<std::string, uint64_t>::const_iterator uiter = m_UInt64FlagMap.find(name);
if (uiter != m_UInt64FlagMap.end())
{
return 1;
}
std::map<std::string, float>::const_iterator fiter = m_FloatFlagMap.find(name);
if (fiter != m_FloatFlagMap.end())
{
return 1;
}
std::map<std::string, double>::const_iterator diter = m_DoubleFlagMap.find(name);
if (diter != m_DoubleFlagMap.end())
{
return 1;
}
std::map<std::string, std::string>::const_iterator citer = m_StringFlagMap.find(name);
if (citer != m_StringFlagMap.end())
{
return 1;
}
return 0;
}
void PHFlag::ReadFromFile(const std::string &name)
{
std::string label;
float fvalue;
int fvaluecount = 0;
double dvalue;
int dvaluecount = 0;
int ivalue;
int ivaluecount = 0;
uint64_t uivalue = 0;
int uivaluecount = 0;
std::string cvalue;
int cvaluecount = 0;
std::string junk;
int junkcount = 0;
std::ifstream infile(name);
while (infile >> label)
{
std::cout << "Label " << label;
if (label.substr(0, 1) == "S")
{
infile >> cvalue;
cvaluecount++;
set_StringFlag(label.substr(1, label.size() - 1), cvalue);
std::cout << " type S read " << cvalue << std::endl;
}
else if (label.substr(0, 1) == "F")
{
infile >> fvalue;
fvaluecount++;
set_FloatFlag(label.substr(1, label.size() - 1), fvalue);
std::cout << " type F read " << fvalue << std::endl;
}
else if (label.substr(0, 1) == "D")
{
infile >> dvalue;
dvaluecount++;
set_DoubleFlag(label.substr(1, label.size() - 1), dvalue);
std::cout << " type D read " << dvalue << std::endl;
}
else if (label.substr(0, 1) == "I")
{
infile >> ivalue;
ivaluecount++;
set_IntFlag(label.substr(1, label.size() - 1), ivalue);
std::cout << " type I read " << ivalue << std::endl;
}
else if (label.substr(0, 1) == "U")
{
infile >> uivalue;
uivaluecount++;
set_uint64Flag(label.substr(1, label.size() - 1), uivalue);
std::cout << " type U read " << uivalue << std::endl;
}
else
{
infile >> junk;
junkcount++;
std::cout << " Junk read " << junk << std::endl;
}
}
std::cout << "Read StringFlags(" << cvaluecount
<< ") FloatFlags(" << fvaluecount
<< ") DoubleFlags(" << dvaluecount
<< ") IntFlags(" << ivaluecount
<< ") uint64Flags(" << uivaluecount
<< ") JunkEntries(" << junkcount
<< ") from file " << name << std::endl;
infile.close();
}
void PHFlag::WriteToFile(const std::string &name)
{
std::ofstream outFile(name);
// loop over the map and write out the content
std::map<std::string, int>::const_iterator intiter;
for (intiter = m_IntFlagMap.begin(); intiter != m_IntFlagMap.end(); ++intiter)
{
outFile << "I" << intiter->first << "\t" << intiter->second << std::endl;
}
std::map<std::string, uint64_t>::const_iterator uintiter;
for (uintiter = m_UInt64FlagMap.begin(); uintiter != m_UInt64FlagMap.end(); ++uintiter)
{
outFile << "U" << uintiter->first << "\t" << uintiter->second << std::endl;
}
std::map<std::string, float>::const_iterator floatiter;
for (floatiter = m_FloatFlagMap.begin(); floatiter != m_FloatFlagMap.end(); ++floatiter)
{
outFile << "F" << floatiter->first << "\t" << floatiter->second << std::endl;
}
int oldprecision = outFile.precision(15);
std::map<std::string, double>::const_iterator doubleiter;
for (doubleiter = m_DoubleFlagMap.begin(); doubleiter != m_DoubleFlagMap.end(); ++doubleiter)
{
outFile << "D" << doubleiter->first << "\t" << doubleiter->second << std::endl;
}
outFile.precision(oldprecision);
std::map<std::string, std::string>::const_iterator chariter;
for (chariter = m_StringFlagMap.begin(); chariter != m_StringFlagMap.end(); ++chariter)
{
outFile << "S" << chariter->first << "\t" << chariter->second << std::endl;
}
outFile.close();
}
void PHFlag::PrintStackTrace() const
{
std::cout << "Called by #3 or #4 in this list: " << std::endl;
std::cout << boost::stacktrace::stacktrace();
std::cout << std::endl;
std::cout << "DO NOT PANIC - this is not a segfault" << std::endl;
}
void PHFlag::ClearFlag(const std::string &name)
{
std::map<std::string, int>::iterator iter = m_IntFlagMap.find(name);
if (iter != m_IntFlagMap.end())
{
m_IntFlagMap.erase(iter);
}
std::map<std::string, uint64_t>::iterator uiter = m_UInt64FlagMap.find(name);
if (uiter != m_UInt64FlagMap.end())
{
m_UInt64FlagMap.erase(uiter);
}
std::map<std::string, double>::iterator diter = m_DoubleFlagMap.find(name);
if (diter != m_DoubleFlagMap.end())
{
m_DoubleFlagMap.erase(diter);
}
std::map<std::string, float>::iterator fiter = m_FloatFlagMap.find(name);
if (fiter != m_FloatFlagMap.end())
{
m_FloatFlagMap.erase(fiter);
}
std::map<std::string, std::string>::iterator citer = m_StringFlagMap.find(name);
if (citer != m_StringFlagMap.end())
{
m_StringFlagMap.erase(citer);
}
}
void PHFlag::ClearAll()
{
m_UInt64FlagMap.clear();
m_IntFlagMap.clear();
m_DoubleFlagMap.clear();
m_FloatFlagMap.clear();
m_StringFlagMap.clear();
return;
}