-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.c
88 lines (69 loc) · 2.67 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
81
82
83
84
85
86
87
88
/* Copyright (c) 2010, Torbjörn Lönnemark <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "ucfg.h"
int main(int argc, char **argv)
{
struct ucfg_node *cfg, *tmp;
int err;
/* create a root element and set its name */
ucfg_node_init(&cfg);
ucfg_node_name_set(cfg, "root");
/* create subsection */
ucfg_node_sub_create(cfg, &tmp);
/* set name and value for first element in subsection */
ucfg_node_name_set(tmp, "child");
ucfg_node_value_set(tmp, "child value");
/* append another element to the subsection */
ucfg_node_next_append(tmp, &tmp);
/* set name and value for that element */
ucfg_node_name_set(tmp, "another child");
ucfg_node_value_set(tmp, "another child's value");
/* append yet another element and name it */
ucfg_node_next_append(tmp, &tmp);
ucfg_node_name_set(tmp, "child node with list");
/* and make it a subsection */
ucfg_node_sub_create(tmp, &tmp);
/* add some values without names (i.e. a list) to the subsection */
ucfg_node_value_set(tmp, "list item1 has some double-quotes:"
"\" and \" such\" ");
ucfg_node_next_append(tmp, &tmp);
ucfg_node_value_set(tmp, "list item2");
/* serialize config onto stdout */
ucfg_write(cfg, stdout);
/* serialize config into file 'example.conf' */
if ((err = ucfg_write_file(cfg, "example.conf")) != UCFG_OK) {
printf("%s\n", ucfg_strerror(err));
}
/* destroy config structure and free its memory */
ucfg_node_destroy(cfg);
/* load previously written config file */
if ((err = ucfg_read_file(&cfg, "example.conf")) == UCFG_OK) {
/* lookup a value in the config and print it */
if (ucfg_lookup(&tmp, cfg,
"root:child node with list:") == UCFG_OK)
printf("%s\n", tmp->value);
printf("root:child is set to '%s'\n",
ucfg_lookup_string(cfg, "root:child"));
if (ucfg_lookup_string(cfg, "abc:def") == NULL)
printf("abc:def is not set\n" );
ucfg_node_destroy(cfg);
} else {
printf("%s\n", ucfg_strerror(err));
}
return 0;
}