Configuration File Support

Background

Configuration is an important part of applications and services. Services are often required to run in different environments where access to other co-operating services such as databases and other subservices may differ. This library helps manage these issues by providing a common C API for dealing with configuration files.

INI File Processing

INI File Format

INI files in this format can be parsed:

# Example INI File, comments are allowed, empty lines ignored

[General]
service=http:/mydomain/api:1234
timeout=300

[Database]
connect=host=localhost port=5432 dbname=mydb user=mydbuser password=secret

Note that sections are optional. An INI file without sections would be similar format to Java properties file.

Simple Load

To load a config file:

#include <c-craft/app.h>

APP_CONFIG cfg = cfg_load(mem, config_file, NULL);

Retrieve Configuration

To retrieve any configuration, get a value by its key. You may pass empty string or NULL for section to get variables not in a section:

#include <c-craft/app.h>

APP_CONFIG cfg = cfg_load(mem, config_file, NULL);
char *connect_str = cfg_get_item(cfg, "Database", "connect");

/* Get all the lines 'as is' for a section. */
char **items = cfg_get_section(cfg, "Database");

Path Searching

The last argument to cfg_load() allow to specify an array of search paths.

#include <c-craft/app.h>

char *paths[] = { "../etc", "/etc", NULL };

APP_CONFIG cfg = cfg_load(mem, config_file, paths);

Note that the array must be terminated with a NULL.

Errors

If any syntax errors are encountered, a NULL is returned, and the error is written to stderr.