Changeset 6536 for code/trunk/src/libraries/core/ConfigFileManager.cc
- Timestamp:
- Mar 16, 2010, 11:22:36 AM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/gamestate (added) merged: 6430-6432,6437,6439-6440
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/ConfigFileManager.cc
r6425 r6536 33 33 #include "util/Convert.h" 34 34 #include "util/Math.h" 35 #include "util/StringUtils.h"36 35 #include "ConsoleCommand.h" 37 36 #include "ConfigValueContainer.h" … … 40 39 namespace orxonox 41 40 { 42 SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());43 SetConsoleCommandShortcutExtern(tconfig).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());44 SetConsoleCommandShortcutExtern(reloadConfig);45 SetConsoleCommandShortcutExtern(cleanConfig);46 SetConsoleCommandShortcutExtern(loadSettings).argumentCompleter(0, autocompletion::files());47 48 bool config(const std::string& classname, const std::string& varname, const std::string& value)49 {50 std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));51 if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())52 {53 std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname));54 if (variable != identifier->second->getLowercaseConfigValueMapEnd())55 return variable->second->set(value);56 }57 return false;58 }59 60 const std::string& getConfig(const std::string& classname, const std::string& varname)61 {62 return ConfigFileManager::getInstance().getValue(ConfigFileType::Settings, classname, varname, "", true);63 }64 65 bool tconfig(const std::string& classname, const std::string& varname, const std::string& value)66 {67 std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));68 if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())69 {70 std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname));71 if (variable != identifier->second->getLowercaseConfigValueMapEnd())72 return variable->second->tset(value);73 }74 return false;75 }76 77 void reloadConfig()78 {79 ConfigFileManager::getInstance().load();80 }81 82 void cleanConfig()83 {84 ConfigFileManager::getInstance().clean(false);85 }86 87 void loadSettings(const std::string& filename)88 {89 ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);90 }91 92 41 ////////////////////////// 93 42 // ConfigFileEntryValue // … … 101 50 // Assemble the entry line 102 51 this->fileEntry_ = this->getKeyString() + " = "; 103 if (this->bString_ )52 if (this->bString_ && !this->value_.empty()) 104 53 this->fileEntry_ += '"' + addSlashes(this->value_) + '"'; 105 54 else … … 145 94 } 146 95 147 unsigned int ConfigFileSection::getVectorSize(const std::string& name) 96 unsigned int ConfigFileSection::getVectorSize(const std::string& name) const 148 97 { 149 98 unsigned int size = 0; … … 166 115 } 167 116 168 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, const std::string& fallback, bool bString) 117 ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name) const 118 { 119 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 120 { 121 if ((*it)->getName() == name) 122 return *it; 123 } 124 return NULL; 125 } 126 127 ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name, unsigned int index) const 128 { 129 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 130 { 131 if (((*it)->getName() == name) && ((*it)->getIndex() == index)) 132 return *it; 133 } 134 return NULL; 135 } 136 137 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString) 169 138 { 170 139 for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) … … 179 148 this->bUpdated_ = true; 180 149 181 return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryValue(name, fallback, bString)));182 } 183 184 std::list<ConfigFileEntry*>::iterator ConfigFileSection::get EntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)150 return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString)); 151 } 152 153 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 185 154 { 186 155 for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) … … 196 165 197 166 if (index == 0) 198 return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));167 return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString)); 199 168 else 200 return this->entries_.insert(++this->get EntryIterator(name, index - 1, "", bString), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));169 return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString)); 201 170 } 202 171 … … 205 174 // ConfigFile // 206 175 //////////////// 176 177 const char* ConfigFile::DEFAULT_CONFIG_FOLDER = "defaultConfig"; 178 179 ConfigFile::ConfigFile(const std::string& filename, bool bCopyFallbackFile) 180 : filename_(filename) 181 , bCopyFallbackFile_(bCopyFallbackFile) 182 , bUpdated_(false) 183 { 184 } 185 207 186 ConfigFile::~ConfigFile() 208 187 { … … 210 189 } 211 190 212 void ConfigFile::load( bool bCreateIfNotExisting)191 void ConfigFile::load() 213 192 { 214 193 // Be sure we start from new in the memory 215 194 this->clear(); 216 195 217 // Get default file if necessary and available 218 boost::filesystem::path filepath(PathConfig::getConfigPath() / this->filename_); 219 if (!boost::filesystem::exists(filepath)) 220 { 221 // Try to get default one from the data folder 222 boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / "defaultConfig" / this->filename_); 223 if (boost::filesystem::exists(defaultFilepath)) 224 { 225 COUT(3) << "Copied " << this->filename_ << " from the defaultConfig folder." << std::endl; 226 boost::filesystem::copy_file(defaultFilepath, filepath); 196 boost::filesystem::path filepath(this->filename_); 197 if (!filepath.is_complete()) 198 { 199 filepath = PathConfig::getConfigPath() / filepath; 200 if (this->bCopyFallbackFile_) 201 { 202 // Look for default file in the data folder 203 if (!boost::filesystem::exists(filepath)) 204 { 205 boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / DEFAULT_CONFIG_FOLDER / this->filename_); 206 if (boost::filesystem::exists(defaultFilepath)) 207 { 208 // Try to copy default file from the data folder 209 try 210 { 211 boost::filesystem::copy_file(defaultFilepath, filepath); 212 COUT(3) << "Copied " << this->filename_ << " from the default config folder." << std::endl; 213 } 214 catch (const boost::filesystem::filesystem_error& ex) 215 { COUT(1) << "Error in ConfigFile: " << ex.what() << std::endl; } 216 } 217 } 227 218 } 228 219 } … … 300 291 { 301 292 // New array 302 std::list<ConfigFileEntry*>::iterator it = newsection->get EntryIterator(getStripped(line.substr(0, pos2)), index, value, false);293 std::list<ConfigFileEntry*>::iterator it = newsection->getOrCreateEntryIterator(getStripped(line.substr(0, pos2)), index, value, false); 303 294 (*it)->setValue(value); 304 295 (*it)->setComment(comment); … … 319 310 COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; 320 311 321 // Save the file in case something changed (like stripped whitespaces) 322 this->save(); 323 324 // Update all ConfigValueContainers 325 this->updateConfigValues(); 312 // DO NOT save the file --> we can open supposedly read only config files 326 313 } // end file.is_open() 327 314 } … … 329 316 void ConfigFile::save() const 330 317 { 318 this->saveAs(this->filename_); 319 } 320 321 void ConfigFile::saveAs(const std::string& filename) const 322 { 323 boost::filesystem::path filepath(filename); 324 if (!filepath.is_complete()) 325 filepath = PathConfig::getConfigPath() / filename; 331 326 std::ofstream file; 332 file.open( (PathConfig::getConfigPathString() + filename_).c_str(), std::fstream::out);327 file.open(filepath.string().c_str(), std::fstream::out); 333 328 file.setf(std::ios::fixed, std::ios::floatfield); 334 329 file.precision(6); … … 336 331 if (!file.is_open()) 337 332 { 338 COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl; 339 COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl; 333 COUT(1) << "Error: Couldn't open config-file \"" << filename << "\"." << std::endl; 340 334 return; 341 335 } … … 346 340 347 341 for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries) 348 {349 342 file << (*it_entries)->getFileEntry() << std::endl; 350 }351 343 352 344 file << std::endl; … … 355 347 file.close(); 356 348 357 COUT(4) << "Saved config file \"" << this->filename_ << "\"." << std::endl; 358 } 359 360 void ConfigFile::saveAs(const std::string& filename) 361 { 362 std::string temp = this->filename_; 363 this->filename_ = filename; 364 this->save(); 365 this->filename_ = temp; 366 } 367 368 void ConfigFile::clean(bool bCleanComments) 369 { 370 for (std::list<ConfigFileSection*>::iterator it1 = this->sections_.begin(); it1 != this->sections_.end(); ) 371 { 372 std::map<std::string, Identifier*>::const_iterator it2 = Identifier::getStringIdentifierMap().find((*it1)->getName()); 373 if (it2 != Identifier::getStringIdentifierMapEnd() && it2->second->hasConfigValues()) 374 { 375 // The section exists, delete comment 376 if (bCleanComments) 377 (*it1)->setComment(""); 378 for (std::list<ConfigFileEntry*>::iterator it3 = (*it1)->entries_.begin(); it3 != (*it1)->entries_.end(); ) 379 { 380 std::map<std::string, ConfigValueContainer*>::const_iterator it4 = it2->second->getConfigValueMap().find((*it3)->getName()); 381 if (it4 != it2->second->getConfigValueMapEnd()) 382 { 383 // The config-value exists, delete comment 384 if (bCleanComments) 385 (*it3)->setComment(""); 386 ++it3; 387 } 388 else 389 { 390 // The config-value doesn't exist 391 delete (*it3); 392 (*it1)->entries_.erase(it3++); 393 } 394 } 395 ++it1; 396 } 397 else 398 { 399 // The section doesn't exist 400 delete (*it1); 401 this->sections_.erase(it1++); 402 } 403 } 404 405 // Save the file 406 this->save(); 349 COUT(4) << "Saved config file \"" << filename << "\"." << std::endl; 407 350 } 408 351 … … 414 357 } 415 358 416 ConfigFileSection* ConfigFile::getSection(const std::string& section) 359 const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) 360 { 361 const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString); 362 this->saveIfUpdated(); 363 return output; 364 } 365 366 const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) 367 { 368 const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString); 369 this->saveIfUpdated(); 370 return output; 371 } 372 373 void ConfigFile::deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex) 374 { 375 if (ConfigFileSection* sectionPtr = this->getSection(section)) 376 { 377 sectionPtr->deleteVectorEntries(name, startindex); 378 this->save(); 379 } 380 } 381 382 ConfigFileSection* ConfigFile::getSection(const std::string& section) const 383 { 384 for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) 385 if ((*it)->getName() == section) 386 return (*it); 387 return NULL; 388 } 389 390 ConfigFileSection* ConfigFile::getOrCreateSection(const std::string& section) 417 391 { 418 392 for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) … … 445 419 } 446 420 447 void ConfigFile::updateConfigValues() 448 { 449 if (this->type_ == ConfigFileType::Settings) 450 { 451 for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapBegin(); it != Identifier::getStringIdentifierMapEnd(); ++it) 452 { 453 if (it->second->hasConfigValues()) 421 422 //////////////////////// 423 // SettingsConfigFile // 424 //////////////////////// 425 426 SettingsConfigFile* SettingsConfigFile::singletonPtr_s = 0; 427 428 SettingsConfigFile::SettingsConfigFile(const std::string& filename) 429 : ConfigFile(filename) 430 { 431 ConsoleCommand* command = createConsoleCommand(createFunctor(&ConfigFile::load, this), "reloadSettings"); 432 CommandExecutor::addConsoleCommandShortcut(command); 433 command = createConsoleCommand(createFunctor(&SettingsConfigFile::setFilename, this), "setSettingsFile"); 434 CommandExecutor::addConsoleCommandShortcut(command); 435 command = createConsoleCommand(createFunctor(&SettingsConfigFile::config, this), "config"); 436 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); 437 command = createConsoleCommand(createFunctor(&SettingsConfigFile::tconfig, this), "tconfig"); 438 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); 439 command = createConsoleCommand(createFunctor(&SettingsConfigFile::getConfig, this), "getConfig"); 440 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()); 441 } 442 443 SettingsConfigFile::~SettingsConfigFile() 444 { 445 } 446 447 void SettingsConfigFile::load() 448 { 449 ConfigFile::load(); 450 this->updateConfigValues(); 451 } 452 453 void SettingsConfigFile::setFilename(const std::string& filename) 454 { 455 ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename); 456 } 457 458 void SettingsConfigFile::addConfigValueContainer(ConfigValueContainer* container) 459 { 460 if (container == NULL) 461 return; 462 std::pair<std::string, ConfigValueContainer*> second(getLowercase(container->getName()), container); 463 this->containers_.insert(std::make_pair(getLowercase(container->getSectionName()), second)); 464 this->sectionNames_.insert(container->getSectionName()); 465 } 466 467 void SettingsConfigFile::removeConfigValueContainer(ConfigValueContainer* container) 468 { 469 if (container == NULL) 470 return; 471 const std::string& sectionLC = getLowercase(container->getSectionName()); 472 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 473 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 474 { 475 if (it->second.second == container) 476 { 477 // Remove entry from section name set this was the last container for that section 478 if (upper == this->containers_.lower_bound(sectionLC)) 479 this->sectionNames_.erase(container->getSectionName()); 480 this->containers_.erase(it); 481 break; 482 } 483 } 484 } 485 486 void SettingsConfigFile::updateConfigValues() 487 { 488 for (ContainerMap::const_iterator it = this->containers_.begin(); it != this->containers_.end(); ++it) 489 { 490 it->second.second->update(); 491 it->second.second->getIdentifier()->updateConfigValues(); 492 } 493 } 494 495 void SettingsConfigFile::clean(bool bCleanComments) 496 { 497 for (std::list<ConfigFileSection*>::iterator itSection = this->sections_.begin(); itSection != this->sections_.end(); ) 498 { 499 const std::string& sectionLC = getLowercase((*itSection)->getName()); 500 ContainerMap::const_iterator lower = this->containers_.lower_bound(sectionLC); 501 ContainerMap::const_iterator upper = this->containers_.upper_bound(sectionLC); 502 if (lower != upper) 503 { 504 // The section exists, delete comment 505 if (bCleanComments) 506 (*itSection)->setComment(""); 507 for (std::list<ConfigFileEntry*>::iterator itEntry = (*itSection)->entries_.begin(); itEntry != (*itSection)->entries_.end(); ) 454 508 { 455 for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = it->second->getConfigValueMapBegin(); it2 != it->second->getConfigValueMapEnd(); ++it2) 456 it2->second->update(); 457 458 it->second->updateConfigValues(); 509 const std::string& entryLC = getLowercase((*itEntry)->getName()); 510 bool bFound = false; 511 for (ContainerMap::const_iterator itContainer = lower; itContainer != upper; ++itContainer) 512 { 513 if (itContainer->second.first == entryLC) 514 { 515 // The config-value exists, delete comment 516 if (bCleanComments) 517 (*itEntry)->setComment(""); 518 ++itEntry; 519 bFound = true; 520 break; 521 } 522 } 523 if (!bFound) 524 { 525 // The config-value doesn't exist 526 delete (*itEntry); 527 (*itSection)->entries_.erase(itEntry++); 528 } 459 529 } 460 } 461 } 530 ++itSection; 531 } 532 else 533 { 534 // The section doesn't exist 535 delete (*itSection); 536 this->sections_.erase(itSection++); 537 } 538 } 539 540 // Save the file 541 this->save(); 542 } 543 544 bool SettingsConfigFile::config(const std::string& section, const std::string& entry, const std::string& value) 545 { 546 return this->configImpl(section, entry, value, &ConfigValueContainer::set); 547 } 548 549 bool SettingsConfigFile::tconfig(const std::string& section, const std::string& entry, const std::string& value) 550 { 551 return this->configImpl(section, entry, value, &ConfigValueContainer::tset); 552 } 553 554 bool SettingsConfigFile::configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)) 555 { 556 const std::string& sectionLC = getLowercase(section); 557 const std::string& entryLC = getLowercase(entry); 558 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 559 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 560 { 561 // Note: Config value vectors cannot be supported 562 if (it->second.first == entryLC && !it->second.second->isVector()) 563 { 564 return (it->second.second->*function)(value); 565 } 566 } 567 return false; 568 } 569 570 std::string SettingsConfigFile::getConfig(const std::string& section, const std::string& entry) 571 { 572 const std::string& sectionLC = getLowercase(section); 573 const std::string& entryLC = getLowercase(entry); 574 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 575 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 576 { 577 // Note: Config value vectors cannot be supported 578 if (it->second.first == entryLC && ! it->second.second->isVector()) 579 { 580 std::string value; 581 it->second.second->getValue<std::string, OrxonoxClass>(&value, NULL); 582 return value; 583 } 584 } 585 return ""; 462 586 } 463 587 … … 469 593 ConfigFileManager* ConfigFileManager::singletonPtr_s = 0; 470 594 471 std::string ConfigFileManager::DEFAULT_CONFIG_FILE = "default.ini";472 473 595 ConfigFileManager::ConfigFileManager() 474 : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)475 {596 { 597 this->configFiles_.assign(NULL); 476 598 } 477 599 478 600 ConfigFileManager::~ConfigFileManager() 479 601 { 480 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ) 481 delete (it++)->second; 482 } 483 484 void ConfigFileManager::setFilename(ConfigFileType type, const std::string& filename) 485 { 486 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 487 if (it != this->configFiles_.end()) 488 { 489 assert(it->second); 490 delete it->second; 491 } 492 this->configFiles_[type] = new ConfigFile(filename, type); 493 this->load(type); 494 } 495 496 void ConfigFileManager::load() 497 { 498 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 499 it->second->load(); 500 } 501 502 void ConfigFileManager::save() 503 { 504 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 505 it->second->save(); 506 } 507 508 void ConfigFileManager::clean(bool bCleanComments) 509 { 510 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 511 this->clean(it->first, bCleanComments); 512 } 513 514 void ConfigFileManager::load(ConfigFileType type) 515 { 516 this->getFile(type)->load(); 517 } 518 519 void ConfigFileManager::save(ConfigFileType type) 520 { 521 this->getFile(type)->save(); 522 } 523 524 void ConfigFileManager::saveAs(ConfigFileType type, const std::string& saveFilename) 525 { 526 this->getFile(type)->saveAs(saveFilename); 527 } 528 529 void ConfigFileManager::clean(ConfigFileType type, bool bCleanComments) 530 { 531 this->getFile(type)->clean(bCleanComments); 532 } 533 534 void ConfigFileManager::updateConfigValues() 535 { 536 for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 537 it->second->updateConfigValues(); 538 } 539 540 void ConfigFileManager::updateConfigValues(ConfigFileType type) 541 { 542 this->getFile(type)->updateConfigValues(); 543 } 544 545 const std::string& ConfigFileManager::getFilename(ConfigFileType type) 546 { 547 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 548 if (it != this->configFiles_.end()) 549 return it->second->getFilename(); 550 else 551 return BLANKSTRING; 552 } 553 554 ConfigFile* ConfigFileManager::getFile(ConfigFileType type) 555 { 556 std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type); 557 if (it != this->configFiles_.end()) 558 return it->second; 559 else 560 { 561 COUT(1) << "ConfigFileManager: Can't find a config file for type with ID " << static_cast<int>(type) << std::endl; 562 COUT(1) << "Using " << DEFAULT_CONFIG_FILE << " file." << std::endl; 563 this->setFilename(type, DEFAULT_CONFIG_FILE); 564 return getFile(type); 565 } 602 for (boost::array<ConfigFile*, 3>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 603 if (*it) 604 delete (*it); 605 } 606 607 void ConfigFileManager::setFilename(ConfigFileType::Value type, const std::string& filename) 608 { 609 if (this->getConfigFile(type)) 610 delete this->configFiles_[type]; 611 // Create and load config file 612 switch (type) 613 { 614 case ConfigFileType::Settings: 615 this->configFiles_[type] = new SettingsConfigFile(filename); 616 break; 617 case ConfigFileType::JoyStickCalibration: 618 case ConfigFileType::CommandHistory: 619 this->configFiles_[type] = new ConfigFile(filename); 620 break; 621 } 622 this->configFiles_[type]->load(); 566 623 } 567 624 }
Note: See TracChangeset
for help on using the changeset viewer.