ConfigFile save

What it says on the tin: a place to discuss proposed new features.
Post Reply
User avatar
pianoman
Halfling
Posts: 47
Joined: Sun Feb 08, 2009 10:52 pm

ConfigFile save

Post 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.
User avatar
wizzler
Kobold
Posts: 34
Joined: Tue Nov 10, 2009 1:22 pm

Re: ConfigFile save

Post 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.
Image Image
User avatar
pianoman
Halfling
Posts: 47
Joined: Sun Feb 08, 2009 10:52 pm

Re: ConfigFile save

Post 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.
Post Reply