canfigger v0.3.x
Lightweight config file parser library
Loading...
Searching...
No Matches
example.c
#include <stdio.h>
#include "canfigger.h"
int
main(int argc, char *argv[])
{
char *default_filename = SOURCE_DIR "/examplerc";
char *filename_ptr = default_filename;
if (argc == 2)
filename_ptr = argv[1];
if (argc > 2)
{
fputs("This example program only accepts a single argument:\n\n", stderr);
fprintf(stderr, "%s <config-file>\n\n", argv[0]);
return -1;
}
//
// Get a linked list containing the parsed config file. Each node contains
// a key (or a "setting", or an "option"), a value and attributes (if they
// are provided in your program's configuration file.
//
// The second argument is based on what the config file uses to separate
// the attributes.
//
struct Canfigger *config = canfigger_parse_file(filename_ptr, ',');
if (!config)
return -1;
// i is only used for testing
int i = 0;
while (config != NULL)
{
//
// The value member of the node must be checked for NULL
// before using it.
//
printf("Key: %s, Value: %s\n", config->key,
config->value != NULL ? config->value : "NULL");
//
// Process attributes if necessary. If you know there are no attributes
// for the current node, you can skip this, and there is no reason in
// this case to call canfigger_free_current_attr_str_advance().
//
// attr must be declared and initialized before using it as an
// argument to canfigger_free_current_attr_str_advance().
char *attr = NULL;
//
// Pass '&addr' to this function and it will get assigned an
// attribute, or NULL if there are none.
canfigger_free_current_attr_str_advance(config->attributes, &attr);
while (attr)
{
printf("Attribute: %s\n", attr);
//
// Get the next attribute in the list (if there is one).
//
canfigger_free_current_attr_str_advance(config->attributes, &attr);
}
// Move to the next node and automatically free the current node
putchar('\n');
i++;
}
// This should be the number of keys in the example config
if (i != 6)
return -1;
return 0;
}
Header file for the Canfigger configuration parser.
void canfigger_free_current_key_node_advance(struct Canfigger **list)
Frees the current key node and advances to the next node in the list.
Definition canfigger.c:114
struct Canfigger * canfigger_parse_file(const char *file, const int delimiter)
Parses a configuration file and creates a linked list of key-value pairs.
Definition canfigger.c:330
void canfigger_free_current_attr_str_advance(struct attributes *attributes, char **attr)
Frees the current attribute string and advances to the next attribute.
Definition canfigger.c:75
Structure to represent a key-value pair with attributes in the configuration.
Definition canfigger.h:60