While looking for a configuration file parser I came across GkeyFile that is part of glib. It was very easy to use and met almost all the requirements that I wanted out of it. Here is a simple howto for GkeyFile…
Key files in GLib are more like .ini files that are mostly available on Windows. The key files data is divided into groups, where the name appears between square bracket characters, and comments are started by the # character. Key files, parsed by GKeyFile can handle strings, localized strings(internationalization support), Boolean values, integers, doubles, and arrays of each of these data types.
A simple example of a KeyFile can be:
#First Group
[Student1]
FirstName= Joe
Age=20
Now in order to get values for FirstName and Age for the group “Student1″ you can use the following code in C…
#include <glib.h>
typedef struct
{
gchar *FirstName;
int Age;
} Student;
int main ()
{
Settings *conf;
GKeyFile *keyfile;
GKeyFileFlags flags;
GError *error = NULL;
gsize length;
keyfile = g_key_file_new ();
flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
if (!g_key_file_load_from_file (keyfile, “Students.conf”, flags, &error))
{
g_error (error->message);
return -1;
}
conf = g_slice_new (Student);
conf->FirstName = g_key_file_get_string (keyfile, “Student1″,
“FirstName”, NULL);
conf->Age = g_key_file_get_integer (keyfile, “Student1″,
“Age”, NULL);
return 0;
}
You can find more help for Gekyfile in Glib Reference Manual
Shoaib Mir
shoaibmir[@]gmail.com