Reference

cfg(3pub)

routines for reading and writing configuration files

NAME

cfg_read_file, cfg_write_file, cfg_error - routines for reading and writing configuration files

SYNOPSIS

#include <publib.h>
const char *cfg_error(void);
int cfg_read_file(struct cfg_variable *list, const char *filename);
int cfg_write_file(struct cfg_variable *list, const char *filename);

DESCRIPTION

These routines read and write configuration files. cfg_read_file reads a configuration file into memory, cfg_write_file writes the in-memory representation to a file. cfg_error returns an error string that describes an error.

The in-memory representation of a configuration file is stored in an array of configuration file descriptors, stored in the following structure type:

struct cfg_variable {
	const char *cfg_name;
	enum cfg_type cfg_type;
	void *cfg_value;
};

The last element in the array should have cfg_name be NULL.

cfg_name gives the name of a configuration variable, as it is in the configuration file. cfg_type gives its type; it must one of the constants CFG_LONG, CFG_ULONG, CFG_DOUBLE, CFG_LONG_EXPR, CFG_ULONG_EXPR, CFG_DOUBLE_EXPR, CFG_BOOLEAN, and CFG_STRING. cfg_value is a pointer to the value; for CFG_LONG, CFG_ULONG, CFG_DOUBLE, and their _EXPR versions, it should be a pointer to a variable of type long, unsigned long, or double, respectively. That variable is set to the value read from the configuration file. Similarly, for CFG_BOOLEAN cfg_value should point at a variable of type int. For CFG_STRING it should be a pointer to a `char *', which is set to the string in the configuration file. Space for the new string is allocated automatically; the initial value must be NULL.

RETURN VALUE

cfg_read_file and cfg_write_file return -1 for error, or 0 for OK. If they return an error, cfg_error returns a pointer to the error message string. The string may change the next time cfg_read_file or cfg_write_file is called.

SEE ALSO

publib(3)

AUTHOR

Lars Wirzenius ([email protected])

History