//----------------------------------------------------------------------- void ConfigFileEx::save(const DataStreamExPtr& _dataStream, const String& _separator, const String& _comments) { _dataStream->setEncoding("UTF-8"); SettingsBySection::iterator secIt; for(secIt = mSettings.begin(); secIt!=mSettings.end(); secIt++) { if(secIt->first.size() > 0) { String line = "[" + secIt->first + "]"; _dataStream->writeLine(line, EOL::CRLF); } SettingsMultiMap* sec = secIt->second; for(SettingsMultiMap::iterator it = sec->begin(); it != sec->end(); it++) { const String& key = it->first; const String& value = it->second; if(key.find_first_of(_comments) != 0) { String line = key + _separator + convertSpecialChars(value); _dataStream->writeLine(line, EOL::CRLF); } } } saved(); }
//----------------------------------------------------------------------- void ConfigFile::load(const DataStreamPtr& stream, const String& separators, bool trimWhitespace) { /* Clear current settings map */ clear(); String currentSection = StringUtil::BLANK; SettingsMultiMap* currentSettings = OGRE_NEW_T(SettingsMultiMap, MEMCATEGORY_GENERAL)(); mSettings[currentSection] = currentSettings; /* Process the file line for line */ String line, optName, optVal; while (!stream->eof()) { line = stream->getLine(); /* Ignore comments & blanks */ if (line.length() > 0 && line.at(0) != '#' && line.at(0) != '@') { if (line.at(0) == '[' && line.at(line.length()-1) == ']') { // Section currentSection = line.substr(1, line.length() - 2); SettingsBySection::const_iterator seci = mSettings.find(currentSection); if (seci == mSettings.end()) { currentSettings = OGRE_NEW_T(SettingsMultiMap, MEMCATEGORY_GENERAL)(); mSettings[currentSection] = currentSettings; } else { currentSettings = seci->second; } } else { /* Find the first seperator character and split the string there */ Ogre::String::size_type separator_pos = line.find_first_of(separators, 0); if (separator_pos != Ogre::String::npos) { optName = line.substr(0, separator_pos); /* Find the first non-seperator character following the name */ Ogre::String::size_type nonseparator_pos = line.find_first_not_of(separators, separator_pos); /* ... and extract the value */ /* Make sure we don't crash on an empty setting (it might be a valid value) */ optVal = (nonseparator_pos == Ogre::String::npos) ? "" : line.substr(nonseparator_pos); if (trimWhitespace) { StringUtil::trim(optVal); StringUtil::trim(optName); } currentSettings->insert(SettingsMultiMap::value_type(optName, optVal)); } } } } }
//----------------------------------------------------------------------- bool ConfigFileEx::hasSetting(const String& _key, const String& _section) const { SettingsBySection::const_iterator secIt = mSettings.find(_section); if (secIt == mSettings.end()) return false; SettingsMultiMap* sec = secIt->second; SettingsMultiMap::const_iterator it = sec->find(_key); return (it != sec->end()); }
void ConfigFile::SetString(Ogre::String key, Ogre::String value, Ogre::String section /* = Ogre::StringUtil::BLANK */) { SettingsMultiMap* set = mSettingsPtr[section]; if (!set) { // new section set = new SettingsMultiMap(); mSettingsPtr[section] = set; } if (set->count(key)) { // known key, delete old first set->erase(key); } // add key set->insert(std::multimap<Ogre::String, Ogre::String>::value_type(key, value)); }
//----------------------------------------------------------------------- bool ConfigFileEx::_clearMultiSetting(const String& _key, const String& _section) { bool wasChanged = false; SettingsBySection::iterator secIt = mSettings.find(_section); if (secIt != mSettings.end()) { SettingsMultiMap* sec = secIt->second; std::pair<SettingsMultiMap::iterator, SettingsMultiMap::iterator> range; range = sec->equal_range(_key); if(range.first != range.second) { sec->erase(range.first, range.second); wasChanged = true; } } return wasChanged; }
void ConfigFile::save(ofstream& stream, const String& separators, bool trimWhitespace) { SettingsBySection::const_iterator section; for(section = mSettings.begin(); section != mSettings.end(); section++) { if (section->first != StringUtil::BLANK) { stream << "[" << section->first << "]" << endl; } else { /// Keine Sektion } SettingsMultiMap::const_iterator setting; SettingsMultiMap *settings = section->second; for(setting = settings->begin(); setting != settings->end(); setting++) { stream << setting->first << "=" << setting->second << endl; } // Insert a linebreak stream << endl; } }
//----------------------------------------------------------------------- void ConfigFileEx::load(const DataStreamExPtr& _dataStream, const String& _separators, const String& _comments, ConfigFileExFlags _flags) { if( !_flags.check( ConfigFileExFlags::MERGE )) _clear(); if(_dataStream->getEncoding().empty()) _dataStream->setEncoding("UTF-8"); String currentSection = ""; SettingsMultiMap* currentSettings = OGRE_NEW_T(SettingsMultiMap, MEMCATEGORY_GENERAL)(); mSettings[currentSection] = currentSettings; /* Process the file line for line */ String line, optName, optVal; while (!_dataStream->eof()) { _dataStream->readLine(line, EOL::CRLF); /* Ignore comments & blanks */ if (line.length() > 0 && line.find_first_of(_comments) != 0) { if (line.at(0) == '[' && line.at(line.length()-1) == ']') { // Section currentSection = line.substr(1, line.length() - 2); SettingsBySection::const_iterator seci = mSettings.find(currentSection); if (seci == mSettings.end()) { currentSettings = OGRE_NEW_T(SettingsMultiMap, MEMCATEGORY_GENERAL)(); mSettings[currentSection] = currentSettings; } else { currentSettings = seci->second; } } else { /* Find the first seperator character and split the string there */ size_t separator_pos = line.find_first_of(_separators, 0); if (separator_pos != String::npos) { optName = line.substr(0, separator_pos); /* Find the first non-seperator character following the name */ String::size_type nonseparator_pos = line.find_first_not_of(_separators, separator_pos); /* ... and extract the value */ /* Make sure we don't crash on an empty setting (it might be a valid value) */ optVal = (nonseparator_pos == String::npos) ? "" : line.substr(nonseparator_pos); if(!_flags.check( ConfigFileExFlags::NO_TRIM_WHITESPACES)) { StringUtil::trim(optVal); StringUtil::trim(optName); } optVal = deconvertSpecialChars(optVal); SettingsMultiMap::iterator setit = currentSettings->end(); if( !_flags.check( ConfigFileExFlags::MULTI_SETTINGS )) { setit = currentSettings->find(optName); } if(setit == currentSettings->end()) { setit = currentSettings->insert(SettingsMultiMap::value_type(optName, String())); } setit->second = optVal; } } } } loaded(); }