Source Walkthrough
To gain access to the LibConf4C functions and structures you must include the line:
#include "conf4c.h"
In order to parse any configuration file you need to create at least three objects:
Conf4CFileAttrib : Contains attributes about the configuration file to be parsed such as the filename, what denotes a comment, and what defines a name / value delimiter
Conf4CParser : To perform the parsing of a configuration file
Conf4CFile : Contains the parsed elements of a configuration file
To parse a configuration file which uses '#' as the start of a comment, a ' ' character as the delimiter between a name and a value, and a configuration file called `apache.conf':
Conf4CFileAttrib *c4cfattrib = conf4c_fileattrib_new ("apache.conf", "#", " ", CONF4C_DELIM_LAZY);
Next, before parsing the configuration file memory must be allocated for storing the parsed configuration values. To create a Conf4CFile which allocates enough memory for (initially) 3 section types, and 25 instances in the base section (all name / value pairs outside of '<></>' section markers):
Conf4CFile *c4cf = conf4c_file_new (3, 25);
Finally, create a Conf4CParser that will parse the configuration file specified by c4cfattrib and populate the Conf4CFile c4cf. Then to actually perform the parsing execute conf4c_parser_parse (). By passing values of '3' and '5', the parse function will (when creating new section types and section instances) allocate room for 3 section instances per section-type, and 5 name / value pairs per section instance.
Conf4CParser *c4cp = conf4c_parser_new (c4cf, c4cfattrib);
conf4c_parser_parse (3, 5);
At this point the Conf4CFile has been populated by the conf4c_parser_parse function.
To easily access the contents of a parsed Conf4CFile you will also want to create a Conf4CReader.
Conf4CReader *c4cr = conf4c_reader_new (c4cf);
Now to iterate through every name / value pair contained within the Conf4CFile begin three while loops. The outermost loop will traverse all configured section-types. The second loop will traverse each section instance of each section-type. And the innermost loop will traverse the name / value pairs of each section instance.
/* when a Conf4CReader is initialized it is already pointing at the */
/* first name / value pair of the first section instance of the first section-type */
do {
do {
do {
} while (conf4c_reader_next_name_value (c4cr));
} while (conf4c_reader_next_stor (c4cr));
} while (conf4c_reader_next_section (c4cr));
Then at each level you can access values such as section type, section instance name, name-value name, and name-value value with the functions:
conf4c_reader_get_section_type ()
conf4c_reader_get_stor_name ()
conf4c_reader_get_value_name ()
conf4c_reader_get_value ()
Or gain direct access with:
conf4c_reader_get_named_value ()
For more information about the Conf4CReader and its functionality see the Conf4C Reference.
This document was generated on October, 29 2004 using texi2html 1.70.