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