A compile-in (C, C++) solution for reading key-value pairs from file(s), looking up settings, and writing a config file back to disk.

Similar projects worth following
There is no standard way in C to parse config files. Several libraries have sprung up to solve the problem, but perhaps that is overkill for what should be a simple operation.

Presenting: cfg_parse - a compile-in solution for reading key-value pairs from file(s), looking up settings, and writing a config file back to disk.

There is full documentation on the Sourceforge page. Project gave me a good opportunity to try out Doxygen, which I like a lot.

Typically, a user should create a pointer to a cfg_struct, initialize it with cfg_init(), and then perform actions on that object (lookup, add, delete) by passing the pointer to the functions here. At end of use, call cfg_delete to clean up the object.

Sample code:

/* driver test program for cfg_parse */

#include "cfg_parse.h"

#include <stdio.h>

int main()
  /* Pointer to a cfg_struct structure */
  struct cfg_struct *cfg;

  /* Initialize config struct */
  cfg = cfg_init();

  /* Specifying some defaults */

  /* "Required" file */
  if (cfg_load(cfg,"config.ini") < 0)
    fprintf(stderr,"Unable to load cfg.ini\n");
    return -1;

  /* Several "optional" files can be added as well
      Each subsequent call upserts values already in
      the cfg structure. */

  /* Retrieve the value for key INFINITY, and print */
  printf("INFINITY = %s\n",cfg_get(cfg,"INFINITY"));

  /* Retrieve the value for key "KEY", and print */
  printf("KEY = %s\n",cfg_get(cfg,"KEY"));

  /* Delete the key-value pair for "DELETE_ME" */

  /* Dump cfg-struct to disk. */

  /* All done, clean up. */

  return 0;


  • Updated to 1.0r6

    Greg Kennedy04/21/2015 at 17:02 0 comments

    The only difference between this and r5 is the addition of extern "C" in the header file. This enables usage within C++ projects.

  • Updated to 1.0r5

    Greg Kennedy08/28/2014 at 23:21 0 comments

    Just a little source cleanup for the latest release.  This is very stable and I use it in lots of my hobby projects now.
    You can either download the zip file with code, or add it as a svn:externals property to automatically stay up-to-date.  A sample line would be:

    svn propset svn:externals "cfg_parse svn://" .

View all 2 project logs

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates