canfigger v0.3.x
Lightweight config file parser library
Loading...
Searching...
No Matches
example.c
1#include <stdio.h>
2
3#include "canfigger.h"
4
5int
6main(int argc, char *argv[])
7{
8 char *default_filename = SOURCE_DIR "/examplerc";
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 //
22 // Get a linked list containing the parsed config file. Each node contains
23 // a key (or a "setting", or an "option"), a value and attributes (if they
24 // are provided in your program's configuration file.
25 //
26 // The second argument is based on what the config file uses to separate
27 // the attributes.
28 //
29 struct Canfigger *config = canfigger_parse_file(filename_ptr, ',');
30
31 if (!config)
32 return -1;
33
34 // i is only used for testing
35 int i = 0;
36
37 while (config != NULL)
38 {
39 //
40 // The value member of the node must be checked for NULL
41 // before using it.
42 //
43 printf("Key: %s, Value: %s\n", config->key,
44 config->value != NULL ? config->value : "NULL");
45
46 //
47 // Process attributes if necessary. If you know there are no attributes
48 // for the current node, you can skip this, and there is no reason in
49 // this case to call canfigger_free_current_attr_str_advance().
50 //
51 // attr must be declared and initialized before using it as an
52 // argument to canfigger_free_current_attr_str_advance().
53 char *attr = NULL;
54 //
55 // Pass '&addr' to this function and it will get assigned an
56 // attribute, or NULL if there are none.
57 canfigger_free_current_attr_str_advance(config->attributes, &attr);
58 while (attr)
59 {
60 printf("Attribute: %s\n", attr);
61
62 //
63 // Get the next attribute in the list (if there is one).
64 //
65 canfigger_free_current_attr_str_advance(config->attributes, &attr);
66 }
67
68 // Move to the next node and automatically free the current node
70 putchar('\n');
71
72 i++;
73 }
74
75 // This should be the number of keys in the example config
76 if (i != 6)
77 return -1;
78
79 return 0;
80}
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