[513] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
[670] | 22 | * Fabian 'x3n' Landau |
---|
[513] | 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[871] | 28 | /** |
---|
| 29 | @file ConfigValueContainer.cc |
---|
| 30 | @brief Implementation of the ConfigValueContainer class. |
---|
| 31 | */ |
---|
| 32 | |
---|
[497] | 33 | #include <fstream> |
---|
[682] | 34 | |
---|
[871] | 35 | #include "ConfigValueContainer.h" |
---|
[742] | 36 | #include "util/Tokenizer.h" |
---|
| 37 | #include "util/Convert.h" |
---|
[871] | 38 | #include "Language.h" |
---|
[497] | 39 | |
---|
| 40 | #define CONFIGFILEPATH "orxonox.ini" |
---|
| 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | /** |
---|
[667] | 45 | @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable. |
---|
| 46 | @param value This is only needed to determine the right type. |
---|
[497] | 47 | @param classname The name of the class the variable belongs to |
---|
| 48 | @param varname The name of the variable |
---|
| 49 | @param defvalue The default-value |
---|
| 50 | */ |
---|
[871] | 51 | ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, MultiTypeMath defvalue) |
---|
[497] | 52 | { |
---|
[705] | 53 | this->bAddedDescription_ = false; |
---|
[667] | 54 | this->classname_ = classname; |
---|
| 55 | this->varname_ = varname; |
---|
[497] | 56 | |
---|
[871] | 57 | this->valueToString(&this->defvalueString_, defvalue); // Try to convert the default-value to a string |
---|
[703] | 58 | this->searchConfigFileLine(); // Search the entry in the config-file |
---|
[497] | 59 | |
---|
[871] | 60 | std::string valueString = this->parseValueString(!(defvalue.isA(MT_string) || defvalue.isA(MT_constchar))); // Parses the value string from the config-file-entry |
---|
| 61 | if (!this->parseString(valueString, defvalue)) // Try to convert the string to a value |
---|
[703] | 62 | this->resetConfigFileEntry(); // The conversion failed |
---|
[497] | 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
[871] | 66 | @brief Converts a value to a string. |
---|
| 67 | @param output The string to write to |
---|
| 68 | @param input The value to convert |
---|
| 69 | @return True if the converson was successful |
---|
[497] | 70 | */ |
---|
[871] | 71 | bool ConfigValueContainer::valueToString(std::string* output, MultiTypeMath& input) |
---|
[667] | 72 | { |
---|
[871] | 73 | if (input.getType() == MT_int) |
---|
| 74 | return ConvertValue(output, input.getInt(), std::string("0")); |
---|
| 75 | else if (input.getType() == MT_uint) |
---|
| 76 | return ConvertValue(output, input.getUnsignedInt(), std::string("0")); |
---|
| 77 | else if (input.getType() == MT_char) |
---|
| 78 | return ConvertValue(output, (int)input.getChar(), std::string("0")); |
---|
| 79 | else if (input.getType() == MT_uchar) |
---|
| 80 | return ConvertValue(output, (unsigned int)input.getUnsignedChar(), std::string("0")); |
---|
| 81 | else if (input.getType() == MT_short) |
---|
| 82 | return ConvertValue(output, input.getShort(), std::string("0")); |
---|
| 83 | else if (input.getType() == MT_ushort) |
---|
| 84 | return ConvertValue(output, input.getUnsignedShort(), std::string("0")); |
---|
| 85 | else if (input.getType() == MT_long) |
---|
| 86 | return ConvertValue(output, input.getLong(), std::string("0")); |
---|
| 87 | else if (input.getType() == MT_ulong) |
---|
| 88 | return ConvertValue(output, input.getUnsignedLong(), std::string("0")); |
---|
| 89 | else if (input.getType() == MT_float) |
---|
| 90 | return ConvertValue(output, input.getFloat(), std::string("0.000000")); |
---|
| 91 | else if (input.getType() == MT_double) |
---|
| 92 | return ConvertValue(output, input.getDouble(), std::string("0.000000")); |
---|
| 93 | else if (input.getType() == MT_longdouble) |
---|
| 94 | return ConvertValue(output, input.getChar(), std::string("0.000000")); |
---|
| 95 | else if (input.getType() == MT_bool) |
---|
| 96 | { |
---|
| 97 | if (input.getBool()) |
---|
| 98 | (*output) = "true"; |
---|
| 99 | else |
---|
| 100 | (*output) = "false"; |
---|
[667] | 101 | |
---|
[871] | 102 | return true; |
---|
| 103 | } |
---|
| 104 | else if (input.getType() == MT_constchar) |
---|
| 105 | { |
---|
| 106 | (*output) = "\"" + input.getString() + "\""; |
---|
| 107 | return true; |
---|
| 108 | } |
---|
| 109 | else if (input.getType() == MT_string) |
---|
| 110 | { |
---|
| 111 | (*output) = "\"" + input.getString() + "\""; |
---|
| 112 | return true; |
---|
| 113 | } |
---|
| 114 | else if (input.getType() == MT_vector2) |
---|
| 115 | { |
---|
| 116 | std::ostringstream ostream; |
---|
| 117 | if (ostream << "(" << input.getVector2().x << "," << input.getVector2().y << ")") |
---|
| 118 | { |
---|
| 119 | (*output) = ostream.str(); |
---|
| 120 | return true; |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | (*output) = "(0,0)"; |
---|
| 125 | return false; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | else if (input.getType() == MT_vector3) |
---|
| 129 | { |
---|
| 130 | std::ostringstream ostream; |
---|
| 131 | if (ostream << "(" << input.getVector3().x << "," << input.getVector3().y << "," << input.getVector3().z << ")") |
---|
| 132 | { |
---|
| 133 | (*output) = ostream.str(); |
---|
| 134 | return true; |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | (*output) = "(0,0,0)"; |
---|
| 139 | return false; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | else if (input.getType() == MT_colourvalue) |
---|
| 143 | { |
---|
| 144 | std::ostringstream ostream; |
---|
| 145 | if (ostream << "(" << input.getColourValue().r << "," << input.getColourValue().g << "," << input.getColourValue().b << "," << input.getColourValue().a << ")") |
---|
| 146 | { |
---|
| 147 | (*output) = ostream.str(); |
---|
| 148 | return true; |
---|
| 149 | } |
---|
| 150 | else |
---|
| 151 | { |
---|
| 152 | (*output) = "(0,0,0,0)"; |
---|
| 153 | return false; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | else if (input.getType() == MT_quaternion) |
---|
| 157 | { |
---|
| 158 | std::ostringstream ostream; |
---|
| 159 | if (ostream << "(" << input.getQuaternion().w << "," << input.getQuaternion().x << "," << input.getQuaternion().y << "," << input.getQuaternion().z << ")") |
---|
| 160 | { |
---|
| 161 | (*output) = ostream.str(); |
---|
| 162 | return true; |
---|
| 163 | } |
---|
| 164 | else |
---|
| 165 | { |
---|
| 166 | (*output) = "(0,0,0,0)"; |
---|
| 167 | return false; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | else if (input.getType() == MT_radian) |
---|
| 171 | return ConvertValue(output, input.getRadian(), std::string("0.000000")); |
---|
| 172 | else if (input.getType() == MT_degree) |
---|
| 173 | return ConvertValue(output, input.getDegree(), std::string("0.000000")); |
---|
[667] | 174 | |
---|
[871] | 175 | return false; |
---|
[667] | 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
[871] | 179 | @brief Parses a given std::string into a value of the type of the associated variable and assigns it. |
---|
| 180 | @param input The string to convert |
---|
| 181 | @return True if the string was successfully parsed |
---|
[667] | 182 | */ |
---|
[947] | 183 | bool ConfigValueContainer::parseString(const std::string& input, const MultiTypeMath& defvalue) |
---|
[667] | 184 | { |
---|
[871] | 185 | if (defvalue.getType() == MT_int) |
---|
| 186 | return this->parseString(input, defvalue.getInt()); |
---|
| 187 | else if (defvalue.getType() == MT_uint) |
---|
| 188 | return this->parseString(input, defvalue.getUnsignedInt()); |
---|
| 189 | else if (defvalue.getType() == MT_char) |
---|
| 190 | return this->parseString(input, defvalue.getChar()); |
---|
| 191 | else if (defvalue.getType() == MT_uchar) |
---|
| 192 | return this->parseString(input, defvalue.getUnsignedChar()); |
---|
| 193 | else if (defvalue.getType() == MT_short) |
---|
| 194 | return this->parseString(input, defvalue.getShort()); |
---|
| 195 | else if (defvalue.getType() == MT_ushort) |
---|
| 196 | return this->parseString(input, defvalue.getUnsignedShort()); |
---|
| 197 | else if (defvalue.getType() == MT_long) |
---|
| 198 | return this->parseString(input, defvalue.getLong()); |
---|
| 199 | else if (defvalue.getType() == MT_ulong) |
---|
| 200 | return this->parseString(input, defvalue.getUnsignedLong()); |
---|
| 201 | else if (defvalue.getType() == MT_float) |
---|
| 202 | return this->parseString(input, defvalue.getFloat()); |
---|
| 203 | else if (defvalue.getType() == MT_double) |
---|
| 204 | return this->parseString(input, defvalue.getDouble()); |
---|
| 205 | else if (defvalue.getType() == MT_longdouble) |
---|
| 206 | return this->parseString(input, defvalue.getLongDouble()); |
---|
| 207 | else if (defvalue.getType() == MT_bool) |
---|
| 208 | return this->parseString(input, defvalue.getBool()); |
---|
| 209 | else if (defvalue.getType() == MT_constchar) |
---|
| 210 | return this->parseString(input, defvalue.getString()); |
---|
| 211 | else if (defvalue.getType() == MT_string) |
---|
| 212 | return this->parseString(input, defvalue.getString()); |
---|
| 213 | else if (defvalue.getType() == MT_vector2) |
---|
| 214 | return this->parseString(input, defvalue.getVector2()); |
---|
| 215 | else if (defvalue.getType() == MT_vector3) |
---|
| 216 | return this->parseString(input, defvalue.getVector3()); |
---|
| 217 | else if (defvalue.getType() == MT_colourvalue) |
---|
| 218 | return this->parseString(input, defvalue.getColourValue()); |
---|
| 219 | else if (defvalue.getType() == MT_quaternion) |
---|
| 220 | return this->parseString(input, defvalue.getQuaternion()); |
---|
| 221 | else if (defvalue.getType() == MT_radian) |
---|
| 222 | return this->parseString(input, defvalue.getRadian()); |
---|
| 223 | else if (defvalue.getType() == MT_degree) |
---|
| 224 | return this->parseString(input, defvalue.getDegree()); |
---|
[667] | 225 | |
---|
[871] | 226 | return false; |
---|
[667] | 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
[871] | 230 | @brief Parses a given std::string into a value of the type int and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
| 231 | @param input The string to convert |
---|
[667] | 232 | @param defvalue The default-value |
---|
[871] | 233 | @return True if the string was successfully parsed |
---|
[667] | 234 | */ |
---|
[871] | 235 | bool ConfigValueContainer::parseString(const std::string& input, int defvalue) |
---|
[667] | 236 | { |
---|
[871] | 237 | int temp; |
---|
| 238 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 239 | this->value_.setValue(temp); |
---|
| 240 | return success; |
---|
[667] | 241 | } |
---|
| 242 | |
---|
| 243 | /** |
---|
[871] | 244 | @brief Parses a given std::string into a value of the type unsigned int and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
| 245 | @param input The string to convert |
---|
[667] | 246 | @param defvalue The default-value |
---|
[871] | 247 | @return True if the string was successfully parsed |
---|
[667] | 248 | */ |
---|
[871] | 249 | bool ConfigValueContainer::parseString(const std::string& input, unsigned int defvalue) |
---|
[667] | 250 | { |
---|
[871] | 251 | unsigned int temp; |
---|
| 252 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 253 | this->value_.setValue(temp); |
---|
| 254 | return success; |
---|
[667] | 255 | } |
---|
| 256 | |
---|
| 257 | /** |
---|
[871] | 258 | @brief Parses a given std::string into a value of the type char and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
| 259 | @param input The string to convert |
---|
[667] | 260 | @param defvalue The default-value |
---|
[871] | 261 | @return True if the string was successfully parsed |
---|
[667] | 262 | */ |
---|
[871] | 263 | bool ConfigValueContainer::parseString(const std::string& input, char defvalue) |
---|
[497] | 264 | { |
---|
[871] | 265 | // I used value_int_ instead of value_char_ to avoid number <-> char confusion in the config-file |
---|
| 266 | int temp; |
---|
| 267 | bool success = ConvertValue(&temp, input, (int)defvalue); |
---|
| 268 | this->value_.setValue((char)temp); |
---|
| 269 | return success; |
---|
[497] | 270 | } |
---|
| 271 | |
---|
| 272 | /** |
---|
[871] | 273 | @brief Parses a given std::string into a value of the type unsigned char and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
| 274 | @param input The string to convert |
---|
[497] | 275 | @param defvalue The default-value |
---|
[703] | 276 | @return True if the string was successfully parsed |
---|
| 277 | */ |
---|
[871] | 278 | bool ConfigValueContainer::parseString(const std::string& input, unsigned char defvalue) |
---|
[703] | 279 | { |
---|
[871] | 280 | // I used value_uint_ instead of value_uchar_ to avoid number <-> char confusion in the config-file |
---|
| 281 | unsigned int temp; |
---|
| 282 | bool success = ConvertValue(&temp, input, (unsigned int)defvalue); |
---|
| 283 | this->value_.setValue((unsigned char)temp); |
---|
| 284 | return success; |
---|
[703] | 285 | } |
---|
| 286 | |
---|
| 287 | /** |
---|
[871] | 288 | @brief Parses a given std::string into a value of the type short and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 289 | @param input The string to convert |
---|
| 290 | @param defvalue The default-value |
---|
| 291 | @return True if the string was successfully parsed |
---|
| 292 | */ |
---|
[871] | 293 | bool ConfigValueContainer::parseString(const std::string& input, short defvalue) |
---|
[703] | 294 | { |
---|
[871] | 295 | short temp; |
---|
| 296 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 297 | this->value_.setValue(temp); |
---|
| 298 | return success; |
---|
[703] | 299 | } |
---|
| 300 | |
---|
| 301 | /** |
---|
[871] | 302 | @brief Parses a given std::string into a value of the type unsigned short and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 303 | @param input The string to convert |
---|
| 304 | @param defvalue The default-value |
---|
| 305 | @return True if the string was successfully parsed |
---|
| 306 | */ |
---|
[871] | 307 | bool ConfigValueContainer::parseString(const std::string& input, unsigned short defvalue) |
---|
[703] | 308 | { |
---|
[871] | 309 | unsigned short temp; |
---|
| 310 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 311 | this->value_.setValue(temp); |
---|
| 312 | return success; |
---|
[703] | 313 | } |
---|
| 314 | |
---|
| 315 | /** |
---|
[871] | 316 | @brief Parses a given std::string into a value of the type long and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 317 | @param input The string to convert |
---|
| 318 | @param defvalue The default-value |
---|
| 319 | @return True if the string was successfully parsed |
---|
| 320 | */ |
---|
[871] | 321 | bool ConfigValueContainer::parseString(const std::string& input, long defvalue) |
---|
[703] | 322 | { |
---|
[871] | 323 | long temp; |
---|
| 324 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 325 | this->value_.setValue(temp); |
---|
| 326 | return success; |
---|
[703] | 327 | } |
---|
| 328 | |
---|
| 329 | /** |
---|
[871] | 330 | @brief Parses a given std::string into a value of the type unsigned long and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 331 | @param input The string to convert |
---|
| 332 | @param defvalue The default-value |
---|
| 333 | @return True if the string was successfully parsed |
---|
| 334 | */ |
---|
[871] | 335 | bool ConfigValueContainer::parseString(const std::string& input, unsigned long defvalue) |
---|
[703] | 336 | { |
---|
[871] | 337 | unsigned long temp; |
---|
| 338 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 339 | this->value_.setValue(temp); |
---|
| 340 | return success; |
---|
[703] | 341 | } |
---|
| 342 | |
---|
| 343 | /** |
---|
[717] | 344 | @brief Parses a given std::string into a value of the type float and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 345 | @param input The string to convert |
---|
| 346 | @param defvalue The default-value |
---|
| 347 | @return True if the string was successfully parsed |
---|
| 348 | */ |
---|
[871] | 349 | bool ConfigValueContainer::parseString(const std::string& input, float defvalue) |
---|
[703] | 350 | { |
---|
[871] | 351 | float temp; |
---|
| 352 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 353 | this->value_.setValue(temp); |
---|
| 354 | return success; |
---|
[703] | 355 | } |
---|
| 356 | |
---|
| 357 | /** |
---|
[717] | 358 | @brief Parses a given std::string into a value of the type double and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 359 | @param input The string to convert |
---|
| 360 | @param defvalue The default-value |
---|
| 361 | @return True if the string was successfully parsed |
---|
| 362 | */ |
---|
[871] | 363 | bool ConfigValueContainer::parseString(const std::string& input, double defvalue) |
---|
[703] | 364 | { |
---|
[871] | 365 | double temp; |
---|
| 366 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 367 | this->value_.setValue(temp); |
---|
| 368 | return success; |
---|
[703] | 369 | } |
---|
| 370 | |
---|
| 371 | /** |
---|
[717] | 372 | @brief Parses a given std::string into a value of the type long double and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 373 | @param input The string to convert |
---|
| 374 | @param defvalue The default-value |
---|
| 375 | @return True if the string was successfully parsed |
---|
| 376 | */ |
---|
[871] | 377 | bool ConfigValueContainer::parseString(const std::string& input, long double defvalue) |
---|
[703] | 378 | { |
---|
[871] | 379 | long double temp; |
---|
| 380 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 381 | this->value_.setValue(temp); |
---|
| 382 | return success; |
---|
[703] | 383 | } |
---|
| 384 | |
---|
| 385 | /** |
---|
[717] | 386 | @brief Parses a given std::string into a value of the type bool and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 387 | @param input The string to convert |
---|
| 388 | @param defvalue The default-value |
---|
| 389 | @return True if the string was successfully parsed |
---|
| 390 | */ |
---|
[871] | 391 | bool ConfigValueContainer::parseString(const std::string& input, bool defvalue) |
---|
[703] | 392 | { |
---|
| 393 | // Try to parse the value-string - is it a word? |
---|
| 394 | if (input.find("true") < input.size() |
---|
| 395 | || input.find("True") < input.size() |
---|
| 396 | || input.find("yes") < input.size() |
---|
| 397 | || input.find("Yes") < input.size()) |
---|
[871] | 398 | this->value_.setValue(true); |
---|
[703] | 399 | else if (input.find("false") < input.size() |
---|
| 400 | || input.find("False") < input.size() |
---|
| 401 | || input.find("no") < input.size() |
---|
| 402 | || input.find("No") < input.size()) |
---|
[871] | 403 | this->value_.setValue(false); |
---|
[703] | 404 | else |
---|
| 405 | { |
---|
| 406 | // Its not a known word - is it a number? |
---|
[871] | 407 | bool temp; |
---|
| 408 | bool success = ConvertValue(&temp, input, defvalue); |
---|
| 409 | this->value_.setValue(temp); |
---|
| 410 | return success; |
---|
[703] | 411 | } |
---|
| 412 | |
---|
| 413 | return true; |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | /** |
---|
[717] | 417 | @brief Parses a given std::string into a value of the type std::string and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 418 | @param input The string to convert |
---|
| 419 | @param defvalue The default-value |
---|
| 420 | @return True if the string was successfully parsed |
---|
| 421 | */ |
---|
[871] | 422 | bool ConfigValueContainer::parseString(const std::string& input, const std::string& defvalue) |
---|
[703] | 423 | { |
---|
| 424 | // Strip the quotes |
---|
| 425 | unsigned int pos1 = input.find("\"") + 1; |
---|
| 426 | unsigned int pos2 = input.find("\"", pos1); |
---|
| 427 | |
---|
| 428 | // Check if the entry was correctly quoted |
---|
| 429 | if (pos1 < input.length() && pos2 < input.length() && !(input.find("\"", pos2 + 1) < input.length())) |
---|
| 430 | { |
---|
| 431 | // It was - get the string between the quotes |
---|
[871] | 432 | this->value_.setValue(input.substr(pos1, pos2 - pos1)); |
---|
[703] | 433 | return true; |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | // It wasn't - use the default-value and restore the entry in the config-file. |
---|
[871] | 437 | this->value_.setValue(defvalue); |
---|
[703] | 438 | return false; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | /** |
---|
[717] | 442 | @brief Parses a given std::string into a value of the type const char* and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 443 | @param input The string to convert |
---|
| 444 | @param defvalue The default-value |
---|
| 445 | @return True if the string was successfully parsed |
---|
| 446 | */ |
---|
[871] | 447 | bool ConfigValueContainer::parseString(const std::string& input, const char* defvalue) |
---|
[703] | 448 | { |
---|
| 449 | // Strip the quotes |
---|
| 450 | unsigned int pos1 = input.find("\"") + 1; |
---|
| 451 | unsigned int pos2 = input.find("\"", pos1); |
---|
| 452 | |
---|
| 453 | // Check if the entry was correctly quoted |
---|
| 454 | if (pos1 < input.length() && pos2 < input.length() && !(input.find("\"", pos2 + 1) < input.length())) |
---|
| 455 | { |
---|
| 456 | // It was - get the string between the quotes |
---|
[871] | 457 | this->value_.setValue(input.substr(pos1, pos2 - pos1)); |
---|
[703] | 458 | return true; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | // It wasn't - use the default-value and restore the entry in the config-file. |
---|
[871] | 462 | this->value_.setValue(defvalue); |
---|
[703] | 463 | return false; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | /** |
---|
[717] | 467 | @brief Parses a given std::string into a value of the type _Vector2 and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 468 | @param input The string to convert |
---|
| 469 | @param defvalue The default-value |
---|
| 470 | @return True if the string was successfully parsed |
---|
| 471 | */ |
---|
[871] | 472 | bool ConfigValueContainer::parseString(const std::string& input, const Vector2& defvalue) |
---|
[703] | 473 | { |
---|
[497] | 474 | // Strip the value-string |
---|
[703] | 475 | unsigned int pos1 = input.find("(") + 1; |
---|
| 476 | unsigned int pos2 = input.find(")", pos1); |
---|
[497] | 477 | |
---|
[703] | 478 | // Try to convert the stripped value-string to Vector2 |
---|
| 479 | if (pos1 < input.length() && pos2 < input.length() && pos1 < pos2) |
---|
| 480 | { |
---|
[715] | 481 | std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ","); |
---|
[871] | 482 | if (!ConvertValue(&this->value_.getVector2().x, tokens[0])) |
---|
[703] | 483 | { |
---|
[871] | 484 | this->value_.setValue(defvalue); |
---|
[703] | 485 | return false; |
---|
| 486 | } |
---|
[871] | 487 | if (!ConvertValue(&this->value_.getVector2().y, tokens[1])) |
---|
[703] | 488 | { |
---|
[871] | 489 | this->value_.setValue(defvalue); |
---|
[703] | 490 | return false; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | return true; |
---|
| 494 | } |
---|
| 495 | |
---|
[871] | 496 | this->value_.setValue(defvalue); |
---|
[703] | 497 | return false; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | /** |
---|
[717] | 501 | @brief Parses a given std::string into a value of the type Vector3 and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 502 | @param input The string to convert |
---|
| 503 | @param defvalue The default-value |
---|
| 504 | @return True if the string was successfully parsed |
---|
| 505 | */ |
---|
[871] | 506 | bool ConfigValueContainer::parseString(const std::string& input, const Vector3& defvalue) |
---|
[703] | 507 | { |
---|
| 508 | // Strip the value-string |
---|
| 509 | unsigned int pos1 = input.find("(") + 1; |
---|
| 510 | unsigned int pos2 = input.find(")", pos1); |
---|
| 511 | |
---|
[497] | 512 | // Try to convert the stripped value-string to Vector3 |
---|
[703] | 513 | if (pos1 < input.length() && pos2 < input.length() && pos1 < pos2) |
---|
[497] | 514 | { |
---|
[715] | 515 | std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ","); |
---|
[871] | 516 | if (!ConvertValue(&this->value_.getVector3().x, tokens[0])) |
---|
[703] | 517 | { |
---|
[871] | 518 | this->value_.setValue(defvalue); |
---|
[703] | 519 | return false; |
---|
| 520 | } |
---|
[871] | 521 | if (!ConvertValue(&this->value_.getVector3().y, tokens[1])) |
---|
[703] | 522 | { |
---|
[871] | 523 | this->value_.setValue(defvalue); |
---|
[703] | 524 | return false; |
---|
| 525 | } |
---|
[871] | 526 | if (!ConvertValue(&this->value_.getVector3().z, tokens[2])) |
---|
[703] | 527 | { |
---|
[871] | 528 | this->value_.setValue(defvalue); |
---|
[703] | 529 | return false; |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | return true; |
---|
| 533 | } |
---|
| 534 | |
---|
[871] | 535 | this->value_.setValue(defvalue); |
---|
[703] | 536 | return false; |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | /** |
---|
[717] | 540 | @brief Parses a given std::string into a value of the type ColourValue and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
[703] | 541 | @param input The string to convert |
---|
| 542 | @param defvalue The default-value |
---|
| 543 | @return True if the string was successfully parsed |
---|
| 544 | */ |
---|
[871] | 545 | bool ConfigValueContainer::parseString(const std::string& input, const ColourValue& defvalue) |
---|
[703] | 546 | { |
---|
| 547 | // Strip the value-string |
---|
| 548 | unsigned int pos1 = input.find("(") + 1; |
---|
| 549 | unsigned int pos2 = input.find(")", pos1); |
---|
| 550 | |
---|
| 551 | // Try to convert the stripped value-string to Vector3 |
---|
| 552 | if (pos1 < input.length() && pos2 < input.length() && pos1 < pos2) |
---|
| 553 | { |
---|
[715] | 554 | std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ","); |
---|
[871] | 555 | if (!ConvertValue(&this->value_.getColourValue().r, tokens[0])) |
---|
[703] | 556 | { |
---|
[871] | 557 | this->value_.setValue(defvalue); |
---|
[703] | 558 | return false; |
---|
| 559 | } |
---|
[871] | 560 | if (!ConvertValue(&this->value_.getColourValue().g, tokens[1])) |
---|
[703] | 561 | { |
---|
[871] | 562 | this->value_.setValue(defvalue); |
---|
[703] | 563 | return false; |
---|
| 564 | } |
---|
[871] | 565 | if (!ConvertValue(&this->value_.getColourValue().b, tokens[2])) |
---|
[703] | 566 | { |
---|
[871] | 567 | this->value_.setValue(defvalue); |
---|
[703] | 568 | return false; |
---|
| 569 | } |
---|
[871] | 570 | if (!ConvertValue(&this->value_.getColourValue().a, tokens[3])) |
---|
[703] | 571 | { |
---|
[871] | 572 | this->value_.setValue(defvalue); |
---|
[703] | 573 | return false; |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | return true; |
---|
[497] | 577 | } |
---|
[703] | 578 | |
---|
[871] | 579 | this->value_.setValue(defvalue); |
---|
[703] | 580 | return false; |
---|
[497] | 581 | } |
---|
| 582 | |
---|
| 583 | /** |
---|
[871] | 584 | @brief Parses a given std::string into a value of the type Quaternion and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
| 585 | @param input The string to convert |
---|
| 586 | @param defvalue The default-value |
---|
| 587 | @return True if the string was successfully parsed |
---|
| 588 | */ |
---|
| 589 | bool ConfigValueContainer::parseString(const std::string& input, const Quaternion& defvalue) |
---|
| 590 | { |
---|
| 591 | // Strip the value-string |
---|
| 592 | unsigned int pos1 = input.find("(") + 1; |
---|
| 593 | unsigned int pos2 = input.find(")", pos1); |
---|
| 594 | |
---|
| 595 | // Try to convert the stripped value-string to Vector3 |
---|
| 596 | if (pos1 < input.length() && pos2 < input.length() && pos1 < pos2) |
---|
| 597 | { |
---|
| 598 | std::vector<std::string> tokens = tokenize(input.substr(pos1, pos2 - pos1), ","); |
---|
| 599 | if (!ConvertValue(&this->value_.getQuaternion().w, tokens[0])) |
---|
| 600 | { |
---|
| 601 | this->value_.setValue(defvalue); |
---|
| 602 | return false; |
---|
| 603 | } |
---|
| 604 | if (!ConvertValue(&this->value_.getQuaternion().x, tokens[1])) |
---|
| 605 | { |
---|
| 606 | this->value_.setValue(defvalue); |
---|
| 607 | return false; |
---|
| 608 | } |
---|
| 609 | if (!ConvertValue(&this->value_.getQuaternion().y, tokens[2])) |
---|
| 610 | { |
---|
| 611 | this->value_.setValue(defvalue); |
---|
| 612 | return false; |
---|
| 613 | } |
---|
| 614 | if (!ConvertValue(&this->value_.getQuaternion().z, tokens[3])) |
---|
| 615 | { |
---|
| 616 | this->value_.setValue(defvalue); |
---|
| 617 | return false; |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | return true; |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | this->value_.setValue(defvalue); |
---|
| 624 | return false; |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | /** |
---|
| 628 | @brief Parses a given std::string into a value of the type long double and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
| 629 | @param input The string to convert |
---|
| 630 | @param defvalue The default-value |
---|
| 631 | @return True if the string was successfully parsed |
---|
| 632 | */ |
---|
| 633 | bool ConfigValueContainer::parseString(const std::string& input, const Radian& defvalue) |
---|
| 634 | { |
---|
| 635 | return ConvertValue(&this->value_.getRadian(), input, defvalue); |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | /** |
---|
| 639 | @brief Parses a given std::string into a value of the type long double and assigns it to the right variable. If the conversion failed, the default-value gets assigned. |
---|
| 640 | @param input The string to convert |
---|
| 641 | @param defvalue The default-value |
---|
| 642 | @return True if the string was successfully parsed |
---|
| 643 | */ |
---|
| 644 | bool ConfigValueContainer::parseString(const std::string& input, const Degree& defvalue) |
---|
| 645 | { |
---|
| 646 | return ConvertValue(&this->value_.getDegree(), input, defvalue); |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | /** |
---|
[703] | 650 | @brief Sets the corresponding entry in the config-file back to the default value. |
---|
[497] | 651 | */ |
---|
[703] | 652 | void ConfigValueContainer::resetConfigFileEntry() |
---|
[497] | 653 | { |
---|
[667] | 654 | (*this->configFileLine_) = this->varname_ + "=" + this->defvalueString_; |
---|
| 655 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
| 656 | } |
---|
[497] | 657 | |
---|
[703] | 658 | /** |
---|
[704] | 659 | @brief Sets the value of the variable back to the default value and resets the config-file entry. |
---|
[703] | 660 | */ |
---|
| 661 | void ConfigValueContainer::resetConfigValue() |
---|
| 662 | { |
---|
[871] | 663 | this->parseString(this->defvalueString_, this->value_); |
---|
[704] | 664 | this->resetConfigFileEntry(); |
---|
[703] | 665 | } |
---|
[497] | 666 | |
---|
| 667 | /** |
---|
| 668 | @brief Searches the corresponding entry in the config-file and creates it, if there is no entry. |
---|
| 669 | */ |
---|
| 670 | void ConfigValueContainer::searchConfigFileLine() |
---|
| 671 | { |
---|
| 672 | // Read the file if needed |
---|
[698] | 673 | if (!ConfigValueContainer::finishedReadingConfigFile()) |
---|
[497] | 674 | ConfigValueContainer::readConfigFile(CONFIGFILEPATH); |
---|
| 675 | |
---|
| 676 | // The string of the section we're searching |
---|
[715] | 677 | std::string section = ""; |
---|
[497] | 678 | section.append("["); |
---|
| 679 | section.append(this->classname_); |
---|
| 680 | section.append("]"); |
---|
| 681 | |
---|
| 682 | // Iterate through all config-file-lines |
---|
| 683 | bool success = false; |
---|
[715] | 684 | std::list<std::string>::iterator it1; |
---|
[698] | 685 | for(it1 = ConfigValueContainer::getConfigFileLines().begin(); it1 != ConfigValueContainer::getConfigFileLines().end(); ++it1) |
---|
[497] | 686 | { |
---|
| 687 | // Don't try to parse comments |
---|
| 688 | if (this->isComment(*it1)) |
---|
| 689 | continue; |
---|
| 690 | |
---|
| 691 | if ((*it1).find(section) < (*it1).length()) |
---|
| 692 | { |
---|
| 693 | // We found the right section |
---|
| 694 | bool bLineIsEmpty = false; |
---|
[715] | 695 | std::list<std::string>::iterator positionToPutNewLineAt; |
---|
[497] | 696 | |
---|
| 697 | // Iterate through all lines in the section |
---|
[715] | 698 | std::list<std::string>::iterator it2; |
---|
[698] | 699 | for(it2 = ++it1; it2 != ConfigValueContainer::getConfigFileLines().end(); ++it2) |
---|
[497] | 700 | { |
---|
| 701 | // Don't try to parse comments |
---|
| 702 | if (this->isComment(*it2)) |
---|
| 703 | continue; |
---|
| 704 | |
---|
| 705 | // This if-else block is used to write a new line right after the last line of the |
---|
| 706 | // section but in front of the following empty lines before the next section. |
---|
| 707 | // (So this helps to keep a nice formatting with empty-lines between sections in the config-file) |
---|
| 708 | if (this->isEmpty(*it2)) |
---|
| 709 | { |
---|
| 710 | if (!bLineIsEmpty) |
---|
| 711 | { |
---|
| 712 | bLineIsEmpty = true; |
---|
| 713 | positionToPutNewLineAt = it2; |
---|
| 714 | } |
---|
| 715 | } |
---|
| 716 | else |
---|
| 717 | { |
---|
| 718 | if (!bLineIsEmpty) |
---|
| 719 | positionToPutNewLineAt = it2; |
---|
| 720 | |
---|
| 721 | bLineIsEmpty = false; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | // Look out for the beginning of the next section |
---|
| 725 | unsigned int open = (*it2).find("["); |
---|
| 726 | unsigned int close = (*it2).find("]"); |
---|
| 727 | if ((open < (*it2).length()) && (close < (*it2).length()) && (open < close)) |
---|
| 728 | { |
---|
| 729 | // The next section startet, so our line isn't yet in the file - now we add it and safe the file |
---|
[698] | 730 | this->configFileLine_ = this->getConfigFileLines().insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_); |
---|
[497] | 731 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
| 732 | success = true; |
---|
| 733 | break; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | // Look out for the variable-name |
---|
| 737 | if ((*it2).find(this->varname_) < (*it2).length()) |
---|
| 738 | { |
---|
| 739 | // We found the right line - safe it and return |
---|
| 740 | this->configFileLine_ = it2; |
---|
| 741 | success = true; |
---|
| 742 | break; |
---|
| 743 | } |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | // Check if we succeeded |
---|
| 747 | if (!success) |
---|
| 748 | { |
---|
| 749 | // Looks like we found the right section, but the file ended without containing our variable - so we add it and safe the file |
---|
[698] | 750 | this->configFileLine_ = this->getConfigFileLines().insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalueString_); |
---|
[497] | 751 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); |
---|
| 752 | success = true; |
---|
| 753 | } |
---|
| 754 | break; |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | |
---|
| 758 | // Check if we succeeded |
---|
| 759 | if (!success) |
---|
| 760 | { |
---|
| 761 | // We obviously didn't found the right section, so we'll create it |
---|
[698] | 762 | this->getConfigFileLines().push_back("[" + this->classname_ + "]"); // Create the section |
---|
| 763 | this->getConfigFileLines().push_back(this->varname_ + "=" + this->defvalueString_); // Create the line |
---|
| 764 | this->configFileLine_ = --this->getConfigFileLines().end(); // Set the pointer to the last element |
---|
[497] | 765 | success = true; |
---|
[698] | 766 | this->getConfigFileLines().push_back(""); // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function |
---|
[667] | 767 | ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); // Save the changed config-file |
---|
[497] | 768 | } |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | /** |
---|
| 772 | @brief Determines if a line in the config-file is a comment. |
---|
| 773 | @param line The line to check |
---|
| 774 | @return True = it's a comment |
---|
| 775 | */ |
---|
[715] | 776 | bool ConfigValueContainer::isComment(const std::string& line) |
---|
[497] | 777 | { |
---|
| 778 | // Strip the line, whitespaces are disturbing |
---|
[715] | 779 | std::string teststring = getStrippedLine(line); |
---|
[497] | 780 | |
---|
| 781 | // There are four possible comment-symbols: |
---|
| 782 | // 1) #comment in script-language style |
---|
| 783 | // 2) %comment in matlab style |
---|
| 784 | // 3) ;comment in unreal tournament config-file style |
---|
| 785 | // 4) //comment in code style |
---|
| 786 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/')) |
---|
| 787 | return true; |
---|
| 788 | |
---|
| 789 | return false; |
---|
| 790 | } |
---|
| 791 | |
---|
| 792 | /** |
---|
| 793 | @brief Determines if a line in the config-file is empty (contains only whitespaces). |
---|
| 794 | @param line The line to check |
---|
| 795 | @return True = it's empty |
---|
| 796 | */ |
---|
[715] | 797 | bool ConfigValueContainer::isEmpty(const std::string& line) |
---|
[497] | 798 | { |
---|
| 799 | return getStrippedLine(line) == ""; |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | /** |
---|
| 803 | @brief Removes all whitespaces from a line. |
---|
| 804 | @param line The line to strip |
---|
| 805 | @return The stripped line |
---|
| 806 | */ |
---|
[715] | 807 | std::string ConfigValueContainer::getStrippedLine(const std::string& line) |
---|
[497] | 808 | { |
---|
[715] | 809 | std::string output = line; |
---|
[497] | 810 | unsigned int pos; |
---|
| 811 | while ((pos = output.find(" ")) < output.length()) |
---|
| 812 | output.erase(pos, 1); |
---|
| 813 | while ((pos = output.find("\t")) < output.length()) |
---|
| 814 | output.erase(pos, 1); |
---|
| 815 | |
---|
| 816 | return output; |
---|
| 817 | } |
---|
| 818 | |
---|
| 819 | /** |
---|
| 820 | @brief Returns the part in the corresponding config-file-entry of the container that defines the value. |
---|
| 821 | @param bStripped True = strip the value-string |
---|
| 822 | @return The value-string |
---|
| 823 | */ |
---|
[715] | 824 | std::string ConfigValueContainer::parseValueString(bool bStripped) |
---|
[497] | 825 | { |
---|
[715] | 826 | std::string output; |
---|
[497] | 827 | if (bStripped) |
---|
| 828 | output = this->getStrippedLine(*this->configFileLine_); |
---|
| 829 | else |
---|
| 830 | output = *this->configFileLine_; |
---|
| 831 | |
---|
| 832 | return output.substr(output.find("=") + 1); |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | /** |
---|
[871] | 836 | @brief Rreturns a list, containing all entrys in the config-file. |
---|
| 837 | @return The list |
---|
[698] | 838 | */ |
---|
[715] | 839 | std::list<std::string>& ConfigValueContainer::getConfigFileLines() |
---|
[698] | 840 | { |
---|
| 841 | // This is done to avoid problems while executing this code before main() |
---|
[715] | 842 | static std::list<std::string> configFileLinesStaticReference = std::list<std::string>(); |
---|
[698] | 843 | return configFileLinesStaticReference; |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | /** |
---|
| 847 | @brief Returns true if the ConfigFile is read and stored into the ConfigFile-lines-list. |
---|
| 848 | @param finished This is used to change the state |
---|
| 849 | @return True if the ConfigFile is read and stored into the ConfigFile-lines-list |
---|
| 850 | */ |
---|
| 851 | bool ConfigValueContainer::finishedReadingConfigFile(bool finished) |
---|
| 852 | { |
---|
| 853 | // This is done to avoid problems while executing this code before main() |
---|
| 854 | static bool finishedReadingConfigFileStaticVariable = false; |
---|
| 855 | |
---|
| 856 | if (finished) |
---|
| 857 | finishedReadingConfigFileStaticVariable = true; |
---|
| 858 | |
---|
| 859 | return finishedReadingConfigFileStaticVariable; |
---|
| 860 | } |
---|
| 861 | |
---|
| 862 | /** |
---|
[497] | 863 | @brief Reads the config-file and stores the lines in a list. |
---|
| 864 | @param filename The name of the config-file |
---|
| 865 | */ |
---|
[715] | 866 | void ConfigValueContainer::readConfigFile(const std::string& filename) |
---|
[497] | 867 | { |
---|
| 868 | // This creates the file if it's not existing |
---|
| 869 | std::ofstream createFile; |
---|
| 870 | createFile.open(filename.c_str(), std::fstream::app); |
---|
| 871 | createFile.close(); |
---|
| 872 | |
---|
| 873 | // Open the file |
---|
| 874 | std::ifstream file; |
---|
| 875 | file.open(filename.c_str(), std::fstream::in); |
---|
| 876 | |
---|
[704] | 877 | if (!file.is_open()) |
---|
| 878 | { |
---|
[871] | 879 | COUT(1) << "An error occurred in ConfigValueContainer.cc:" << std::endl; |
---|
[704] | 880 | COUT(1) << "Error: Couldn't open config-file " << filename << " to read the config values!" << std::endl; |
---|
| 881 | return; |
---|
| 882 | } |
---|
| 883 | |
---|
[497] | 884 | char line[1024]; |
---|
| 885 | |
---|
| 886 | // Iterate through the file and add the lines into the list |
---|
| 887 | while (file.good() && !file.eof()) |
---|
| 888 | { |
---|
| 889 | file.getline(line, 1024); |
---|
[698] | 890 | ConfigValueContainer::getConfigFileLines().push_back(line); |
---|
[497] | 891 | // std::cout << "### ->" << line << "<- : empty: " << isEmpty(line) << " comment: " << isComment(line) << std::endl; |
---|
| 892 | } |
---|
| 893 | |
---|
| 894 | // The last line is useless |
---|
[698] | 895 | ConfigValueContainer::getConfigFileLines().pop_back(); |
---|
[497] | 896 | |
---|
| 897 | // Add an empty line to the end of the file if needed |
---|
| 898 | // this is needed for the algorithm in the searchConfigFileLine-function |
---|
[698] | 899 | if ((ConfigValueContainer::getConfigFileLines().size() > 0) && !isEmpty(*ConfigValueContainer::getConfigFileLines().rbegin())) |
---|
[497] | 900 | { |
---|
| 901 | // std::cout << "### newline added" << std::endl; |
---|
[698] | 902 | ConfigValueContainer::getConfigFileLines().push_back(""); |
---|
[497] | 903 | } |
---|
| 904 | |
---|
| 905 | file.close(); |
---|
[704] | 906 | |
---|
| 907 | ConfigValueContainer::finishedReadingConfigFile(true); |
---|
[497] | 908 | } |
---|
| 909 | |
---|
| 910 | /** |
---|
[705] | 911 | @brief Writes the content of the list, containing all lines of the config-file, into the config-file. |
---|
| 912 | @param filename The name of the config-file |
---|
| 913 | */ |
---|
[715] | 914 | void ConfigValueContainer::writeConfigFile(const std::string& filename) |
---|
[497] | 915 | { |
---|
| 916 | // Make sure we stored the config-file in the list |
---|
[698] | 917 | if (!ConfigValueContainer::finishedReadingConfigFile()) |
---|
[497] | 918 | ConfigValueContainer::readConfigFile(filename); |
---|
| 919 | |
---|
| 920 | // Open the file |
---|
| 921 | std::ofstream file; |
---|
| 922 | file.open(filename.c_str(), std::fstream::out); |
---|
[949] | 923 | file.setf(std::ios::fixed, std::ios::floatfield); |
---|
| 924 | file.precision(6); |
---|
[497] | 925 | |
---|
[704] | 926 | if (!file.is_open()) |
---|
| 927 | { |
---|
[871] | 928 | COUT(1) << "An error occurred in ConfigValueContainer.cc:" << std::endl; |
---|
[704] | 929 | COUT(1) << "Error: Couldn't open config-file " << filename << " to write the config values!" << std::endl; |
---|
| 930 | return; |
---|
| 931 | } |
---|
| 932 | |
---|
[497] | 933 | // Iterate through the list an write the lines into the file |
---|
[715] | 934 | std::list<std::string>::iterator it; |
---|
[704] | 935 | for (it = ConfigValueContainer::getConfigFileLines().begin(); it != ConfigValueContainer::getConfigFileLines().end(); ++it) |
---|
[497] | 936 | { |
---|
| 937 | file << (*it) << std::endl; |
---|
| 938 | } |
---|
| 939 | |
---|
| 940 | file.close(); |
---|
| 941 | } |
---|
[705] | 942 | |
---|
| 943 | /** |
---|
| 944 | @brief Adds a description to the config-value. |
---|
| 945 | @param description The description |
---|
| 946 | */ |
---|
[715] | 947 | void ConfigValueContainer::description(const std::string& description) |
---|
[705] | 948 | { |
---|
| 949 | if (!this->bAddedDescription_) |
---|
| 950 | { |
---|
[715] | 951 | this->description_ = std::string("ConfigValueDescription::" + this->classname_ + "::" + this->varname_); |
---|
[871] | 952 | AddLanguageEntry(this->description_, description); |
---|
[705] | 953 | this->bAddedDescription_ = true; |
---|
| 954 | } |
---|
| 955 | } |
---|
[871] | 956 | |
---|
| 957 | /** |
---|
| 958 | @brief Returns the description of the config-value. |
---|
| 959 | @return The description |
---|
| 960 | */ |
---|
| 961 | const std::string& ConfigValueContainer::getDescription() const |
---|
| 962 | { |
---|
| 963 | return GetLocalisation(this->description_); |
---|
| 964 | } |
---|
[497] | 965 | } |
---|