cfg_parse


Simple config file parser in C

Table of Contents


Introduction

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.

Back to top


Quick Usage

Here is some usage info for cfg_parse. Download the files cfg_parse.h and cfg_parse.c, and compile them along with the rest of your program.

You may also wish to include it as an automatic svn:externals property, as in:

svn propset svn:externals "cfg_parse svn://svn.code.sf.net/p/cfg-parse/code/trunk" .

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>

const char* local_config_path = "/home/developer/.config";

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"))
  {
    fprintf(stderr, "Unable to load cfg.ini\n");
    cfg_free(cfg);
    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, local_config_path);

  /* 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;
}

Back to top


Full Documentation

Complete documentation has been generated by Doxygen and posted here.

View the complete manual here.

Back to top


License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Back to top


Downloads

The current version is v1.0r15, released on Apr 20, 2019.

Visit the Files area on SourceForge to view all the available files.

Source code is provided via svn on the Sourceforge project page. Please use anonymous SVN checkout / export to obtain the latest version. Click here to visit the Code page.

Back to top


Links

Back to top


Page design by Greg Kennedy.