Close
0%
0%

cfg_parse

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.

The current home of cfg_parse is the Sourceforge project page here:

https://cfg-parse.sourceforge.io/

There is full documentation at that site - this 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_free 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 */
  cfg_set(cfg, "KEY", "VALUE");
  cfg_set(cfg, "KEY_A", "DEFAULT_VALUE_A");

  /* "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. */
  cfg_load(cfg, "/usr/local/etc/config.ini");
  cfg_load(cfg, "~/.config");

  /* 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" */
  cfg_delete(cfg, "DELETE_ME");

  /* Dump cfg-struct to disk. */
  cfg_save(cfg, "config_new.ini");

  /* All done, clean up. */
  cfg_free(cfg);

  return 0;
}

To actually use the software, you can either:

  • Download the zip file with code (available at the Sourceforge files section), 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://svn.code.sf.net/p/cfg-parse/code/trunk" .

  • Updated to 1.0r13

    Greg Kennedy04/03/2019 at 03:34 0 comments

    I needed to revisit this over the past couple days and so I've made a number of changes and improvements to the code.  The interface remains mostly the same, with the addition of some new functions:

    • cfg_set_array() - set multiple key/value pairs in one call
    • cfg_delete_array() - delete multiple keys in one call
    • cfg_prune() - "trim" the cfg_struct to only the whitelisted keys the user wants
    • cfg_get_keys() - return a list of all keys currently stored in the struct

    Other minor improvements include better handling of NULL values on input, and the file is no longer loaded in reverse order (so the load / save should not change the file any more).

    I've updated the downloads, webpage, and Doxygen manual with the changes.  Remember, the current home of cfg_parse is still the Sourceforge page here:

    https://cfg-parse.sourceforge.io/

    If anyone wants to do a code review I'd love another set of eyes.

  • 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://svn.code.sf.net/p/cfg-parse/code/trunk" .

View all 3 project logs

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

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