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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
Copyright 2015 IoT.bzh
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <string.h>
#include <syslog.h>
#include <assert.h>
#include <errno.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/uri.h>
#include "wgt.h"
static xmlDocPtr configxml = NULL;
static xmlNodePtr next(xmlNodePtr node, const char *type)
{
while (node && node->type != XML_ELEMENT_NODE && strcmp(type, node->name))
node = node->next;
return node;
}
static xmlNodePtr first(const char *type)
{
xmlNodePtr root;
if (configxml) {
root = xmlDocGetRootElement(configxml);
if (root)
return next(root->children, type);
}
return NULL;
}
static xmlNodePtr element_based_localisation(const char *type)
{
xmlNodePtr resu;
char *lang;
resu = first(type);
while (resu) {
lang = xmlNodeGetLang(resu);
if (lang) {
xmlFree(lang);
}
resu = next(resu->next, type);
}
return resu;
}
void confixml_close()
{
if (configxml) {
xmlFreeDoc(configxml);
configxml = NULL;
}
}
int confixml_open()
{
assert(!configxml);
configxml = xmlReadFile(_config_xml_, NULL, 0);
if (configxml == NULL) {
syslog(LOG_ERR, "xml parse of config file %s failed", _config_xml_);
return -1;
}
return 0;
}
/* elements based on localisation */
xmlNodePtr confixml_name()
{
return element_based_localisation(_name_);
}
xmlNodePtr confixml_description()
{
return element_based_localisation(_description_);
}
xmlNodePtr confixml_license()
{
return element_based_localisation(_license_);
}
/* elements based on path localisation */
xmlNodePtr confixml_author()
{
return first(_author_);
}
xmlNodePtr confixml_content()
{
return first(_content_);
}
/* element multiple */
xmlNodePtr confixml_first_feature()
{
return first(_feature_);
}
xmlNodePtr confixml_next_feature(xmlNodePtr node)
{
return next(node->next, _feature_);
}
xmlNodePtr confixml_first_preference()
{
return first(_preference_);
}
xmlNodePtr confixml_next_preference(xmlNodePtr node)
{
return next(node->next, _preference_);
}
xmlNodePtr confixml_first_icon()
{
return first(_icon_);
}
xmlNodePtr confixml_next_icon(xmlNodePtr node)
{
return next(node->next, _icon_);
}
/* best sized icon */
static int score_dim(xmlNodePtr ref, xmlNodePtr x, const char *dim, int request)
{
int r, iref, ix;
char *sref, *sx;
sref = xmlGetProp(ref, dim);
if (sref) {
iref = atoi(sref);
xmlFree(sref);
sx = xmlGetProp(x, dim);
if (sx) {
/* sref && sx */
ix = atoi(sx);
xmlFree(sx);
if (ix >= request) {
if (iref >= request)
r = ix - iref;
else
r = request - ix;
} else {
if (iref >= request)
r = iref - request;
else
r = iref - ix;
}
} else {
/* sref && !sx */
if (iref >= request)
r = iref - request;
else
r = 0;
}
} else {
sx = xmlGetProp(x, dim);
if (sx) {
/* !sref && sx */
ix = atoi(sx);
xmlFree(sx);
if (ix >= request)
r = request - ix;
else
r = 0;
} else {
/* !sref && !sx */
r = 0;
}
}
return r;
}
static int better_icon(xmlNodePtr ref, xmlNodePtr x, int width, int height)
{
int sw = score_dim(ref, x, _width_, width);
int sh = score_dim(ref, x, _height_, height);
return sw+sh < 0;
}
xmlNodePtr confixml_icon(int width, int height)
{
xmlNodePtr resu, icon;
resu = confixml_first_icon();
icon = confixml_next_icon(resu);
while (icon) {
if (better_icon(resu, icon, width, height))
resu = icon;
icon = confixml_next_icon(icon);
}
return resu;
}
|