Changeset 2103 for code/trunk/src/core/ConfigFileManager.cc
- Timestamp:
- Nov 2, 2008, 12:22:42 AM (17 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed (with no actual effect on merging)
-
code/trunk/src/core/ConfigFileManager.cc
r1889 r2103 28 28 29 29 #include "ConfigFileManager.h" 30 #include "ConfigValueContainer.h" 31 #include "ConsoleCommand.h" 32 #include "Identifier.h" 30 31 #include <cassert> 33 32 #include "util/Convert.h" 34 33 #include "util/String.h" 35 34 #include "ConsoleCommand.h" 35 #include "ConfigValueContainer.h" 36 36 37 37 namespace orxonox … … 39 39 const int CONFIG_FILE_MAX_LINELENGHT = 1024; 40 40 const char* const DEFAULT_CONFIG_FILE = "default.ini"; 41 42 ConfigFileManager* ConfigFileManager::singletonRef_s = 0; 41 43 42 44 SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue()); … … 45 47 SetConsoleCommandShortcutExtern(cleanConfig); 46 48 SetConsoleCommandShortcutExtern(loadSettings).argumentCompleter(0, autocompletion::files()); 47 SetConsoleCommandShortcutExtern(loadKeybindings).argumentCompleter(0, autocompletion::files());48 49 49 50 bool config(const std::string& classname, const std::string& varname, const std::string& value) … … 83 84 void loadSettings(const std::string& filename) 84 85 { 85 ConfigFileManager::getInstance().setFile(CFT_Settings, filename, false); 86 } 87 88 void loadKeybindings(const std::string& filename) 89 { 90 ConfigFileManager::getInstance().setFile(CFT_Keybindings, filename); 91 } 92 86 ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename); 87 } 93 88 94 89 ////////////////////////// … … 121 116 122 117 123 /////////////////////////////// 118 //////////////////////////////// 124 119 // ConfigFileEntryVectorValue // 125 /////////////////////////////// 120 //////////////////////////////// 126 121 std::string ConfigFileEntryVectorValue::getFileEntry() const 127 122 { … … 217 212 ConfigFile::~ConfigFile() 218 213 { 219 for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ) 220 delete (*(it++)); 214 this->clear(); 221 215 } 222 216 223 217 void ConfigFile::load(bool bCreateIfNotExisting) 224 218 { 225 if (bCreateIfNotExisting)226 {227 // This creates the default config file if it's not existing 228 std::ofstream createFile;229 createFile.open(this->filename_.c_str(), std::fstream::app);230 createFile.close();231 }219 // Be sure we start from new 220 this->clear(); 221 222 // This creates the config file if it's not existing 223 std::ofstream createFile; 224 createFile.open(this->filename_.c_str(), std::fstream::app); 225 createFile.close(); 232 226 233 227 // Open the file … … 329 323 file.close(); 330 324 331 COUT( 0) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;325 COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; 332 326 333 327 // Save the file in case something changed (like stripped whitespaces) 334 328 this->save(); 329 330 // Update all ConfigValueContainers 331 this->updateConfigValues(); 335 332 } 336 333 … … 366 363 } 367 364 368 void ConfigFile::save (const std::string& filename)365 void ConfigFile::saveAs(const std::string& filename) 369 366 { 370 367 std::string temp = this->filename_; … … 415 412 } 416 413 414 void ConfigFile::clear() 415 { 416 for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ) 417 delete (*(it++)); 418 this->sections_.clear(); 419 } 420 417 421 ConfigFileSection* ConfigFile::getSection(const std::string& section) 418 422 { … … 446 450 } 447 451 452 void ConfigFile::updateConfigValues() 453 { 454 if (this->type_ == ConfigFileType::Settings) 455 { 456 for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getIdentifierMapBegin(); it != Identifier::getIdentifierMapEnd(); ++it) 457 { 458 if (it->second->hasConfigValues()) 459 { 460 for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = (*it).second->getConfigValueMapBegin(); it2 != (*it).second->getConfigValueMapEnd(); ++it2) 461 it2->second->update(); 462 463 it->second->updateConfigValues(); 464 } 465 } 466 } 467 } 468 448 469 449 470 /////////////////////// 450 471 // ConfigFileManager // 451 472 /////////////////////// 473 452 474 ConfigFileManager::ConfigFileManager() 453 { 454 this->setFile(CFT_Settings, DEFAULT_CONFIG_FILE); 475 : mininmalFreeType_(ConfigFileType::numberOfReservedTypes) 476 { 477 assert(singletonRef_s == 0); 478 singletonRef_s = this; 455 479 } 456 480 … … 458 482 { 459 483 for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ) 460 delete (*(it++)).second; 461 } 462 463 ConfigFileManager& ConfigFileManager::getInstance() 464 { 465 static ConfigFileManager instance; 466 return instance; 467 } 468 469 void ConfigFileManager::setFile(ConfigFileType type, const std::string& filename, bool bCreateIfNotExisting) 484 delete (it++)->second; 485 486 assert(singletonRef_s != 0); 487 singletonRef_s = 0; 488 } 489 490 void ConfigFileManager::setFilename(ConfigFileType type, const std::string& filename) 470 491 { 471 492 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 472 493 if (it != this->configFiles_.end()) 473 if ((*it).second != 0) 474 delete (*it).second; 475 476 this->configFiles_[type] = new ConfigFile(this->getFilePath(filename)); 477 this->load(type, bCreateIfNotExisting); 478 } 479 480 void ConfigFileManager::load(bool bCreateIfNotExisting) 494 { 495 assert(it->second); 496 delete it->second; 497 } 498 this->configFiles_[type] = new ConfigFile(filename, type); 499 this->load(type); 500 } 501 502 void ConfigFileManager::load() 481 503 { 482 504 for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 483 (*it).second->load(bCreateIfNotExisting); 484 485 this->updateConfigValues(); 505 it->second->load(); 486 506 } 487 507 … … 489 509 { 490 510 for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 491 (*it).second->save();511 it->second->save(); 492 512 } 493 513 … … 495 515 { 496 516 for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 497 this->clean((*it).first, bCleanComments); 498 } 499 500 void ConfigFileManager::load(ConfigFileType type, bool bCreateIfNotExisting) 501 { 502 this->getFile(type)->load(bCreateIfNotExisting); 503 this->updateConfigValues(type); 517 this->clean(it->first, bCleanComments); 518 } 519 520 void ConfigFileManager::load(ConfigFileType type) 521 { 522 this->getFile(type)->load(); 504 523 } 505 524 … … 509 528 } 510 529 511 void ConfigFileManager::save (ConfigFileType type, const std::string& filename)512 { 513 this->getFile(type)->save (filename);530 void ConfigFileManager::saveAs(ConfigFileType type, const std::string& saveFilename) 531 { 532 this->getFile(type)->saveAs(saveFilename); 514 533 } 515 534 … … 519 538 } 520 539 521 void ConfigFileManager::updateConfigValues() const540 void ConfigFileManager::updateConfigValues() 522 541 { 523 542 for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 524 this->updateConfigValues((*it).first); 525 } 526 527 void ConfigFileManager::updateConfigValues(ConfigFileType type) const 528 { 529 if (type == CFT_Settings) 530 { 531 for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getIdentifierMapBegin(); it != Identifier::getIdentifierMapEnd(); ++it) 532 { 533 if ((*it).second->hasConfigValues() /* && (*it).second != ClassIdentifier<KeyBinder>::getIdentifier()*/) 534 { 535 for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = (*it).second->getConfigValueMapBegin(); it2 != (*it).second->getConfigValueMapEnd(); ++it2) 536 (*it2).second->update(); 537 538 (*it).second->updateConfigValues(); 539 } 540 } 541 } 542 else if (type == CFT_Keybindings) 543 { 544 // todo 545 } 543 it->second->updateConfigValues(); 544 } 545 546 void ConfigFileManager::updateConfigValues(ConfigFileType type) 547 { 548 this->getFile(type)->updateConfigValues(); 549 } 550 551 const std::string& ConfigFileManager::getFilename(ConfigFileType type) 552 { 553 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 554 if (it != this->configFiles_.end()) 555 return it->second->getFilename(); 556 else 557 return BLANKSTRING; 546 558 } 547 559 548 560 ConfigFile* ConfigFileManager::getFile(ConfigFileType type) 549 561 { 550 std::map<ConfigFileType, ConfigFile*>:: iterator it = this->configFiles_.find(type);562 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 551 563 if (it != this->configFiles_.end()) 552 return (*it).second; 553 554 if (type == CFT_Settings) 555 return this->configFiles_[type] = new ConfigFile(DEFAULT_CONFIG_FILE); 556 else 557 return this->configFiles_[type] = new ConfigFile(""); 558 } 559 560 std::string ConfigFileManager::getFilePath(const std::string& name) const 561 { 562 return name; 564 return it->second; 565 else 566 { 567 COUT(1) << "ConfigFileManager: Can't find a config file for type with ID " << (int)type << std::endl; 568 COUT(1) << "Using " << DEFAULT_CONFIG_FILE << " file." << std::endl; 569 this->setFilename(type, DEFAULT_CONFIG_FILE); 570 return getFile(type); 571 } 563 572 } 564 573 }
Note: See TracChangeset
for help on using the changeset viewer.