I was in need of a small, light weight configuration utility for some projects I’ve been working on. Most of the utilities I found were really overkill for what I needed. I created MiniCFG as a quick and dirty solution that seems to work well.

MiniCFG is a small set of functions written in C for parsing configuration files. Configuration data is stored as name/value pairs in a config file, one per line. There are no real restrictions on the format for names or values except that the cannot contain hash ‘#’ characters which are used to indicate comments.

Example

# this is an example configuration file
# name/value pairs are delimited by a '=' character
name = value
# the '=' character can exist within a value
line = y = mx + b
# names can contain spaces
foo bar = what's in a name
# comments begin with a hash '#' character
another name = another value # comments can follow a value
# name/value pairs can override previous definitions
name = yet another value
   # comments and settings may be preceeded by white space
   something = else
# a hash cannot be contained within a name, value or quoted strings
quote = "this is not # what you'd expect"
# a name without a value is legal and will return null
with equal sign =
without

There are three functions used to access the configuration data:

struct config* open_config(char* filename);

This opens and parses the configuration file.

int close_config(struct config* cfg);

This function closes the configuration data and cleans up any used resources.

char* load_setting(struct config* cfg, char* name);

This function retrieves the value for a given name or key. If the name does not exist, NULL is returned. MiniCFG is free under the MIT license and can be downloaded here.]]>

Related Posts

Leave a Reply