1 | #include <fstream> |
---|
2 | #include <string> |
---|
3 | #include "ConfigValueContainer.h" |
---|
4 | |
---|
5 | //#define CONFIGFILEPATH "O:\\oh\\bin\\orxonox.ini" |
---|
6 | #define CONFIGFILEPATH "orxonox.ini" |
---|
7 | |
---|
8 | namespace orxonox |
---|
9 | { |
---|
10 | std::list<std::string>* ConfigValueContainer::configFileLines_s = 0; |
---|
11 | bool ConfigValueContainer::readConfigFile_s = false; |
---|
12 | |
---|
13 | ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, int defvalue) |
---|
14 | { |
---|
15 | std::ostringstream ostream; |
---|
16 | |
---|
17 | if (ostream << defvalue) |
---|
18 | this->defvalue_ = ostream.str(); |
---|
19 | else |
---|
20 | this->defvalue_ = "0"; |
---|
21 | |
---|
22 | this->setDefaultValues(classname, varname); |
---|
23 | std::string valueString = this->getValueString(); |
---|
24 | |
---|
25 | std::istringstream istream(valueString); |
---|
26 | if (!(istream >> this->value_int_)) |
---|
27 | { |
---|
28 | this->value_int_ = defvalue; |
---|
29 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
30 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, double defvalue) |
---|
35 | { |
---|
36 | std::ostringstream ostream; |
---|
37 | |
---|
38 | if (ostream << defvalue) |
---|
39 | this->defvalue_ = ostream.str(); |
---|
40 | else |
---|
41 | this->defvalue_ = "0.000000"; |
---|
42 | |
---|
43 | this->setDefaultValues(classname, varname); |
---|
44 | std::string valueString = this->getValueString(); |
---|
45 | |
---|
46 | std::istringstream istream(valueString); |
---|
47 | if (!(istream >> this->value_double_)) |
---|
48 | { |
---|
49 | this->value_double_ = defvalue; |
---|
50 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
51 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, bool defvalue) |
---|
56 | { |
---|
57 | if (defvalue) |
---|
58 | this->defvalue_ = "true"; |
---|
59 | else |
---|
60 | this->defvalue_ = "false"; |
---|
61 | |
---|
62 | this->setDefaultValues(classname, varname); |
---|
63 | std::string valueString = this->getValueString(); |
---|
64 | |
---|
65 | if (valueString.find("true") < valueString.size() || valueString.find("yes") < valueString.size()) |
---|
66 | this->value_bool_ = true; |
---|
67 | else if (valueString.find("false") < valueString.size() || valueString.find("no") < valueString.size()) |
---|
68 | this->value_bool_ = false; |
---|
69 | else |
---|
70 | { |
---|
71 | std::istringstream istream(valueString); |
---|
72 | if (!(istream >> this->value_bool_)) |
---|
73 | { |
---|
74 | this->value_bool_ = defvalue; |
---|
75 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
76 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue) |
---|
82 | { |
---|
83 | this->defvalue_ = defvalue; |
---|
84 | this->setDefaultValues(classname, varname); |
---|
85 | this->value_string_ = this->getValueString(false); |
---|
86 | } |
---|
87 | |
---|
88 | ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector3 defvalue) |
---|
89 | { |
---|
90 | std::ostringstream ostream; |
---|
91 | if (ostream << "(" << defvalue.x << "," << defvalue.y << "," << defvalue.z << ")") |
---|
92 | this->defvalue_ = ostream.str(); |
---|
93 | else |
---|
94 | this->defvalue_ = "(0,0,0)"; |
---|
95 | |
---|
96 | this->setDefaultValues(classname, varname); |
---|
97 | std::string valueString = this->getValueString(); |
---|
98 | |
---|
99 | unsigned int pos; |
---|
100 | while ((pos = valueString.find(" ")) < valueString.length()) |
---|
101 | valueString.erase(pos, 1); |
---|
102 | while ((pos = valueString.find("(")) < valueString.length()) |
---|
103 | valueString.erase(pos, 1); |
---|
104 | while ((pos = valueString.find(")")) < valueString.length()) |
---|
105 | valueString.erase(pos, 1); |
---|
106 | while ((pos = valueString.find(",")) < valueString.length()) |
---|
107 | valueString.replace(pos, 1, " "); |
---|
108 | |
---|
109 | std::istringstream istream(valueString); |
---|
110 | |
---|
111 | if (!(istream >> this->value_vector3_.x)) |
---|
112 | { |
---|
113 | this->value_vector3_.x = defvalue.x; |
---|
114 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
115 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
116 | } |
---|
117 | if (!(istream >> this->value_vector3_.y)) |
---|
118 | { |
---|
119 | this->value_vector3_.y = defvalue.y; |
---|
120 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
121 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
122 | } |
---|
123 | if (!(istream >> this->value_vector3_.z)) |
---|
124 | { |
---|
125 | this->value_vector3_.z = defvalue.z; |
---|
126 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
127 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::ColourValue defvalue) |
---|
132 | { |
---|
133 | std::ostringstream ostream; |
---|
134 | if (ostream << "(" << defvalue.r << "," << defvalue.g << "," << defvalue.b << "," << defvalue.a << ")") |
---|
135 | this->defvalue_ = ostream.str(); |
---|
136 | else |
---|
137 | this->defvalue_ = "(0,0,0,0)"; |
---|
138 | |
---|
139 | this->setDefaultValues(classname, varname); |
---|
140 | std::string valueString = this->getValueString(); |
---|
141 | |
---|
142 | unsigned int pos; |
---|
143 | while ((pos = valueString.find(" ")) < valueString.length()) |
---|
144 | valueString.erase(pos, 1); |
---|
145 | while ((pos = valueString.find("(")) < valueString.length()) |
---|
146 | valueString.erase(pos, 1); |
---|
147 | while ((pos = valueString.find(")")) < valueString.length()) |
---|
148 | valueString.erase(pos, 1); |
---|
149 | while ((pos = valueString.find(",")) < valueString.length()) |
---|
150 | valueString.replace(pos, 1, " "); |
---|
151 | |
---|
152 | std::istringstream istream(valueString); |
---|
153 | |
---|
154 | if (!(istream >> this->value_colourvalue_.r)) |
---|
155 | { |
---|
156 | this->value_colourvalue_.r = defvalue.r; |
---|
157 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
158 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
159 | } |
---|
160 | if (!(istream >> this->value_colourvalue_.g)) |
---|
161 | { |
---|
162 | this->value_colourvalue_.g = defvalue.g; |
---|
163 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
164 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
165 | } |
---|
166 | if (!(istream >> this->value_colourvalue_.b)) |
---|
167 | { |
---|
168 | this->value_colourvalue_.b = defvalue.b; |
---|
169 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
170 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
171 | } |
---|
172 | if (!(istream >> this->value_colourvalue_.a)) |
---|
173 | { |
---|
174 | this->value_colourvalue_.a = defvalue.a; |
---|
175 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; |
---|
176 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | void ConfigValueContainer::setDefaultValues(const std::string& classname, const std::string& varname) |
---|
181 | { |
---|
182 | this->classname_ = classname; |
---|
183 | this->varname_ = varname; |
---|
184 | |
---|
185 | this->searchConfigFileLine(); |
---|
186 | |
---|
187 | this->value_int_ = 0; |
---|
188 | this->value_double_ = 0.000000; |
---|
189 | this->value_bool_ = false; |
---|
190 | this->value_string_ = ""; |
---|
191 | this->value_vector3_ = Ogre::Vector3(0, 0, 0); |
---|
192 | this->value_colourvalue_ = Ogre::ColourValue(0, 0, 0, 0); |
---|
193 | } |
---|
194 | |
---|
195 | void ConfigValueContainer::searchConfigFileLine() |
---|
196 | { |
---|
197 | if (!ConfigValueContainer::readConfigFile_s) |
---|
198 | ConfigValueContainer::readConfigFile(CONFIGFILEPATH); |
---|
199 | |
---|
200 | this->configFileLine_ = 0; |
---|
201 | |
---|
202 | std::string section = ""; |
---|
203 | section.append("["); |
---|
204 | section.append(this->classname_); |
---|
205 | section.append("]"); |
---|
206 | |
---|
207 | bool success = false; |
---|
208 | std::list<std::string>::iterator it1; |
---|
209 | for(it1 = ConfigValueContainer::configFileLines_s->begin(); it1 != ConfigValueContainer::configFileLines_s->end(); ++it1) |
---|
210 | { |
---|
211 | if (this->isComment(*it1)) |
---|
212 | continue; |
---|
213 | |
---|
214 | if ((*it1).find(section) < (*it1).length()) |
---|
215 | { |
---|
216 | bool bLineIsEmpty = false; |
---|
217 | std::list<std::string>::iterator positionToPutNewLineAt; |
---|
218 | |
---|
219 | std::list<std::string>::iterator it2; |
---|
220 | for(it2 = ++it1; it2 != ConfigValueContainer::configFileLines_s->end(); ++it2) |
---|
221 | { |
---|
222 | if (this->isComment(*it2)) |
---|
223 | continue; |
---|
224 | |
---|
225 | if (this->isEmpty(*it2)) |
---|
226 | { |
---|
227 | if (!bLineIsEmpty) |
---|
228 | { |
---|
229 | bLineIsEmpty = true; |
---|
230 | positionToPutNewLineAt = it2; |
---|
231 | } |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | if (!bLineIsEmpty) |
---|
236 | positionToPutNewLineAt = it2; |
---|
237 | |
---|
238 | bLineIsEmpty = false; |
---|
239 | } |
---|
240 | |
---|
241 | unsigned int open = (*it2).find("["); |
---|
242 | unsigned int close = (*it2).find("]"); |
---|
243 | if ((open < (*it2).length()) && (close < (*it2).length()) && (open < close)) |
---|
244 | { |
---|
245 | this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalue_); |
---|
246 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
247 | success = true; |
---|
248 | break; |
---|
249 | } |
---|
250 | |
---|
251 | if ((*it2).find(this->varname_) < (*it2).length()) |
---|
252 | { |
---|
253 | this->configFileLine_ = it2; |
---|
254 | success = true; |
---|
255 | break; |
---|
256 | } |
---|
257 | } |
---|
258 | if (!success) |
---|
259 | { |
---|
260 | this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalue_); |
---|
261 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
262 | success = true; |
---|
263 | } |
---|
264 | break; |
---|
265 | } |
---|
266 | } |
---|
267 | if (!success) |
---|
268 | { |
---|
269 | this->configFileLines_s->push_back("[" + this->classname_ + "]"); |
---|
270 | this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalue_); |
---|
271 | this->configFileLine_ = --this->configFileLines_s->end(); |
---|
272 | success = true; |
---|
273 | this->configFileLines_s->push_back(""); |
---|
274 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | bool ConfigValueContainer::isComment(const std::string& line) |
---|
279 | { |
---|
280 | std::string teststring = getStrippedLine(line); |
---|
281 | |
---|
282 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/')) |
---|
283 | return true; |
---|
284 | |
---|
285 | return false; |
---|
286 | } |
---|
287 | |
---|
288 | bool ConfigValueContainer::isEmpty(const std::string& line) |
---|
289 | { |
---|
290 | return getStrippedLine(line) == ""; |
---|
291 | } |
---|
292 | |
---|
293 | std::string ConfigValueContainer::getStrippedLine(const std::string& line) |
---|
294 | { |
---|
295 | std::string output = line; |
---|
296 | unsigned int pos; |
---|
297 | while ((pos = output.find(" ")) < output.length()) |
---|
298 | output.erase(pos, 1); |
---|
299 | |
---|
300 | return output; |
---|
301 | } |
---|
302 | |
---|
303 | std::string ConfigValueContainer::getValueString(bool bStripped) |
---|
304 | { |
---|
305 | std::string output; |
---|
306 | if (bStripped) |
---|
307 | output = this->getStrippedLine(*this->configFileLine_); |
---|
308 | else |
---|
309 | output = *this->configFileLine_; |
---|
310 | |
---|
311 | return output.substr(output.find("=") + 1); |
---|
312 | } |
---|
313 | |
---|
314 | void ConfigValueContainer::readConfigFile(const std::string& filename) |
---|
315 | { |
---|
316 | ConfigValueContainer::readConfigFile_s = true; |
---|
317 | |
---|
318 | if (!ConfigValueContainer::configFileLines_s) |
---|
319 | ConfigValueContainer::configFileLines_s = new std::list<std::string>; |
---|
320 | |
---|
321 | std::ofstream createFile; |
---|
322 | createFile.open(filename.c_str(), std::fstream::app); |
---|
323 | createFile.close(); |
---|
324 | |
---|
325 | std::ifstream file; |
---|
326 | file.open(filename.c_str(), std::fstream::in); |
---|
327 | |
---|
328 | char line[1024]; |
---|
329 | |
---|
330 | while (file.good() && !file.eof()) |
---|
331 | { |
---|
332 | file.getline(line, 1024); |
---|
333 | ConfigValueContainer::configFileLines_s->push_back(line); |
---|
334 | // std::cout << "### ->" << line << "<- : empty: " << isEmpty(line) << " comment: " << isComment(line) << std::endl; |
---|
335 | } |
---|
336 | |
---|
337 | ConfigValueContainer::configFileLines_s->pop_back(); |
---|
338 | |
---|
339 | if ((ConfigValueContainer::configFileLines_s->size() > 0) && !isEmpty(*ConfigValueContainer::configFileLines_s->rbegin())) |
---|
340 | { |
---|
341 | // std::cout << "### newline added" << std::endl; |
---|
342 | ConfigValueContainer::configFileLines_s->push_back(""); |
---|
343 | } |
---|
344 | |
---|
345 | file.close(); |
---|
346 | } |
---|
347 | |
---|
348 | void ConfigValueContainer::writeConfigFile(const std::string& filename) |
---|
349 | { |
---|
350 | ConfigValueContainer::readConfigFile_s = true; |
---|
351 | |
---|
352 | if (!ConfigValueContainer::configFileLines_s) |
---|
353 | ConfigValueContainer::configFileLines_s = new std::list<std::string>; |
---|
354 | |
---|
355 | std::ofstream createFile; |
---|
356 | createFile.open(filename.c_str(), std::fstream::app); |
---|
357 | createFile.close(); |
---|
358 | |
---|
359 | std::ofstream file; |
---|
360 | file.open(filename.c_str(), std::fstream::out); |
---|
361 | |
---|
362 | std::list<std::string>::iterator it; |
---|
363 | for(it = ConfigValueContainer::configFileLines_s->begin(); it != ConfigValueContainer::configFileLines_s->end(); ++it) |
---|
364 | { |
---|
365 | file << (*it) << std::endl; |
---|
366 | } |
---|
367 | |
---|
368 | file.close(); |
---|
369 | } |
---|
370 | } |
---|