-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.c
178 lines (153 loc) · 4.1 KB
/
config.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
#include <unistd.h>
#endif
#ifdef _MSC_VER
#include <io.h>
#if _MSC_VER >= 1400
#undef access
#define access _access
#undef strdup
#define strdup _strdup
#endif
#define F_OK 00
#endif
/* libxml2 */
#include <libxml/globals.h>
#include <libxml/parser.h>
#include <libxml/xmlstring.h>
static xmlDocPtr global_config_doc = NULL;
static xmlNodePtr global_config_root = NULL;
int config_init(const char* config_xml, const char* root_node)
{
if( access(config_xml, F_OK) == -1 ) {
printf("configuration file '%s' does not exist\n", config_xml);
return 0;
}
printf("using configuration file '%s'\n", config_xml);
global_config_doc = xmlReadFile(config_xml, NULL, 0);
if( !global_config_doc ) {
printf("could not load configuration file\n");
return 0;
}
global_config_root = xmlDocGetRootElement(global_config_doc);
if( !global_config_root ) {
printf("invalid configuration file - no root element\n");
return 0;
}
if( xmlStrcmp(global_config_root->name, (const xmlChar*)root_node) != 0 ) {
printf("invalid configuration file - no '%s' element\n", root_node);
return 0;
}
return 1;
}
static int config_read_option(const xmlNode* config_node, const char* name, xmlChar** option)
{
int result = 0;
if( config_node ) {
xmlNodePtr config_child = config_node->children;
while( config_child != NULL) {
if( config_child->type == XML_ELEMENT_NODE ) {
if( xmlStrcmp(config_child->name, (const xmlChar*)"option") == 0 ) {
xmlChar* opt_name = xmlGetProp(config_child, (const xmlChar*)"name");
if( opt_name ) {
if( xmlStrcmp(opt_name, (const xmlChar*)name) == 0 ) {
result = 1;
*option = xmlGetProp(config_child, (const xmlChar*)"value");
xmlFree(opt_name);
break;
}
xmlFree(opt_name);
}
}
else {
printf("invalid node '%s' found\n", config_child->name);
}
}
config_child = config_child->next;
}
}
/*
if( result == 0 )
printf("unknown option '%s'\n", name);
*/
return result;
}
static void config_read_option_int(const xmlNode* config_node, const char* opt_name, int* value)
{
xmlChar* opt = NULL;
if( config_read_option(config_node, opt_name, &opt) ) {
if( opt && xmlStrlen(opt) > 0 ) {
*value = atoi((const char*)opt);
}
}
xmlFree(opt);
}
static void config_read_option_str_ptr(const xmlNode* config_node, const char* opt_name, char** value)
{
xmlChar* opt = NULL;
if( config_read_option(config_node, opt_name, &opt) ) {
if( *value )
free(*value);
*value = strdup((const char*)opt);
/* TODO: check result */
xmlFree(opt);
opt = NULL;
}
}
int config_read(struct config_entry config_entries[], const char* config_name)
{
int config_found = 0;
xmlNodePtr global_config_child = global_config_root->children;
while( global_config_child != NULL ) {
if( global_config_child->type == XML_ELEMENT_NODE ) {
if( xmlStrcmp(global_config_child->name, (const xmlChar*)config_name) == 0 ) {
config_found = 1;
break;
}
}
global_config_child = global_config_child->next;
}
if( !config_found ) {
printf("could not find configuration '%s'\n", config_name);
return 0;
}
int i = 0;
for( ; config_entries[i].name != NULL; ++i )
{
struct config_entry* ce = &config_entries[i];
/* printf("entry - %s - %d - ", ce->name, ce->type); */
if( ce->type == CFG_INT ) {
int* value = (int*)ce->value;
config_read_option_int(global_config_child, ce->name, value);
/* printf("%d\n", *value); */
}
else if( ce->type == CFG_STR_PTR ) {
char** value = (char**)ce->value;
config_read_option_str_ptr(global_config_child, ce->name, value);
/* printf("%s\n", *value ? *value : ""); */
}
else
printf("unknown config_entry type %d\n", ce->type);
}
return 1;
}
void config_free(struct config_entry config_entries[])
{
int i = 0;
for( ; config_entries[i].name != NULL; ++i )
{
struct config_entry* ce = &config_entries[i];
if( ce->type == CFG_STR_PTR ) {
char** value = (char**)ce->value;
free(*value);
}
}
if( global_config_doc ) {
xmlFreeDoc(global_config_doc);
global_config_doc = NULL;
}
}