canfigger v0.3.2 Lightweight config file parser library

Loading...
Searching...
No Matches
example-01.c
1#include <stdio.h>
2
3#include "tests/test.h"
4
5 int
6main(int argc, char *argv[])
7{
8 char *default_filename = SOURCE_DIR "/example-01.conf";
9 char *filename_ptr = default_filename;
10
11 if (argc == 2)
12 filename_ptr = argv[1];
13
14 if (argc > 2)
15 {
16 fputs("This example program only accepts a single argument:\n\n", stderr);
17 fprintf(stderr, "%s <config-file>\n\n", argv[0]);
18 return -1;
19 }
20
21 // Parse the config file. The second argument is the delimiter used to
22 // separate the value from any attributes on the same line.
23 struct Canfigger *list = canfigger_parse_file(filename_ptr, ',');
24 if (!list)
25 return -1;
26
27 int count = 0;
28
29 while (list)
30 {
31 // value may be NULL for keys with no '=' on the line.
32 printf("Key: %s, Value: %s\n", list->key,
33 list->value ? list->value : "NULL");
34
35 // Iterate attributes if present. Initialize attr to NULL before the
36 // first call; each call frees the previous string and loads the next.
37 // Skip this block entirely if the node has no attributes.
38 char *attr = NULL;
39 canfigger_free_current_attr_str_advance(list->attributes, &attr);
40 while (attr)
41 {
42 printf("Attribute: %s\n", attr);
43 canfigger_free_current_attr_str_advance(list->attributes, &attr);
44 }
45
46 // Free the current node and advance to the next.
47 canfigger_free_current_key_node_advance(&list);
48 putchar('\n');
49
50 count++;
51 }
52
53 // Verify all entries in the example config were visited.
54 assert(count == 6);
55
56 return 0;
57}
canfigger_parse_file
struct Canfigger * canfigger_parse_file(const char *file, const int delimiter)
Parse a configuration file into a linked list of key-value nodes.
Definition canfigger.c:356
canfigger_free_current_attr_str_advance
void canfigger_free_current_attr_str_advance(struct attributes *attributes, char **attr)
Free the current attribute string and advance to the next attribute.
Definition canfigger.c:86
canfigger_free_current_key_node_advance
void canfigger_free_current_key_node_advance(struct Canfigger **node)
Free the current node and advance the list pointer to the next node.
Definition canfigger.c:125
Canfigger
A single node in the parsed configuration linked list.
Definition canfigger.h:127