/src/libxslt/libexslt/sets.c
Line | Count | Source (jump to first uncovered line) |
1 | | #define IN_LIBEXSLT |
2 | | #include "libexslt/libexslt.h" |
3 | | |
4 | | #include <libxml/tree.h> |
5 | | #include <libxml/xpath.h> |
6 | | #include <libxml/xpathInternals.h> |
7 | | |
8 | | #include <libxslt/xsltutils.h> |
9 | | #include <libxslt/xsltInternals.h> |
10 | | #include <libxslt/extensions.h> |
11 | | |
12 | | #include "exslt.h" |
13 | | |
14 | | /** |
15 | | * exsltSetsDifferenceFunction: |
16 | | * @ctxt: an XPath parser context |
17 | | * @nargs: the number of arguments |
18 | | * |
19 | | * Wraps #xmlXPathDifference for use by the XPath processor |
20 | | */ |
21 | | static void |
22 | 35.2k | exsltSetsDifferenceFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
23 | 35.2k | xmlNodeSetPtr arg1, arg2, ret; |
24 | | |
25 | 35.2k | if (nargs != 2) { |
26 | 312 | xmlXPathSetArityError(ctxt); |
27 | 312 | return; |
28 | 312 | } |
29 | | |
30 | 34.8k | arg2 = xmlXPathPopNodeSet(ctxt); |
31 | 34.8k | if (xmlXPathCheckError(ctxt)) |
32 | 240 | return; |
33 | | |
34 | 34.6k | arg1 = xmlXPathPopNodeSet(ctxt); |
35 | 34.6k | if (xmlXPathCheckError(ctxt)) { |
36 | 91 | xmlXPathFreeNodeSet(arg2); |
37 | 91 | return; |
38 | 91 | } |
39 | | |
40 | 34.5k | ret = xmlXPathDifference(arg1, arg2); |
41 | | |
42 | 34.5k | if (ret != arg1) |
43 | 20.7k | xmlXPathFreeNodeSet(arg1); |
44 | 34.5k | xmlXPathFreeNodeSet(arg2); |
45 | | |
46 | 34.5k | xmlXPathReturnNodeSet(ctxt, ret); |
47 | 34.5k | } |
48 | | |
49 | | /** |
50 | | * exsltSetsIntersectionFunction: |
51 | | * @ctxt: an XPath parser context |
52 | | * @nargs: the number of arguments |
53 | | * |
54 | | * Wraps #xmlXPathIntersection for use by the XPath processor |
55 | | */ |
56 | | static void |
57 | 75.7k | exsltSetsIntersectionFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
58 | 75.7k | xmlNodeSetPtr arg1, arg2, ret; |
59 | | |
60 | 75.7k | if (nargs != 2) { |
61 | 310 | xmlXPathSetArityError(ctxt); |
62 | 310 | return; |
63 | 310 | } |
64 | | |
65 | 75.3k | arg2 = xmlXPathPopNodeSet(ctxt); |
66 | 75.3k | if (xmlXPathCheckError(ctxt)) |
67 | 19.2k | return; |
68 | | |
69 | 56.1k | arg1 = xmlXPathPopNodeSet(ctxt); |
70 | 56.1k | if (xmlXPathCheckError(ctxt)) { |
71 | 418 | xmlXPathFreeNodeSet(arg2); |
72 | 418 | return; |
73 | 418 | } |
74 | | |
75 | 55.7k | ret = xmlXPathIntersection(arg1, arg2); |
76 | | |
77 | 55.7k | xmlXPathFreeNodeSet(arg1); |
78 | 55.7k | xmlXPathFreeNodeSet(arg2); |
79 | | |
80 | 55.7k | xmlXPathReturnNodeSet(ctxt, ret); |
81 | 55.7k | } |
82 | | |
83 | | /** |
84 | | * exsltSetsDistinctFunction: |
85 | | * @ctxt: an XPath parser context |
86 | | * @nargs: the number of arguments |
87 | | * |
88 | | * Wraps #xmlXPathDistinct for use by the XPath processor |
89 | | */ |
90 | | static void |
91 | 160k | exsltSetsDistinctFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
92 | 160k | xmlXPathObjectPtr obj; |
93 | 160k | xmlNodeSetPtr ns, ret; |
94 | 160k | int boolval = 0; |
95 | 160k | void *user = NULL; |
96 | | |
97 | 160k | if (nargs != 1) { |
98 | 317 | xmlXPathSetArityError(ctxt); |
99 | 317 | return; |
100 | 317 | } |
101 | | |
102 | 160k | if (ctxt->value != NULL) { |
103 | 160k | boolval = ctxt->value->boolval; |
104 | 160k | user = ctxt->value->user; |
105 | 160k | ctxt->value->boolval = 0; |
106 | 160k | ctxt->value->user = NULL; |
107 | 160k | } |
108 | 160k | ns = xmlXPathPopNodeSet(ctxt); |
109 | 160k | if (xmlXPathCheckError(ctxt)) |
110 | 385 | return; |
111 | | |
112 | | /* !!! must be sorted !!! */ |
113 | 160k | ret = xmlXPathDistinctSorted(ns); |
114 | | |
115 | 160k | if (ret != ns) |
116 | 49.2k | xmlXPathFreeNodeSet(ns); |
117 | | |
118 | 160k | obj = xmlXPathWrapNodeSet(ret); |
119 | 160k | obj->user = user; |
120 | 160k | obj->boolval = boolval; |
121 | 160k | valuePush((ctxt), obj); |
122 | 160k | } |
123 | | |
124 | | /** |
125 | | * exsltSetsHasSameNodesFunction: |
126 | | * @ctxt: an XPath parser context |
127 | | * @nargs: the number of arguments |
128 | | * |
129 | | * Wraps #xmlXPathHasSameNodes for use by the XPath processor |
130 | | */ |
131 | | static void |
132 | | exsltSetsHasSameNodesFunction (xmlXPathParserContextPtr ctxt, |
133 | 142k | int nargs) { |
134 | 142k | xmlNodeSetPtr arg1, arg2; |
135 | 142k | int ret; |
136 | | |
137 | 142k | if (nargs != 2) { |
138 | 296 | xmlXPathSetArityError(ctxt); |
139 | 296 | return; |
140 | 296 | } |
141 | | |
142 | 142k | arg2 = xmlXPathPopNodeSet(ctxt); |
143 | 142k | if (xmlXPathCheckError(ctxt)) |
144 | 7.69k | return; |
145 | | |
146 | 134k | arg1 = xmlXPathPopNodeSet(ctxt); |
147 | 134k | if (xmlXPathCheckError(ctxt)) { |
148 | 305 | xmlXPathFreeNodeSet(arg2); |
149 | 305 | return; |
150 | 305 | } |
151 | | |
152 | 134k | ret = xmlXPathHasSameNodes(arg1, arg2); |
153 | | |
154 | 134k | xmlXPathFreeNodeSet(arg1); |
155 | 134k | xmlXPathFreeNodeSet(arg2); |
156 | | |
157 | 134k | xmlXPathReturnBoolean(ctxt, ret); |
158 | 134k | } |
159 | | |
160 | | /** |
161 | | * exsltSetsLeadingFunction: |
162 | | * @ctxt: an XPath parser context |
163 | | * @nargs: the number of arguments |
164 | | * |
165 | | * Wraps #xmlXPathLeading for use by the XPath processor |
166 | | */ |
167 | | static void |
168 | 136k | exsltSetsLeadingFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
169 | 136k | xmlNodeSetPtr arg1, arg2, ret; |
170 | | |
171 | 136k | if (nargs != 2) { |
172 | 1.70k | xmlXPathSetArityError(ctxt); |
173 | 1.70k | return; |
174 | 1.70k | } |
175 | | |
176 | 134k | arg2 = xmlXPathPopNodeSet(ctxt); |
177 | 134k | if (xmlXPathCheckError(ctxt)) |
178 | 870 | return; |
179 | | |
180 | 134k | arg1 = xmlXPathPopNodeSet(ctxt); |
181 | 134k | if (xmlXPathCheckError(ctxt)) { |
182 | 154 | xmlXPathFreeNodeSet(arg2); |
183 | 154 | return; |
184 | 154 | } |
185 | | |
186 | | /* If the second node set is empty, then the first node set is |
187 | | * returned. |
188 | | */ |
189 | 133k | if (xmlXPathNodeSetIsEmpty(arg2)) { |
190 | 53.7k | xmlXPathReturnNodeSet(ctxt, arg1); |
191 | | |
192 | 53.7k | xmlXPathFreeNodeSet(arg2); |
193 | | |
194 | 53.7k | return; |
195 | 53.7k | } |
196 | | /* !!! must be sorted */ |
197 | 80.2k | ret = xmlXPathNodeLeadingSorted(arg1, xmlXPathNodeSetItem(arg2, 0)); |
198 | | |
199 | 80.2k | xmlXPathFreeNodeSet(arg1); |
200 | 80.2k | xmlXPathFreeNodeSet(arg2); |
201 | | |
202 | 80.2k | xmlXPathReturnNodeSet(ctxt, ret); |
203 | 80.2k | } |
204 | | |
205 | | /** |
206 | | * exsltSetsTrailingFunction: |
207 | | * @ctxt: an XPath parser context |
208 | | * @nargs: the number of arguments |
209 | | * |
210 | | * Wraps #xmlXPathTrailing for use by the XPath processor |
211 | | */ |
212 | | static void |
213 | 304k | exsltSetsTrailingFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
214 | 304k | xmlNodeSetPtr arg1, arg2, ret; |
215 | | |
216 | 304k | if (nargs != 2) { |
217 | 10.2k | xmlXPathSetArityError(ctxt); |
218 | 10.2k | return; |
219 | 10.2k | } |
220 | | |
221 | 294k | arg2 = xmlXPathPopNodeSet(ctxt); |
222 | 294k | if (xmlXPathCheckError(ctxt)) |
223 | 5.95k | return; |
224 | | |
225 | 288k | arg1 = xmlXPathPopNodeSet(ctxt); |
226 | 288k | if (xmlXPathCheckError(ctxt)) { |
227 | 1.19k | xmlXPathFreeNodeSet(arg2); |
228 | 1.19k | return; |
229 | 1.19k | } |
230 | | |
231 | | /* If the second node set is empty, then the first node set is |
232 | | * returned. |
233 | | */ |
234 | 287k | if (xmlXPathNodeSetIsEmpty(arg2)) { |
235 | 199k | xmlXPathReturnNodeSet(ctxt, arg1); |
236 | | |
237 | 199k | xmlXPathFreeNodeSet(arg2); |
238 | | |
239 | 199k | return; |
240 | 199k | } |
241 | | /* !!! mist be sorted */ |
242 | 87.1k | ret = xmlXPathNodeTrailingSorted(arg1, xmlXPathNodeSetItem(arg2, 0)); |
243 | | |
244 | 87.1k | xmlXPathFreeNodeSet(arg1); |
245 | 87.1k | xmlXPathFreeNodeSet(arg2); |
246 | | |
247 | 87.1k | xmlXPathReturnNodeSet(ctxt, ret); |
248 | 87.1k | } |
249 | | |
250 | | /** |
251 | | * exsltSetsRegister: |
252 | | * |
253 | | * Registers the EXSLT - Sets module |
254 | | */ |
255 | | |
256 | | void |
257 | 3.72k | exsltSetsRegister (void) { |
258 | 3.72k | xsltRegisterExtModuleFunction ((const xmlChar *) "difference", |
259 | 3.72k | EXSLT_SETS_NAMESPACE, |
260 | 3.72k | exsltSetsDifferenceFunction); |
261 | 3.72k | xsltRegisterExtModuleFunction ((const xmlChar *) "intersection", |
262 | 3.72k | EXSLT_SETS_NAMESPACE, |
263 | 3.72k | exsltSetsIntersectionFunction); |
264 | 3.72k | xsltRegisterExtModuleFunction ((const xmlChar *) "distinct", |
265 | 3.72k | EXSLT_SETS_NAMESPACE, |
266 | 3.72k | exsltSetsDistinctFunction); |
267 | 3.72k | xsltRegisterExtModuleFunction ((const xmlChar *) "has-same-node", |
268 | 3.72k | EXSLT_SETS_NAMESPACE, |
269 | 3.72k | exsltSetsHasSameNodesFunction); |
270 | 3.72k | xsltRegisterExtModuleFunction ((const xmlChar *) "leading", |
271 | 3.72k | EXSLT_SETS_NAMESPACE, |
272 | 3.72k | exsltSetsLeadingFunction); |
273 | 3.72k | xsltRegisterExtModuleFunction ((const xmlChar *) "trailing", |
274 | 3.72k | EXSLT_SETS_NAMESPACE, |
275 | 3.72k | exsltSetsTrailingFunction); |
276 | 3.72k | } |
277 | | |
278 | | /** |
279 | | * exsltSetsXpathCtxtRegister: |
280 | | * |
281 | | * Registers the EXSLT - Sets module for use outside XSLT |
282 | | */ |
283 | | int |
284 | | exsltSetsXpathCtxtRegister (xmlXPathContextPtr ctxt, const xmlChar *prefix) |
285 | 0 | { |
286 | 0 | if (ctxt |
287 | 0 | && prefix |
288 | 0 | && !xmlXPathRegisterNs(ctxt, |
289 | 0 | prefix, |
290 | 0 | (const xmlChar *) EXSLT_SETS_NAMESPACE) |
291 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
292 | 0 | (const xmlChar *) "difference", |
293 | 0 | (const xmlChar *) EXSLT_SETS_NAMESPACE, |
294 | 0 | exsltSetsDifferenceFunction) |
295 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
296 | 0 | (const xmlChar *) "intersection", |
297 | 0 | (const xmlChar *) EXSLT_SETS_NAMESPACE, |
298 | 0 | exsltSetsIntersectionFunction) |
299 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
300 | 0 | (const xmlChar *) "distinct", |
301 | 0 | (const xmlChar *) EXSLT_SETS_NAMESPACE, |
302 | 0 | exsltSetsDistinctFunction) |
303 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
304 | 0 | (const xmlChar *) "has-same-node", |
305 | 0 | (const xmlChar *) EXSLT_SETS_NAMESPACE, |
306 | 0 | exsltSetsHasSameNodesFunction) |
307 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
308 | 0 | (const xmlChar *) "leading", |
309 | 0 | (const xmlChar *) EXSLT_SETS_NAMESPACE, |
310 | 0 | exsltSetsLeadingFunction) |
311 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
312 | 0 | (const xmlChar *) "trailing", |
313 | 0 | (const xmlChar *) EXSLT_SETS_NAMESPACE, |
314 | 0 | exsltSetsTrailingFunction)) { |
315 | 0 | return 0; |
316 | 0 | } |
317 | 0 | return -1; |
318 | 0 | } |