Friday, 15 November 2013

My code - SimpleCfg library

SimpleCfg is a lightweight, single-header-file C++ library I created two years ago to suffice all my needs in the loading of small configuration files.

Allegro, the C-language game programming library I usually use, has it's own configuration file routines, but they are very outdated, unable to have more than one file loaded at once, and obviously unavailable when I am not using Allegro. I originally created SimpleCfg for some OpenGL and Winsock programs.

SimpleCfg's usage is very simple (oh the redundancy!). It was greatly and clearly inspired by TinyXML's syntax. In fact, I used to call it TinyCfg until I discovered that someone was already using this name. Little do they know, that my library is even tinier and simpler! The API consists of only two classes: SimpleCfgFile and SimpleCfgValue.

Since one example is worth more than a thousand words, here's a valid SimpleCfg configuration file:

// test configuration file for SimpleCfg C++ library
int1 =2
int2 =22
int3 = 333 // comment


float2 =1.5
float3 = 2.0 // comment 
string1="First string"

string2 ="Second string"
string3 = "Second string"// comment

Very simple indeed. And the code for loading and parsing this file is even simpler!

#include "SimpleCfg.h"

#include <iostream>

using namespace std;
using namespace SimpleCfg

int main()
     SimpleCfgFile file("test.cfg");
     if(!file.IsLoaded()) return 1;
     if(file.GetValue("testValue") == undefinedValue) return 1;
     cout << "int1 = " << file.GetValue("int1").ToInt() << "\n";
     cout << "int2 = " << file.GetValue("int2").ToInt() << "\n";
     cout << "int3 = " << file.GetValue("int3").ToInt() << "\n";
     cout << "float1 = " << file.GetValue("float1").ToFloat() << "\n";
     cout << "float2 = " << file.GetValue("float2").ToFloat() << "\n";
     cout << "float3 = " << file.GetValue("float3").ToFloat() << "\n";
     cout << "string1 = " << file.GetValue("string1").ToString().c_str() << "\n";
     cout << "string2 = " << file.GetValue("string2").ToString().c_str() << "\n";
     cout << "string3 = " << file.GetValue("string3").ToString().c_str() << "\n";
     while(true) continue;
     return 0;

As you can see, SimpleCfg is the king in simple C++ configuration file libraries!

And the best thing is: it's completely free and open-source.
Download SimpleCfg.h here.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

No comments:

Post a Comment