forked from carlren/ORUtils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKeyValueConfig.h
executable file
·161 lines (137 loc) · 5.65 KB
/
KeyValueConfig.h
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
// Copyright 2014-2017 Oxford University Innovation Limited and the authors of InfiniTAM
#pragma once
#include <map>
#include <string.h>
namespace ORUtils {
/** Helper class for managing key-value like configuration information. Most
helpful for parsing strings.
*/
class KeyValueConfig {
public:
/** Just a helper for sorting... ignore unless you know what it's
about!
*/
struct ltstr {
bool operator()(const char* s1, const char* s2) const
{ return strcmp(s1, s2) < 0; }
};
/** This is a helper class to manage parsing something like
multiple-choice options within a KeyValueConfig.
*/
class ChoiceList {
public:
/** To create a choice list, you'll have to add options with
a name and a value each.
*/
void addChoice(const char *name, int value)
{ choices[name] = value; }
/** Given a choice in string format, this function returns
the corresponding value. If the choice is not available,
-1 is returned and success is set to false (if provided).
*/
int getValueForChoice(const char *choice, bool *success = NULL) const
{
List::const_iterator search = choices.find(choice);
if (search!=choices.end()) {
if (success!=NULL) *success = true;
return search->second;
} else {
if (success!=NULL) *success = false;
return -1;
}
}
/** This inverse search returns the textual description for
a given value. If the value is invalid, NULL is returned.
*/
const char* getChoiceForValue(int choice) const
{
for (List::const_iterator it = choices.begin(); it!=choices.end(); ++it) {
if (it->second == choice) {
return it->first;
}
}
return NULL;
}
/** This method lists the textual choices available in the list.
The porvided connector is placed in between each two of the
available options, but not at the beginning or end.
*/
void listChoices(const char *connector = ", ") const;
private:
typedef std::map<const char*,int,KeyValueConfig::ltstr> List;
List choices;
};
/** Constructor */
KeyValueConfig(void) {}
/** Constructor calling parseString(). */
KeyValueConfig(const char *str) { parseString(str); }
/** Copy constructor */
KeyValueConfig(const KeyValueConfig & src);
/** Destructor */
~KeyValueConfig(void);
/** @name Setting and Getting Values
@{ */
/** Set a property for the config. Will overwrite the existing value,
if the key is already present. Usually keys are case insensitive,
which is achieved by setting all keys to lower case.
*/
void setProperty(const char *key, const char *value, bool toLower=true);
/** Remove a property from the config. No problem, if the key is not
found.
*/
void unsetProperty(const char *key, bool toLower = true);
/** Remove all properties from the config. */
void unsetAllProperties(void);
/** Retrieve the value of a property or NULL, if the key is not used. */
const char* getProperty(const char *key, bool toLower = true) const;
/** Parse a string in the form of "key1=value1,key2=value2,..." */
bool parseString(const char *string, bool toLower = true);
/** @} */
/** @name Parse the Options in a KeyValueConfig
@{ */
/** This method parses the configuration option @p key which is
described in a lengthy, textual form as @p description. The
value stored in this KeyValueConfig is interpreted as a textual
key for the ChoiceList @p choices and the value, if provided,
is stored as @p opt_value. Finally, also the verbosity can be
controlled with @p verbose.
*/
void parseChoiceProperty(const char *key, const char *description, int & opt_value, const ChoiceList & choices, int verbose = -1) const;
/** This method parses the configuration option @p key which is
described in a lengthy, textual form as @p description. If the
key is present in this KeyValueConfig the value of @p opt_value
will be set to true, otherwise it will be set to false. Finally,
also the verbosity can be controlled with @p verbose.
*/
void parseBoolProperty(const char *key, const char *description, bool & opt_value, int verbose = -1) const;
/** This method parses the configuration option @p key which is
described in a lengthy, textual form as @p description. The
value stored in this KeyValueConfig under the given key is
interpreted as an integer option and, if provided, is stored
as @p opt_value. Finally, also the verbosity can be controlled
with @p verbose.
*/
void parseIntProperty(const char *key, const char *description, int & opt_value, int verbose = -1) const;
/** This method parses the configuration option @p key which is
described in a lengthy, textual form as @p description. The
value stored in this KeyValueConfig under the given key is
interpreted as an floating point option and, if provided, is stored
as @p opt_value. Finally, also the verbosity can be controlled
with @p verbose.
*/
void parseFltProperty(const char *key, const char *description, double & opt_value, int verbose = -1) const;
void parseFltProperty(const char *key, const char *description, float & opt_value, int verbose = -1) const;
/** This method parses the configuration option @p key which is
described in a lengthy, textual form as @p description. The
value stored in this KeyValueConfig under the given key is
expected as a raw string option and, if provided, is stored
as @p opt_value. Finally, also the verbosity can be controlled
with @p verbose.
*/
void parseStrProperty(const char *key, const char *description, const char* & opt_value, int verbose = -1) const;
/** @} */
private:
typedef std::map<char*,char*,ltstr> PropertyList;
PropertyList property;
};
}