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
|
// Copyright 2008 Nanorex, Inc. See LICENSE file for details.
#include "Nanorex/Utility/NXProperties.h"
namespace Nanorex {
/* CONSTRUCTOR */
/**
* Constructs an empty NXProperties object.
*/
NXProperties::NXProperties() {
}
/* DESTRUCTOR */
NXProperties::~NXProperties() {
}
/* FUNCTION: writeToFile */
/**
* Writes these properties to the given file. The format of the file is as
* follows:
*
\code
key1=value1
key2=value2
...
\endcode
*
* @return If the given file was successfully opened and completely written, an
* empty string ("") is returned. Otherwise, an error message is returned.
*/
std::string NXProperties::writeToFile(const std::string& filename) {
std::string errors = "";
// Open file for reading.
std::ofstream outFile(filename.c_str(), std::ios::out);
if (!outFile) {
errors = "File could not be opened: " + filename + ".";
} else {
std::string line;
std::map<std::string, std::string>::iterator iter;
for (iter = properties.begin(); iter != properties.end(); iter++) {
line = iter->first;
line += "=";
line += iter->second;
outFile << line << std::endl;
}
outFile.close();
}
return errors;
}
/* FUNCTION: readFromFile */
/**
* Opens the given file and reads in the properties it contains. The format
* of the file is as follows:
*
\code
# Comment
key1=value1
key2=value2
...
\endcode
*
* Comments and blank lines are ignored as are white space around keys and
* values.
*
* @return If the given file was successfully opened and completely read, an
* empty string ("") is returned. Otherwise, an error message is returned if
* any line in the input file is malformed. The whole file is read and parsed
* and all good/well-formed lines are incorporated into this set of properties.
*/
std::string NXProperties::readFromFile(const std::string& filename) {
std::string errors = "";
// Open file for reading.
std::ifstream inFile(filename.c_str(), std::ios::in);
if (!inFile) {
errors = "File could not be opened: " + filename + ".";
} else {
// Read properties.
std::string line;
while (std::getline(inFile, line, '\n')) {
try {
deSerializeLine(line);
} catch (...) {
errors += " Bad line: " + line;
}
}
inFile.close();
}
return errors;
}
/* FUNCTION: setProperty */
/**
* Sets the given key's value.
*/
void NXProperties::setProperty(const std::string& key,
const std::string& value) {
properties[key] = value;
}
/* FUNCTION; addProperties */
/**
* Adds the given Properties to this Properties object, prepending the key of
* each new key/value pair with the given string. Any existing properties will
* be over-ridden by those found in the new Properties.
*
* @param newProps
* The new key/value pairs to add.
* @param prePendChars
* The string to prepend to each new key/value pair's key.
*/
void NXProperties::addProperties(NXProperties* newProps,
const char* prePendChars) {
std::string prePendString = std::string(prePendChars);
std::map<std::string, std::string>::iterator newPropsIter;
for (newPropsIter = newProps->properties.begin();
newPropsIter != newProps->properties.end();
newPropsIter++) {
properties[prePendString + newPropsIter->first] = newPropsIter->second;
}
}
/* FUNCTION: keyExists */
bool NXProperties::keyExists(const char* key) {
return (properties.count(key) != 0);
}
/* FUNCTION: getProperty */
/**
* Returns the value of the given key, or an empty string if the key is not
* found.
*/
const char* NXProperties::getProperty(const char* key) {
const char* value = properties[std::string(key)].c_str();
return value;
}
/**
* Returns the value of the given key, or an empty string if the key is not
* found.
*/
const char* NXProperties::getProperty(const std::string& key) {
const char* value = properties[key].c_str();
return value;
}
/**
* Returns the value of the given key with an optional default value if the key
* is not found.
*/
const char* NXProperties::getProperty(const char* key,
const char* defaultValue) {
if (properties.count(key) == 0)
return defaultValue;
else
return getProperty(key);
}
/**
* Returns the value of the given key with an optional default value if the key
* is not found.
*/
const char* NXProperties::getProperty(const std::string& key,
const char* defaultValue) {
if (properties.count(key) == 0)
return defaultValue;
else
return getProperty(key);
}
/* FUNCTION: getPropertyKeys */
/**
* Returns all the keys in a vector.
*/
std::vector<std::string> NXProperties::getPropertyKeys() {
std::vector<std::string> keys;
std::map<std::string, std::string>::iterator iter = properties.begin();
std::map<std::string, std::string>::iterator iterEnd = properties.end();
while (iter != iterEnd) {
keys.push_back(iter->first);
iter++;
}
return keys;
}
/* FUNCTION: clear */
/**
* Clears all properties.
*/
void NXProperties::clear() {
properties.clear();
}
/* FUNCTION: deSerializeLine */
void NXProperties::deSerializeLine(const std::string& dataLine) {
std::string key, value;
std::string line = dataLine;
// Trim front.
while ((line.length() != 0) && (line[0] == ' '))
line.erase(0, 1);
// Ignore commented and blank lines
if ((line.length() == 0) || (line[0] == '#'))
return;
// Get key/value pair
unsigned int index = line.find("=", 1);
if (index != std::string::npos) {
key = line.substr(0, index);
// Trim end.
while ((key.length() != 0) && (key[key.length() - 1] == ' '))
key.erase(key.length() - 1, 1);
value = line.substr(index + 1, line.length() - 1);
//Trim front.
while ((value.length() != 0) && (value[0] == ' '))
value.erase(0, 1);
// Trim end.
while ((value.length() != 0) && (value[value.length() - 1] == ' '))
value.erase(value.length() - 1, 1);
// Add to table
properties[key] = value;
}
}
} // Nanorex::
|