INI File Parser ( C++ )

INI File Yesterday I was just browsing Stackoverflow, One question took my attention on INI File parsing. So I was just thinking why it is so difficult to parse ini file. I decided to write a parser for the same. On one hand India Vs England IIIrd test was causing panic and other side this parser activity was getting more and more interesting, It took me only 2 hrs to finish the parser.

This is very simple parser written in C++ using STL. Parser will validate the correctness of the configuration file and allows you to query for the keys using template functions. You just need to include the header and .cpp file for using this.
Typical Usage is as follows

    #include "IniParser.h"
    #include <assert.h>
    int main( int argc, char ** argv){
        IniParser myparser;
        int intValue;
        try {
            myparser.Load(argv[1]);
            myparser.QueryKeyValue<int>("Section4","KeyInt",intValue);
        } catch ( IniParseException &e) {
            std::cout << e.what();
            exit (1);
        }
        return 0;
    }
void IniParser::Load( const std::string &filename)

takes file name and parse the file, if found any error then it will throws

IniParseException

exception
Querying Key value
Following API will need to use for querying key.

    void QueryKeyValue(const std::string &section,
                            const std::string &key,
                            ValueType &defaultValue = ValueType())
                            const throw (IniParseException)

Usage of the API is as follows
If you know the type of the value is int then call this function as follows

myparser.QueryKeyValue<int>("Section4","KeyInt",intValue);

If this call does not throw any exception then intValue will have required value.
Will add writing/editing capabilities soon of course when India is again loosing next Test Match.

Advertisements

3 thoughts on “INI File Parser ( C++ )

  1. Its very nice and very easy to modify.. I wanted the parser to ignore duplicate sections as well as to ignore the case of the Key..and I was able to do it with few changes.. Thanks a lot. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s