Page 1 of 1

ConfigFile save

Posted: Tue Apr 20, 2010 1:50 am
by pianoman
It'd be nice if ConfigFile had a save() method.

The ConfigDialog saves it's ConfigFile itself, but I had a situation where I needed to save a ConfigFile also, so I wrote a generic utility method for saving any ConfigFile. I'll share it here to make it easy to include, or for others to use before it's included :) :

Code: Select all

	/** Saves the given ConfigFile to a file with the given path/name.  */
	static void saveConfig(Ogre::ConfigFile& config, std::string filename) {
		std::fstream fout(filename.c_str(), std::ios::out);

		// Go through all sections & settings in the file
		Ogre::ConfigFile::SectionIterator seci = config.getSectionIterator();

		while (seci.hasMoreElements())
		{
			Ogre::String sectionName = seci.peekNextKey();
			if (sectionName.length() > 0)
				fout << '\n' << '[' << seci.peekNextKey() << ']' << '\n';

			Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
			Ogre::ConfigFile::SettingsMultiMap::iterator i;
			for (i = settings->begin(); i != settings->end(); ++i)
			{
				fout << i->first.c_str() << '=' << i->second.c_str() << '\n';
			}
		}
	}
One thing the above method doesn't to is preserve previous formatting or comments ("# comment"), but that probably wouldn't matter under most circumstances.

Re: ConfigFile save

Posted: Mon May 03, 2010 9:36 am
by wizzler
Been discussed earlier: http://www.ogre3d.org/forums/viewtopic.php?f=4&t=39976

What they did was to inherit configfile and improve it with save ability.

Re: ConfigFile save

Posted: Tue May 04, 2010 2:44 am
by pianoman
Oh, I didn't see that, thanks. Is that only in Ogre 7+? I'm still on a project using 6.x and couldn't find any saving capability.