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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
// Demonstrates almost all of libpom's API
#include <pom.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <assert.h>
static size_t custom_read(void *udata, char *buf, size_t size) {
int fd = (int)(intptr_t)udata;
// only read up to 4 bytes at a time. why not!
// it's much slower, but it is allowed.
ssize_t ret = read(fd, buf, size < 4 ? size : 4);
if (ret < 0) {
// read error occured.
// we could store an error away somewhere if we wanted to
// (read errors are unusual anyways.)
ret = 0;
}
return ret;
}
int main(void) {
pom_error *error;
// ordinary usage: load from a path
pom_conf *conf = pom_load_path(NULL, "conf.pom", &error);
if (error) {
pom_error_print(error);
free(error);
return EXIT_FAILURE;
}
// get a key
const char *indentation_type = pom_conf_get(conf, "indentation-type");
printf("indentation-type: %s\n", indentation_type ? indentation_type : "(none)");
// free configuration
pom_conf_free(conf);
pom_settings settings = {0};
strcpy(settings.error_lang, "fr"); // erreurs en français
// load configuration with custom settings and a custom reader
int fd = open("conf.pom", O_RDONLY);
conf = pom_load(&settings,
"conf.pom", // file name for error messages
custom_read, // read function
(void *)(intptr_t)fd, // will be passed as 1st argument to custom_read
&error
);
if (error) {
pom_error_print(error);
free(error);
return EXIT_FAILURE;
}
// nicer way of doing what we did above
printf("indentation-type: %s\n", pom_conf_get_or_default(conf, "indentation-type", "(none)"));
// parse value as signed integer
int64_t int_val;
if ((error = pom_conf_get_int(conf, "tab-size", &int_val))) {
pom_error_print(error);
free(error);
} else {
printf("tab size: %" PRId64 "\n", int_val);
}
// parse value as unsigned integer, use default of 2
uint64_t uint_val;
if ((error = pom_conf_get_uint_or_default(conf, "padding-pixels", &uint_val, 2))) {
pom_error_print(error);
free(error);
} else {
printf("padding pixels: %" PRIu64 "\n", uint_val);
}
// parse value as double
double float_val;
if ((error = pom_conf_get_float_or_default(conf, "font-size", &float_val, 12.0))) {
pom_error_print(error);
free(error);
} else {
printf("font size: %f\n", float_val);
}
// parse value as boolean
bool bool_val;
if ((error = pom_conf_get_bool_or_default(conf, "show-line-numbers", &bool_val, true))) {
pom_error_print(error);
free(error);
} else {
printf("show line numbers: %d\n", bool_val);
}
// extract section out of configuration
const pom_conf *file_extensions = pom_conf_section(conf, "file-extensions");
// parse value as list
char **Cpp_extensions = pom_conf_get_list(file_extensions, "Cpp");
if (Cpp_extensions) {
for (size_t i = 0; Cpp_extensions[i]; i++)
printf("- Defined C++ extension %s\n", Cpp_extensions[i]);
} else {
printf("no extensions defined for C++\n");
}
free(Cpp_extensions);
// iterate over keys in section
pom_key_iter *key_iter = NULL;
const char *key;
const pom_conf *plugins = pom_conf_section(conf, "plug-in");
while ((key = pom_conf_next_key(plugins, &key_iter))) {
const pom_conf *plugin = pom_conf_section(plugins, key);
const char *path = pom_conf_get_or_default(plugin, "path", "(none)");
bool enabled;
if ((error = pom_conf_get_bool_or_default(plugin, "enabled", &enabled, true))) {
pom_error_print(error);
free(error);
// (enabled will still be true in this case)
}
// get location where key was defined
const char *file;
uint64_t line;
pom_conf_location(plugins, key, &file, &line);
printf("%s:%" PRIu64 ": plug-in: %s (path = %s, enabled = %d)\n",
file, line, key, path, enabled);
}
// load config from string
pom_conf *overrides = pom_load_string(NULL, "<built-in overrides>",
"tab-size = 12", &error);
assert(!error);
// create a copy of a configuration
pom_conf *copy = pom_conf_copy(conf);
// merge configurations
pom_conf_merge(copy, overrides);
// iterate over items (key-value pairs) in configuration
const pom_item *item;
pom_item_iter *item_iter = NULL;
while ((item = pom_conf_next_item(copy, &item_iter))) {
if (strchr(item->key, 'b'))
printf("\t%s: %s\n", item->key, item->value);
}
// iterate over all the keys which haven't been accessed directly
pom_unread_key_iter *unread_iter = NULL;
const char *unread;
while ((unread = pom_conf_next_unread_key(copy, &unread_iter))) {
printf("unrecognized key %s\n", unread);
}
pom_conf_free(copy);
pom_conf_free(overrides);
pom_conf_free(conf);
return 0;
}
|