- Timestamp:
- Dec 9, 2007, 11:21:14 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchy/src/orxonox/core/ConfigValueContainer.cc
r435 r447 8 8 namespace orxonox 9 9 { 10 std::list<std::string>* ConfigValueContainer::configFileLines_s = 0; 11 bool ConfigValueContainer::readConfigFile_s = false; 12 10 std::list<std::string>* ConfigValueContainer::configFileLines_s = 0; // Set the static member variable configFileLines_s to zero 11 bool ConfigValueContainer::readConfigFile_s = false; // Set the static member variable readConfigFile_s to false 12 13 /** 14 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_int_. 15 @param classname The name of the class the variable belongs to 16 @param varname The name of the variable 17 @param defvalue The default-value 18 */ 13 19 ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, int defvalue) 14 20 { 21 // Try to convert the default-value from int to string 15 22 std::ostringstream ostream; 16 17 23 if (ostream << defvalue) 18 24 this->defvalue_ = ostream.str(); … … 20 26 this->defvalue_ = "0"; 21 27 28 // Set the default values, then get the value-string 22 29 this->setDefaultValues(classname, varname); 23 30 std::string valueString = this->getValueString(); 24 31 32 // Try to convert the value-string to int 25 33 std::istringstream istream(valueString); 26 34 if (!(istream >> this->value_int_)) 27 35 { 36 // The conversion failed - use the default value and restore the entry in the config-file 28 37 this->value_int_ = defvalue; 29 38 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 32 41 } 33 42 43 /** 44 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_double_. 45 @param classname The name of the class the variable belongs to 46 @param varname The name of the variable 47 @param defvalue The default-value 48 */ 34 49 ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, double defvalue) 35 50 { 51 // Try to convert the default-value from double to string 36 52 std::ostringstream ostream; 37 38 53 if (ostream << defvalue) 39 54 this->defvalue_ = ostream.str(); … … 41 56 this->defvalue_ = "0.000000"; 42 57 58 // Set the default values, then get the value-string 43 59 this->setDefaultValues(classname, varname); 44 60 std::string valueString = this->getValueString(); 45 61 62 // Try to convert the value-string to double 46 63 std::istringstream istream(valueString); 47 64 if (!(istream >> this->value_double_)) 48 65 { 66 // The conversion failed - use the default value and restore the entry in the config-file 49 67 this->value_double_ = defvalue; 50 68 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 53 71 } 54 72 73 /** 74 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_bool_. 75 @param classname The name of the class the variable belongs to 76 @param varname The name of the variable 77 @param defvalue The default-value 78 */ 55 79 ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, bool defvalue) 56 80 { 81 // Convert the default-value from bool to string 57 82 if (defvalue) 58 83 this->defvalue_ = "true"; … … 60 85 this->defvalue_ = "false"; 61 86 87 // Set the default values, then get the value-string 62 88 this->setDefaultValues(classname, varname); 63 89 std::string valueString = this->getValueString(); 64 90 91 // Try to parse the value-string - is it a word? 65 92 if (valueString.find("true") < valueString.size() || valueString.find("yes") < valueString.size()) 66 93 this->value_bool_ = true; … … 69 96 else 70 97 { 98 // Its not a known word - is it a number? 71 99 std::istringstream istream(valueString); 72 100 if (!(istream >> this->value_bool_)) 73 101 { 102 // The conversion failed - use the default value and restore the entry in the config-file 74 103 this->value_bool_ = defvalue; 75 104 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 79 108 } 80 109 110 /** 111 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_string_. 112 @param classname The name of the class the variable belongs to 113 @param varname The name of the variable 114 @param defvalue The default-value 115 */ 81 116 ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, const char* defvalue) 82 117 { 118 // Not much to do here - just set all member-variables and check the config-file 83 119 this->defvalue_ = defvalue; 84 120 this->setDefaultValues(classname, varname); … … 86 122 } 87 123 124 /** 125 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_vector3_. 126 @param classname The name of the class the variable belongs to 127 @param varname The name of the variable 128 @param defvalue The default-value 129 */ 88 130 ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::Vector3 defvalue) 89 131 { 132 // Try to convert the default-value from Vector3 to string 90 133 std::ostringstream ostream; 91 134 if (ostream << "(" << defvalue.x << "," << defvalue.y << "," << defvalue.z << ")") … … 94 137 this->defvalue_ = "(0,0,0)"; 95 138 139 // Set the default values, then get the value-string 96 140 this->setDefaultValues(classname, varname); 97 141 std::string valueString = this->getValueString(); 98 142 143 // Strip the value-string 144 valueString = this->getStrippedLine(valueString); 99 145 unsigned int pos; 100 while ((pos = valueString.find(" ")) < valueString.length())101 valueString.erase(pos, 1);102 146 while ((pos = valueString.find("(")) < valueString.length()) 103 147 valueString.erase(pos, 1); … … 107 151 valueString.replace(pos, 1, " "); 108 152 153 // Try to convert the stripped value-string to Vector3 109 154 std::istringstream istream(valueString); 110 111 155 if (!(istream >> this->value_vector3_.x)) 112 156 { 157 // The conversion failed - use the default value and restore the entry in the config-file 113 158 this->value_vector3_.x = defvalue.x; 114 159 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 117 162 if (!(istream >> this->value_vector3_.y)) 118 163 { 164 // The conversion failed - use the default value and restore the entry in the config-file 119 165 this->value_vector3_.y = defvalue.y; 120 166 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 123 169 if (!(istream >> this->value_vector3_.z)) 124 170 { 171 // The conversion failed - use the default value and restore the entry in the config-file 125 172 this->value_vector3_.z = defvalue.z; 126 173 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 129 176 } 130 177 178 /** 179 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets this->value_colourvalue_. 180 @param classname The name of the class the variable belongs to 181 @param varname The name of the variable 182 @param defvalue The default-value 183 */ 131 184 ConfigValueContainer::ConfigValueContainer(const std::string& classname, const std::string& varname, Ogre::ColourValue defvalue) 132 185 { 186 // Try to convert the default-value from ColourValue to string 133 187 std::ostringstream ostream; 134 188 if (ostream << "(" << defvalue.r << "," << defvalue.g << "," << defvalue.b << "," << defvalue.a << ")") … … 137 191 this->defvalue_ = "(0,0,0,0)"; 138 192 193 // Set the default values, then get the value-string 139 194 this->setDefaultValues(classname, varname); 140 195 std::string valueString = this->getValueString(); 141 196 197 // Strip the value-string 198 valueString = this->getStrippedLine(valueString); 142 199 unsigned int pos; 143 while ((pos = valueString.find(" ")) < valueString.length())144 valueString.erase(pos, 1);145 200 while ((pos = valueString.find("(")) < valueString.length()) 146 201 valueString.erase(pos, 1); … … 150 205 valueString.replace(pos, 1, " "); 151 206 207 // Try to convert the stripped value-string to Vector3 152 208 std::istringstream istream(valueString); 153 154 209 if (!(istream >> this->value_colourvalue_.r)) 155 210 { 211 // The conversion failed - use the default value and restore the entry in the config-file 156 212 this->value_colourvalue_.r = defvalue.r; 157 213 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 160 216 if (!(istream >> this->value_colourvalue_.g)) 161 217 { 218 // The conversion failed - use the default value and restore the entry in the config-file 162 219 this->value_colourvalue_.g = defvalue.g; 163 220 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 166 223 if (!(istream >> this->value_colourvalue_.b)) 167 224 { 225 // The conversion failed - use the default value and restore the entry in the config-file 168 226 this->value_colourvalue_.b = defvalue.b; 169 227 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 172 230 if (!(istream >> this->value_colourvalue_.a)) 173 231 { 232 // The conversion failed - use the default value and restore the entry in the config-file 174 233 this->value_colourvalue_.a = defvalue.a; 175 234 (*this->configFileLine_) = this->varname_ + "=" + this->defvalue_; … … 178 237 } 179 238 239 /** 240 @brief Sets the default values of the container and searches the coresponding entry in the config-file. 241 @param classname The name of the class the variable belongs to 242 @param varname The name of the variable 243 */ 180 244 void ConfigValueContainer::setDefaultValues(const std::string& classname, const std::string& varname) 181 245 { 246 // Set the class and variable names 182 247 this->classname_ = classname; 183 248 this->varname_ = varname; 184 249 250 // Search the entry in the config-file 185 251 this->searchConfigFileLine(); 186 252 253 // Set the values of all types to zero 187 254 this->value_int_ = 0; 188 255 this->value_double_ = 0.000000; … … 193 260 } 194 261 262 /** 263 @brief Searches the corresponding entry in the config-file and creates it, if there is no entry. 264 */ 195 265 void ConfigValueContainer::searchConfigFileLine() 196 266 { 267 // Read the file if needed 197 268 if (!ConfigValueContainer::readConfigFile_s) 198 269 ConfigValueContainer::readConfigFile(CONFIGFILEPATH); 199 270 271 // Just in case something goes wrong 200 272 this->configFileLine_ = 0; 201 273 274 // The string of the section we're searching 202 275 std::string section = ""; 203 276 section.append("["); … … 205 278 section.append("]"); 206 279 280 // Iterate through all config-file-lines 207 281 bool success = false; 208 282 std::list<std::string>::iterator it1; 209 283 for(it1 = ConfigValueContainer::configFileLines_s->begin(); it1 != ConfigValueContainer::configFileLines_s->end(); ++it1) 210 284 { 285 // Don't try to parse comments 211 286 if (this->isComment(*it1)) 212 287 continue; … … 214 289 if ((*it1).find(section) < (*it1).length()) 215 290 { 291 // We found the right section 216 292 bool bLineIsEmpty = false; 217 293 std::list<std::string>::iterator positionToPutNewLineAt; 218 294 295 // Iterate through all lines in the section 219 296 std::list<std::string>::iterator it2; 220 297 for(it2 = ++it1; it2 != ConfigValueContainer::configFileLines_s->end(); ++it2) 221 298 { 299 // Don't try to parse comments 222 300 if (this->isComment(*it2)) 223 301 continue; 224 302 303 // This if-else block is used to write a new line right after the last line of the 304 // section but in front of the following empty lines before the next section. 305 // (So this helps to keep a nice formatting with empty-lines between sections in the config-file) 225 306 if (this->isEmpty(*it2)) 226 307 { … … 239 320 } 240 321 322 // Look out for the beginning of the next section 241 323 unsigned int open = (*it2).find("["); 242 324 unsigned int close = (*it2).find("]"); 243 325 if ((open < (*it2).length()) && (close < (*it2).length()) && (open < close)) 244 326 { 327 // The next section startet, so our line isn't yet in the file - now we add it and safe the file 245 328 this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalue_); 246 329 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); … … 249 332 } 250 333 334 // Look out for the variable-name 251 335 if ((*it2).find(this->varname_) < (*it2).length()) 252 336 { 337 // We found the right line - safe it and return 253 338 this->configFileLine_ = it2; 254 339 success = true; … … 256 341 } 257 342 } 343 344 // Check if we succeeded 258 345 if (!success) 259 346 { 347 // Looks like we found the right section, but the file ended without containing our variable - so we add it and safe the file 260 348 this->configFileLine_ = this->configFileLines_s->insert(positionToPutNewLineAt, this->varname_ + "=" + this->defvalue_); 261 349 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); … … 265 353 } 266 354 } 355 356 // Check if we succeeded 267 357 if (!success) 268 358 { 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(); 359 // We obviously didn't found the right section, so we'll create it 360 this->configFileLines_s->push_back("[" + this->classname_ + "]"); // Create the section 361 this->configFileLines_s->push_back(this->varname_ + "=" + this->defvalue_); // Create the line 362 this->configFileLine_ = --this->configFileLines_s->end(); // Set the pointer to the last element 272 363 success = true; 273 this->configFileLines_s->push_back(""); 274 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); 275 } 276 } 277 364 this->configFileLines_s->push_back(""); // Add an empty line - this is needed for the algorithm in the searchConfigFileLine-function 365 ConfigValueContainer::writeConfigFile(CONFIGFILEPATH); // Save the changed config-file 366 } 367 } 368 369 /** 370 @brief Determines if a line in the config-file is a comment. 371 @param line The line to check 372 @return True = it's a comment 373 */ 278 374 bool ConfigValueContainer::isComment(const std::string& line) 279 375 { 376 // Strip the line, whitespaces are disturbing 280 377 std::string teststring = getStrippedLine(line); 281 378 379 // There are four possible comment-symbols: 380 // 1) #comment in script-language style 381 // 2) %comment in matlab style 382 // 3) ;comment in unreal tournament config-file style 383 // 4) //comment in code style 282 384 if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/')) 283 385 return true; … … 286 388 } 287 389 390 /** 391 @brief Determines if a line in the config-file is empty (contains only whitespaces). 392 @param line The line to check 393 @return True = it's empty 394 */ 288 395 bool ConfigValueContainer::isEmpty(const std::string& line) 289 396 { … … 291 398 } 292 399 400 /** 401 @brief Removes all whitespaces from a line. 402 @param line The line to strip 403 @return The stripped line 404 */ 293 405 std::string ConfigValueContainer::getStrippedLine(const std::string& line) 294 406 { … … 301 413 } 302 414 415 /** 416 @brief Returns the part in the corresponding config-file-entry of the container that defines the value. 417 @param bStripped True = strip the value-string 418 @return The value-string 419 */ 303 420 std::string ConfigValueContainer::getValueString(bool bStripped) 304 421 { … … 312 429 } 313 430 431 /** 432 @brief Reads the config-file and stores the lines in a list. 433 @param filename The name of the config-file 434 */ 314 435 void ConfigValueContainer::readConfigFile(const std::string& filename) 315 436 { 316 437 ConfigValueContainer::readConfigFile_s = true; 317 438 439 // Create the list if needed 318 440 if (!ConfigValueContainer::configFileLines_s) 319 441 ConfigValueContainer::configFileLines_s = new std::list<std::string>; 320 442 443 // This creates the file if it's not existing 321 444 std::ofstream createFile; 322 445 createFile.open(filename.c_str(), std::fstream::app); 323 446 createFile.close(); 324 447 448 // Open the file 325 449 std::ifstream file; 326 450 file.open(filename.c_str(), std::fstream::in); … … 328 452 char line[1024]; 329 453 454 // Iterate through the file and add the lines into the list 330 455 while (file.good() && !file.eof()) 331 456 { … … 335 460 } 336 461 462 // The last line is useless 337 463 ConfigValueContainer::configFileLines_s->pop_back(); 338 464 465 // Add an empty line to the end of the file if needed 466 // this is needed for the algorithm in the searchConfigFileLine-function 339 467 if ((ConfigValueContainer::configFileLines_s->size() > 0) && !isEmpty(*ConfigValueContainer::configFileLines_s->rbegin())) 340 468 { … … 346 474 } 347 475 476 /** 477 @param Writes the content of the list, containing all lines of the config-file, into the config-file. 478 @param filename The name of the config-file 479 */ 348 480 void ConfigValueContainer::writeConfigFile(const std::string& filename) 349 481 { 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 482 // Make sure we stored the config-file in the list 483 if (!ConfigValueContainer::readConfigFile_s) 484 ConfigValueContainer::readConfigFile(filename); 485 486 // Open the file 359 487 std::ofstream file; 360 488 file.open(filename.c_str(), std::fstream::out); 361 489 490 // Iterate through the list an write the lines into the file 362 491 std::list<std::string>::iterator it; 363 492 for(it = ConfigValueContainer::configFileLines_s->begin(); it != ConfigValueContainer::configFileLines_s->end(); ++it)
Note: See TracChangeset
for help on using the changeset viewer.