Click here to Skip to main content
15,893,668 members
Articles / Operating Systems / Windows

INI file reader using the spirit library

Rate me:
Please Sign up or sign in to vote.
4.90/5 (31 votes)
3 Jan 2006CPOL5 min read 107K   1.6K   45  
A simple implementation of an INI file reader using the boost::spirit framework
#pragma warning (disable : 4786)
#include <string>
#include <vector>
using namespace std;
#include "ini_file.h"

void usage(char ** argv)
{
	printf("\nUsage : %s ini_file\n", argv[0]);
}

void PrintIniFile(CIniFile &ifile)
{
	int i;
	for (i=0;i<ifile.GetNumCategories();i++)
	{
		ifile.SetCategory(i);
		printf("Cat %d = [%s]\n", i, ifile.GetCategoryName().c_str());
		int j;
		for (j=0;j<ifile.GetNumEntries();j++)
		{
			ifile.SetEntry(j);
			printf("Entry %d [%s]=[%s]\n", j, ifile.GetEntryName().c_str(), ifile.GetEntryValue().c_str());
		}
		printf("\n");
	}
}

int main(int argc, char ** argv)
{
	if (argc != 2)
	{
		usage(argv);
	}
	else
	{
		CIniFile ifile;
		ifile.LoadFile(argv[1]);

		switch (ifile.m_status)
		{
		case 0:
			PrintIniFile(ifile);
			break;
		case 1:
			printf("Cannot open input file !\n");
			break;
		case 2:
			printf("Some error occured while parsing input file !\n");
			PrintIniFile(ifile);
			break;
		default:
			break;
		}
	}

	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 Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions