- Timestamp:
- Dec 29, 2009, 10:30:19 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gamestate/src/libraries/core/ConfigFileManager.cc
r6425 r6432 40 40 namespace orxonox 41 41 { 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 42 ////////////////////////// 93 43 // ConfigFileEntryValue // … … 101 51 // Assemble the entry line 102 52 this->fileEntry_ = this->getKeyString() + " = "; 103 if (this->bString_ )53 if (this->bString_ && !this->value_.empty()) 104 54 this->fileEntry_ += '"' + addSlashes(this->value_) + '"'; 105 55 else … … 145 95 } 146 96 147 unsigned int ConfigFileSection::getVectorSize(const std::string& name) 97 unsigned int ConfigFileSection::getVectorSize(const std::string& name) const 148 98 { 149 99 unsigned int size = 0; … … 166 116 } 167 117 168 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, const std::string& fallback, bool bString) 118 std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name) const 119 { 120 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 121 { 122 if ((*it)->getName() == name) 123 return it; 124 } 125 return this->entries_.end(); 126 } 127 128 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString) 169 129 { 170 130 for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) … … 179 139 this->bUpdated_ = true; 180 140 181 return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryValue(name, fallback, bString))); 182 } 183 184 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 141 return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString)); 142 } 143 144 std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index) const 145 { 146 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 147 { 148 if (((*it)->getName() == name) && ((*it)->getIndex() == index)) 149 return it; 150 } 151 return this->entries_.end(); 152 } 153 154 std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 185 155 { 186 156 for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) … … 196 166 197 167 if (index == 0) 198 return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));168 return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString)); 199 169 else 200 return this->entries_.insert(++this->get EntryIterator(name, index - 1, "", bString), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));170 return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString)); 201 171 } 202 172 … … 205 175 // ConfigFile // 206 176 //////////////// 177 ConfigFile::ConfigFile(const std::string& filename) 178 : filename_(filename) 179 , bUpdated_(false) 180 { 181 } 182 207 183 ConfigFile::~ConfigFile() 208 184 { … … 210 186 } 211 187 212 void ConfigFile::load( bool bCreateIfNotExisting)188 void ConfigFile::load() 213 189 { 214 190 // Be sure we start from new in the memory … … 300 276 { 301 277 // New array 302 std::list<ConfigFileEntry*>::iterator it = newsection->get EntryIterator(getStripped(line.substr(0, pos2)), index, value, false);278 std::list<ConfigFileEntry*>::iterator it = newsection->getOrCreateEntryIterator(getStripped(line.substr(0, pos2)), index, value, false); 303 279 (*it)->setValue(value); 304 280 (*it)->setComment(comment); … … 319 295 COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; 320 296 321 // Save the file in case something changed (like stripped white spaces)297 // Save the file in case something changed (like stripped white space) 322 298 this->save(); 323 324 // Update all ConfigValueContainers325 this->updateConfigValues();326 299 } // end file.is_open() 327 300 } … … 329 302 void ConfigFile::save() const 330 303 { 304 this->saveAs(this->filename_); 305 } 306 307 void ConfigFile::saveAs(const std::string& filename) const 308 { 331 309 std::ofstream file; 332 file.open((PathConfig::getConfigPathString() + filename _).c_str(), std::fstream::out);310 file.open((PathConfig::getConfigPathString() + filename).c_str(), std::fstream::out); 333 311 file.setf(std::ios::fixed, std::ios::floatfield); 334 312 file.precision(6); … … 336 314 if (!file.is_open()) 337 315 { 338 COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl; 339 COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl; 316 COUT(1) << "Error: Couldn't open config-file \"" << filename << "\"." << std::endl; 340 317 return; 341 318 } … … 346 323 347 324 for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries) 348 {349 325 file << (*it_entries)->getFileEntry() << std::endl; 350 }351 326 352 327 file << std::endl; … … 355 330 file.close(); 356 331 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(); 332 COUT(4) << "Saved config file \"" << filename << "\"." << std::endl; 407 333 } 408 334 … … 414 340 } 415 341 416 ConfigFileSection* ConfigFile::getSection(const std::string& section) 342 const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) 343 { 344 const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString); 345 this->saveIfUpdated(); 346 return output; 347 } 348 349 const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) 350 { 351 const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString); 352 this->saveIfUpdated(); 353 return output; 354 } 355 356 void ConfigFile::deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex) 357 { 358 if (ConfigFileSection* sectionPtr = this->getSection(section)) 359 { 360 sectionPtr->deleteVectorEntries(name, startindex); 361 this->save(); 362 } 363 } 364 365 ConfigFileSection* ConfigFile::getSection(const std::string& section) const 366 { 367 for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) 368 if ((*it)->getName() == section) 369 return (*it); 370 return NULL; 371 } 372 373 ConfigFileSection* ConfigFile::getOrCreateSection(const std::string& section) 417 374 { 418 375 for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) … … 445 402 } 446 403 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()) 404 405 //////////////////////// 406 // SettingsConfigFile // 407 //////////////////////// 408 409 SettingsConfigFile* SettingsConfigFile::singletonPtr_s = 0; 410 411 SettingsConfigFile::SettingsConfigFile(const std::string& filename) 412 : ConfigFile(filename) 413 { 414 ConsoleCommand* command = createConsoleCommand(createFunctor(&ConfigFile::load, this), "reloadSettings"); 415 CommandExecutor::addConsoleCommandShortcut(command); 416 command = createConsoleCommand(createFunctor(&SettingsConfigFile::setFilename, this), "setSettingsFile"); 417 CommandExecutor::addConsoleCommandShortcut(command); 418 command = createConsoleCommand(createFunctor(&SettingsConfigFile::config, this), "config"); 419 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); 420 command = createConsoleCommand(createFunctor(&SettingsConfigFile::tconfig, this), "tconfig"); 421 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); 422 command = createConsoleCommand(createFunctor(&SettingsConfigFile::getConfig, this), "getConfig"); 423 CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()); 424 } 425 426 SettingsConfigFile::~SettingsConfigFile() 427 { 428 } 429 430 void SettingsConfigFile::load() 431 { 432 ConfigFile::load(); 433 this->updateConfigValues(); 434 } 435 436 void SettingsConfigFile::setFilename(const std::string& filename) 437 { 438 ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename); 439 } 440 441 void SettingsConfigFile::addConfigValueContainer(ConfigValueContainer* container) 442 { 443 if (container == NULL) 444 return; 445 std::pair<std::string, ConfigValueContainer*> second(getLowercase(container->getName()), container); 446 this->containers_.insert(std::make_pair(getLowercase(container->getSectionName()), second)); 447 this->sectionNames_.insert(container->getSectionName()); 448 } 449 450 void SettingsConfigFile::removeConfigValueContainer(ConfigValueContainer* container) 451 { 452 if (container == NULL) 453 return; 454 const std::string& sectionLC = getLowercase(container->getSectionName()); 455 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 456 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 457 { 458 if (it->second.second == container) 459 { 460 // Remove entry from section name set this was the last container for that section 461 if (upper == this->containers_.lower_bound(sectionLC)) 462 this->sectionNames_.erase(container->getSectionName()); 463 this->containers_.erase(it); 464 break; 465 } 466 } 467 } 468 469 void SettingsConfigFile::updateConfigValues() 470 { 471 for (ContainerMap::const_iterator it = this->containers_.begin(); it != this->containers_.end(); ++it) 472 { 473 it->second.second->update(); 474 it->second.second->getIdentifier()->updateConfigValues(); 475 } 476 } 477 478 void SettingsConfigFile::clean(bool bCleanComments) 479 { 480 for (std::list<ConfigFileSection*>::iterator itSection = this->sections_.begin(); itSection != this->sections_.end(); ) 481 { 482 const std::string& sectionLC = getLowercase((*itSection)->getName()); 483 ContainerMap::const_iterator lower = this->containers_.lower_bound(sectionLC); 484 ContainerMap::const_iterator upper = this->containers_.upper_bound(sectionLC); 485 if (lower != upper) 486 { 487 // The section exists, delete comment 488 if (bCleanComments) 489 (*itSection)->setComment(""); 490 for (std::list<ConfigFileEntry*>::iterator itEntry = (*itSection)->entries_.begin(); itEntry != (*itSection)->entries_.end(); ) 454 491 { 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(); 492 const std::string& entryLC = getLowercase((*itEntry)->getName()); 493 bool bFound = false; 494 for (ContainerMap::const_iterator itContainer = lower; itContainer != upper; ++itContainer) 495 { 496 if (itContainer->second.first == entryLC) 497 { 498 // The config-value exists, delete comment 499 if (bCleanComments) 500 (*itEntry)->setComment(""); 501 ++itEntry; 502 bFound = true; 503 break; 504 } 505 } 506 if (!bFound) 507 { 508 // The config-value doesn't exist 509 delete (*itEntry); 510 (*itSection)->entries_.erase(itEntry++); 511 } 459 512 } 460 } 461 } 513 ++itSection; 514 } 515 else 516 { 517 // The section doesn't exist 518 delete (*itSection); 519 this->sections_.erase(itSection++); 520 } 521 } 522 523 // Save the file 524 this->save(); 525 } 526 527 bool SettingsConfigFile::config(const std::string& section, const std::string& entry, const std::string& value) 528 { 529 return this->configImpl(section, entry, value, &ConfigValueContainer::set); 530 } 531 532 bool SettingsConfigFile::tconfig(const std::string& section, const std::string& entry, const std::string& value) 533 { 534 return this->configImpl(section, entry, value, &ConfigValueContainer::tset); 535 } 536 537 bool SettingsConfigFile::configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)) 538 { 539 const std::string& sectionLC = getLowercase(section); 540 const std::string& entryLC = getLowercase(entry); 541 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 542 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 543 { 544 // Note: Config value vectors cannot be supported 545 if (it->second.first == entryLC && !it->second.second->isVector()) 546 { 547 return (it->second.second->*function)(value); 548 } 549 } 550 return false; 551 } 552 553 std::string SettingsConfigFile::getConfig(const std::string& section, const std::string& entry) 554 { 555 const std::string& sectionLC = getLowercase(section); 556 const std::string& entryLC = getLowercase(entry); 557 ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); 558 for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) 559 { 560 // Note: Config value vectors cannot be supported 561 if (it->second.first == entryLC && ! it->second.second->isVector()) 562 { 563 std::string value; 564 it->second.second->getValue<std::string, OrxonoxClass>(&value, NULL); 565 return value; 566 } 567 } 568 return ""; 462 569 } 463 570 … … 469 576 ConfigFileManager* ConfigFileManager::singletonPtr_s = 0; 470 577 471 std::string ConfigFileManager::DEFAULT_CONFIG_FILE = "default.ini";472 473 578 ConfigFileManager::ConfigFileManager() 474 : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)475 {579 { 580 this->configFiles_.assign(NULL); 476 581 } 477 582 478 583 ConfigFileManager::~ConfigFileManager() 479 584 { 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 } 585 for (boost::array<ConfigFile*, 3>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) 586 if (*it) 587 delete (*it); 588 } 589 590 void ConfigFileManager::setFilename(ConfigFileType::Value type, const std::string& filename) 591 { 592 if (this->getConfigFile(type)) 593 delete this->configFiles_[type]; 594 // Create and load config file 595 switch (type) 596 { 597 case ConfigFileType::Settings: 598 this->configFiles_[type] = new SettingsConfigFile(filename); 599 break; 600 case ConfigFileType::JoyStickCalibration: 601 case ConfigFileType::CommandHistory: 602 this->configFiles_[type] = new ConfigFile(filename); 603 break; 604 } 605 this->configFiles_[type]->load(); 566 606 } 567 607 }
Note: See TracChangeset
for help on using the changeset viewer.