File: options.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (537 lines) | stat: -rw-r--r-- 10,156 bytes parent folder | download | duplicates (4)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: options.C,v 1.27.14.2 2007/05/17 00:02:42 amoll Exp $ 
//

#include <BALL/DATATYPE/options.h>

#include <cstdlib>
#include <cerrno>
#include <cstdio>
#include <cmath>

#include <fstream>
#include <list>
#include <algorithm>

using namespace std;
using std::ofstream;
using std::ios;

namespace BALL 
{

	const Size Options::MAX_ENTRY_LENGTH = 1024;

	Options::Options()
		:	StringHashMap<String>(),
			name_("")
	{
	}

	Options::Options(const Options& options)
		:	StringHashMap<String>(options),
			name_(options.name_)
	{
	}

	Options::~Options()
	{
		clear();
	}

	void Options::addParameterDescription(const String& key, String description, ParamFile::ParameterType type, list<String>* allowed_values)
	{
		ParamFile::ParameterDescription td;
		td.name = key;
		td.description = description;
		td.type = type;
		if (allowed_values) td.allowed_values = *allowed_values;
		descriptions_.insert(make_pair(key, td));
	}

	const ParamFile::ParameterDescription* Options::getParameterDescription(const String& key) const
	{
		StringHashMap<ParamFile::ParameterDescription>::ConstIterator it = descriptions_.find(key);

		if (it == descriptions_.end())
		{
			return NULL;
		}
		else
		{
			return &it->second;
		}
	}

	Options* Options::createSubcategory(String name)
	{
		StringHashMap<Options*>::Iterator it = subcategories_.find(name);
		if (it == subcategories_.end())
		{
			Options* child_options = new Options;
			child_options->name_ = name;
			it = subcategories_.insert(make_pair(name, child_options)).first;
		}
		return it->second;
	}

	Options* Options::getSubcategory(String name)
	{
		StringHashMap<Options*>::Iterator it = subcategories_.find(name);
		if (it != subcategories_.end())
		{
			return it->second;
		}
		return NULL;
	}

	StringHashMap<Options*>::Iterator Options::beginSubcategories()
	{
		return subcategories_.begin();
	}

	StringHashMap<Options*>::Iterator Options::endSubcategories()
	{
		return subcategories_.end();
	}

	bool Options::isReal(const String& key) const
	{
		// an empty string is no real number
		const String& value = get(key);
		return (!value.isEmpty() && value.isFloat());
	}

	bool Options::isVector(const String& key) const 
	{
		// if the key does not exist - then the nonexistent value	
		// cannot contain a vector
		if (!has(key))
		{
			return false;
		}

		// try to interpret the string as three double values
		double dummy;
		return sscanf(get(key).c_str(), "(%lf %lf %lf)", &dummy, &dummy, &dummy) == 3;
	}

	bool Options::isBool(const String& key) const
	{
		String s = get(key);
		if (s == "")
		{
			return false;
		}

		s.toLower();
		return (s.compare("true") == 0 || s.compare("false") == 0);
	}
	
	bool Options::isSet(const String& key) const
	{
		return (StringHashMap<String>::find(key) != StringHashMap<String>::end());
	}
	
	bool Options::isInteger(const String& key) const 
	{
		// if it cannot be read as a floating point number
		// it cannot be an integer
		if (!isReal(key))
		{
			return false;
		}			
		
		// check wheter it is an integer
		long long_value = ::atol(get(key).c_str());
		double double_value   = ::atof(get(key).c_str());

		// check if it is an integer (cutoff is 1e-7)
		return fabs(double_value - ((double)long_value)) <= 1e-7;
	}


	double Options::getReal(const String& key) const
	{
		if (!has(key))
		{
			return 0.0;
		}
		double value;
		errno = 0;
		value = ::atof((*find(key)).second.c_str());
		
		if (errno == 0)
		{
			return value;
		} 
		else 
		{
			return 0.0;
		}
	}

	Vector3	Options::getVector(const String& key) const 
	{
		Vector3	h(0,0,0);
		
		if (!has(key))
		{
			return h;
		}
		
		// use temporary variables of double precision
		// this avoids trouble if the definition of 
		// Vector3 is changed between TVector3<float> and TVector3<double>
		double x, y, z;
		
		// try to interpret the option as a vector
		sscanf(get(key).c_str(), "(%lf %lf %lf)", &x, &y, &z);
		h.x = x; 
		h.y = y;
		h.z = z;

		return h;
	}


	bool Options::getBool(const String& key) const
	{
		if (!has(key))
		{
			return false;
		}

		ConstIterator it = find(key);
		return (it != end()) && (it->second == "true" || it->second == "1");
	}

	long Options::getInteger(const String& key) const
	{
		if (!has(key))
		{
			return 0;
		}

		long value;
		errno = 0;
		ConstIterator it = find(key);
		if (it == end())
		{
			return 0;
		}
		value = atol((*it).second.c_str());
		
		if (errno == 0)
		{
			return value;
		} 
		else 
		{
			errno = 0;
			return 0;
		}
	}

	void Options::set(const String& key, const String& value)
	{
		(*this)[key] = value;
	}

	void Options::setInteger(const String& key, const long value)
	{
		static char buffer[MAX_ENTRY_LENGTH + 1];
		sprintf(buffer, "%ld", value);			
		set(key, &(buffer[0]));
	}

	void Options::setReal(const String& key, const double value)
	{
		char buffer[MAX_ENTRY_LENGTH + 1];
		sprintf(buffer, "%f", value);
		set(key, buffer);
	}

	void Options::setVector(const String& key, const Vector3& value)
	{
		char buffer[MAX_ENTRY_LENGTH + 1];
		sprintf(buffer, "(%f %f %f)", value.x, value.y, value.z);
		set(key, buffer);
	}

	void Options::setBool(const String& key, const bool value)
	{
		if (value)
		{
			set(key, "true");
		}
		else
		{
			set(key, "false");
		}
	}

	String Options::setDefault(const String& key, const String& value)

	{
		if (!has(key))
		{
			set(key, value);
			return value;
		} 
		else 
		{
			return get(key);
		}
	}
		
	double Options::setDefaultReal(const String& key, const double value)
	{
		if (!has(key) || !isReal(key))
		{
			setReal(key, value);
			return value;
		} 
		else 
		{
			return getReal(key);
		}
	}

	bool Options::setDefaultBool(const String& key, const bool value)
	{
		if (!has(key) || !isBool(key))
		{
			setBool(key, value);
			return value;
		} 
		else 
		{
			return getBool(key);
		}
	}

	long Options::setDefaultInteger(const String& key, const long value)
	{
		if (!has(key) || !isInteger(key))
		{
			setInteger(key, value);
			return value;
		}
		else
		{
			return getInteger(key);
		}
	}

	void Options::setName(const String& name)
	{
		name_ = name;
	}

	const String& Options::getName() const
	{
		return name_;
	}

	String Options::get(const String& key) const
	{
		if (!has(key))
		{
			return "";
		}
		ConstIterator it = find(key);

		if (it == end())
		{
			return "";
		}
		else
		{
			return (*it).second;
		}
	}

	bool Options::readOptionFile(const String& filename)
	{
		ifstream	infile;
		infile.open(filename.c_str(), ios::in);
		if (!infile)
		{
			return false;
		}

		char		buffer[MAX_ENTRY_LENGTH + 1];
		String	s, key;
		while (infile.getline(buffer, MAX_ENTRY_LENGTH))
		{
			if ((buffer[0] != '#') && (buffer[0] != '!') && (buffer[0] != ';')) 
			{
				s = buffer;
				size_t i = 0;
				for(; i < s.length(); ++i) {
					if(s[i] == ' ' || s[i] == '\t') {
						break;
					}
				}

				key = s.left(i);

				while(i < s.length() && (s[i] == ' ' || s[i] == '\t')) ++i;

				if(i < s.length()) {
					s = s.getSubstring(i);
				} else {
					s = "";
				}

				set(key, s);
			}
		}
					
		infile.close();
		return true;
	}
		
	bool Options::writeOptionFile(const String& filename) const
	{
		std::list<String>		entry_list;
		String							entry;

		std::ofstream stream(filename.c_str());//, File::OUT);
		if (!stream.is_open())
		{
			return false;
		}

		stream << "![OptionsTable: " << getName() << " (" << size() << " entries)]" << endl;

		StringHashMap<String>::ConstIterator	it(begin());
		for(; !(it == end()); ++it)
		{
			entry = (*it).first + ' ' + (*it).second;
			entry_list.push_back(entry);
		}

		entry_list.sort();

		std::list<String>::iterator	list_it = entry_list.begin();
		for (; list_it != entry_list.end(); ++list_it) 
		{
	  	stream << *list_it << endl;
		}

		stream << "!-----------------------------------" << endl;
		
		stream.close();
		entry_list.clear();
		return true;
	}

	void Options::write(PersistenceManager& pm) const
	{
		std::list<String>		entry_list;
		String							entry;

		pm.writePrimitive(name_, "name_");
		pm.writePrimitive(size(), "size");

		StringHashMap<String>::ConstIterator it(begin());
		for (; it != end(); ++it)
		{
			entry = it->first + ' ' + it->second;
			entry_list.push_back(entry);
		}

		entry_list.sort();
		std::list<String>::iterator	list_it = entry_list.begin();
		for (; list_it != entry_list.end(); ++list_it) 
		{
	  	pm.writePrimitive(*list_it, "key/value");
		}
	}
	
	bool Options::read(PersistenceManager& pm)
	{
		clear();
		bool success = pm.readPrimitive(name_, "name_");
		Size size;
		success &= pm.readPrimitive(size, "size");

		String line;
		for (Size i=0; (i<size) && success; i++)
		{
			success &= pm.readPrimitive(line, "key/value");
			set(line.getField(0, " "), line.after(" "));
		}	

		return success;
	}

	void Options::dump(ostream& stream, Size /* depth */) const
	{
		std::list<String>		entry_list;
		String							entry;

		stream << "[OptionsTable: " << getName() << " (" << size() << " entries)]" << endl;

		StringHashMap<String>::ConstIterator	it(begin());
		for(; !(it == end()); ++it)
		{
			entry = (*it).first + ' ' + (*it).second;
			entry_list.push_back(entry);
		}

		entry_list.sort();

		std::list<String>::iterator	list_it = entry_list.begin();
		for (; list_it != entry_list.end(); ++list_it) 
		{
	  	stream << *list_it << endl;
		}

		stream << "-----------------------------------" << endl;

		entry_list.clear();
	}

	const Options& Options::operator = (const Options& options)
	{
		StringHashMap<String>::operator = (options);
		name_ = options.name_;

		return *this;
	}

	bool Options::operator == (const Options& option) const 
	{
		if (this->name_ != option.name_)
		{
			return false;
		}
		
		return StringHashMap<String>::operator == (option);
	}

	bool Options::operator != (const Options& option) const 
	{
		return !(*this == option);
	}

	void Options::clear()
	{
		name_ = "";
		StringHashMap<String>::clear();
		descriptions_.clear();

		for(StringHashMap<Options*>::iterator it = subcategories_.begin(); it != subcategories_.end(); it++)
		{
			delete it->second;
		}
		subcategories_.clear();
	}


} // namespace BALL