-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.c
80 lines (66 loc) · 2.11 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#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
canfigger_free_current_key_node_advance(&config);
putchar('\n');
i++;
}
// This should be the number of keys in the example config
if (i != 6)
return -1;
return 0;
}