//-----------------------------------------------------------------------
	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();
	}
	//-----------------------------------------------------------------------
	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());
	}
Esempio n. 3
0
    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();
	}