-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties.h
230 lines (207 loc) · 5.76 KB
/
properties.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
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
#ifndef PROPERTIES_H
#define PROPERTIES_H
#include <map>
#include <string>
#include <vector>
class Properties: public std::map<std::string, std::string>
{
public:
bool HasProperty(const std::string &key) const
{
return find(key) != end();
}
void SetProperty(const char* key, bool intval)
{
SetProperty(std::string(key), std::to_string(intval));
}
void SetProperty(const char* key, int intval)
{
SetProperty(std::string(key), std::to_string(intval));
}
void SetProperty(const char* key, uint32_t val)
{
SetProperty(std::string(key), std::to_string(val));
}
void SetProperty(const char* key, uint64_t val)
{
SetProperty(std::string(key), std::to_string(val));
}
void SetProperty(const char* key, const char* val)
{
SetProperty(std::string(key), std::string(val));
}
void SetProperty(const std::string &key, const std::string &val)
{
insert(std::pair<std::string, std::string>(key, val));
}
void GetChildren(const std::string& path, Properties &children) const
{
//Create sarch string
std::string parent(path);
//Add the final .
parent += ".";
//For each property
for (const_iterator it = begin(); it != end(); ++it) {
const std::string &key = it->first;
//Check if it is from parent
if (key.compare(0, parent.length(), parent) == 0)
//INsert it
{
children.SetProperty(key.substr(parent.length(), key.length() - parent.length()), it->second);
}
}
}
void GetChildren(const char* path, Properties &children) const
{
GetChildren(std::string(path), children);
}
Properties GetChildren(const std::string& path) const
{
Properties properties;
//Get them
GetChildren(path, properties);
//Return
return properties;
}
Properties GetChildren(const char* path) const
{
Properties properties;
//Get them
GetChildren(path, properties);
//Return
return properties;
}
void GetChildrenArray(const char* path, std::vector<Properties> &array) const
{
//Create sarch string
std::string parent(path);
//Add the final .
parent += ".";
//Get array length
int length = GetProperty(parent + "length", 0);
//For each element
for (int i = 0; i < length; ++i) {
char index[64];
//Print string
snprintf(index, sizeof(index), "%d", i);
//And get children
array.push_back(GetChildren(parent + index));
}
}
const char* GetProperty(const char* key) const
{
return GetProperty(key, "");
}
std::string GetProperty(const char* key, const std::string defaultValue) const
{
//Find item
const_iterator it = find(std::string(key));
//If not found
if (it == end())
//return default
{
return defaultValue;
}
//Return value
return it->second;
}
std::string GetProperty(const std::string &key, const std::string defaultValue) const
{
//Find item
const_iterator it = find(key);
//If not found
if (it == end())
//return default
{
return defaultValue;
}
//Return value
return it->second;
}
const char* GetProperty(const char* key, const char *defaultValue) const
{
//Find item
const_iterator it = find(std::string(key));
//If not found
if (it == end())
//return default
{
return defaultValue;
}
//Return value
return it->second.c_str();
}
const char* GetProperty(const std::string &key, char *defaultValue) const
{
//Find item
const_iterator it = find(key);
//If not found
if (it == end())
//return default
{
return defaultValue;
}
//Return value
return it->second.c_str();
}
int GetProperty(const char* key, int defaultValue) const
{
return GetProperty(std::string(key), defaultValue);
}
int GetProperty(const std::string &key, int defaultValue) const
{
//Find item
const_iterator it = find(key);
//If not found
if (it == end())
//return default
{
return defaultValue;
}
//Return value
return atoi(it->second.c_str());
}
uint64_t GetProperty(const char* key, uint64_t defaultValue) const
{
return GetProperty(std::string(key), defaultValue);
}
uint64_t GetProperty(const std::string &key, uint64_t defaultValue) const
{
//Find item
const_iterator it = find(key);
//If not found
if (it == end())
//return default
{
return defaultValue;
}
//Return value
return atoll(it->second.c_str());
}
bool GetProperty(const char* key, bool defaultValue) const
{
return GetProperty(std::string(key), defaultValue);
}
bool GetProperty(const std::string &key, bool defaultValue) const
{
//Find item
const_iterator it = find(key);
//If not found
if (it == end())
//return default
{
return defaultValue;
}
//Get value
char * val = (char *)it->second.c_str();
//Check it
if (strcasecmp(val, (char *)"yes") == 0) {
return true;
} else if (strcasecmp(val, (char *)"true") == 0) {
return true;
}
//Return value
return (atoi(val));
}
};
#endif // PROPERTIES_H