Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A C++ Config File Parser

, 16 May 2008
An STL based C++ utility class to parse structured config files.
config-src.zip
config
config
config.vcproj.EMEA.kschweb.user
Debug
debug
config.ilk
#include <iostream>
using namespace std;

#include "config.h"

int main(int argc, char* argv[], char* envp[])
{
	cout << "config demo program" << endl;
	cout << "(c) 2008 Kai G. Schwebke" << endl;
	cout << "This code is licensed under LGPL and comes with no warranty;" << endl;
	cout << "see COPYING for details." << endl;
	cout << endl;

	// read config file with environment variable expansion support
	Config config("demo.config", envp);
	
	// basic usage of properties
	if (config.pBool("showHello")) {
		int cnt = config.pInt("helloCount");
		string msg = config.pString("helloMessage");
		for (int i = 0; i < cnt; ++i) {
			cout << msg << endl;
		}
		cout << endl;
	}

	// properties with expanded names (no difference in using)
	cout << "tempFolder    = '" << config.pString("tempFolder") << "'" << endl;
	cout << "tempSubFolder = '" << config.pString("tempSubFolder") << "'" << endl;
	cout << endl;

	// get properties for all subgroups starting with prefix
	map<string, Config*> messages = config.getGroups(); // all groups
	const string messagePrefix = "message"; // prefix for group name
	for (map<string, Config*>::iterator i = messages.begin(); i != messages.end(); ++i) {
		string groupName = i->first;
		Config* group = i->second;

		// test group name for prefix
		if (groupName.substr(0, messagePrefix.length()) == messagePrefix) {
			// display group contents
			cout << group->pString("name") << ":" << endl;
			cout << "   " << group->pString("text") << endl;
		}
	}

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

freejack

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 17 May 2008
Article Copyright 2008 by freejack
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid