blob: 7718d5f119fabb787d0cd026cd36991baaaf1838 [file] [log] [blame]
Daniel Veillard3ce52572002-02-03 15:08:05 +00001/*
2 * types.c: converter functions between the internal representation
3 * and the Python objects
4 *
5 * See Copyright for the status of this software.
6 *
7 * daniel@veillard.com
8 */
9#include "libxml_wrap.h"
10
11PyObject *
12libxml_intWrap(int val) {
13 PyObject *ret;
14
15#ifdef DEBUG
16 printf("libxml_intWrap: val = %d\n", val);
17#endif
18 ret = PyInt_FromLong((long) val);
19 return(ret);
20}
21
22PyObject *
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000023libxml_longWrap(long val) {
24 PyObject *ret;
25
26#ifdef DEBUG
27 printf("libxml_longWrap: val = %ld\n", val);
28#endif
29 ret = PyInt_FromLong(val);
30 return(ret);
31}
32
33PyObject *
Daniel Veillard3ce52572002-02-03 15:08:05 +000034libxml_doubleWrap(double val) {
35 PyObject *ret;
36
37#ifdef DEBUG
38 printf("libxml_doubleWrap: val = %f\n", val);
39#endif
40 ret = PyFloat_FromDouble((double) val);
41 return(ret);
42}
43
44PyObject *
45libxml_charPtrWrap(char *str) {
46 PyObject *ret;
47
48#ifdef DEBUG
49 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
50#endif
51 if (str == NULL) {
52 Py_INCREF(Py_None);
53 return(Py_None);
54 }
55 /* TODO: look at deallocation */
56 ret = PyString_FromString(str);
57 xmlFree(str);
58 return(ret);
59}
60
61PyObject *
Daniel Veillardc575b992002-02-08 13:28:40 +000062libxml_charPtrConstWrap(const char *str) {
63 PyObject *ret;
64
65#ifdef DEBUG
66 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
67#endif
68 if (str == NULL) {
69 Py_INCREF(Py_None);
70 return(Py_None);
71 }
72 /* TODO: look at deallocation */
73 ret = PyString_FromString(str);
74 return(ret);
75}
76
77PyObject *
Daniel Veillard3ce52572002-02-03 15:08:05 +000078libxml_xmlCharPtrWrap(xmlChar *str) {
79 PyObject *ret;
80
81#ifdef DEBUG
82 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
83#endif
84 if (str == NULL) {
85 Py_INCREF(Py_None);
86 return(Py_None);
87 }
88 /* TODO: look at deallocation */
89 ret = PyString_FromString(str);
90 xmlFree(str);
91 return(ret);
92}
93
94PyObject *
Daniel Veillardc575b992002-02-08 13:28:40 +000095libxml_xmlCharPtrConstWrap(const xmlChar *str) {
96 PyObject *ret;
97
98#ifdef DEBUG
99 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
100#endif
101 if (str == NULL) {
102 Py_INCREF(Py_None);
103 return(Py_None);
104 }
105 /* TODO: look at deallocation */
106 ret = PyString_FromString(str);
107 return(ret);
108}
109
110PyObject *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000111libxml_constcharPtrWrap(const char *str) {
112 PyObject *ret;
113
114#ifdef DEBUG
115 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
116#endif
117 if (str == NULL) {
118 Py_INCREF(Py_None);
119 return(Py_None);
120 }
121 /* TODO: look at deallocation */
122 ret = PyString_FromString(str);
123 return(ret);
124}
125
126PyObject *
127libxml_constxmlCharPtrWrap(const xmlChar *str) {
128 PyObject *ret;
129
130#ifdef DEBUG
131 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
132#endif
133 if (str == NULL) {
134 Py_INCREF(Py_None);
135 return(Py_None);
136 }
137 /* TODO: look at deallocation */
138 ret = PyString_FromString(str);
139 return(ret);
140}
141
142PyObject *
143libxml_xmlDocPtrWrap(xmlDocPtr doc) {
144 PyObject *ret;
145
146#ifdef DEBUG
147 printf("libxml_xmlDocPtrWrap: doc = %p\n", doc);
148#endif
149 if (doc == NULL) {
150 Py_INCREF(Py_None);
151 return(Py_None);
152 }
153 /* TODO: look at deallocation */
154 ret = PyCObject_FromVoidPtrAndDesc((void *) doc, "xmlDocPtr", NULL);
155 return(ret);
156}
157
158PyObject *
159libxml_xmlNodePtrWrap(xmlNodePtr node) {
160 PyObject *ret;
161
162#ifdef DEBUG
163 printf("libxml_xmlNodePtrWrap: node = %p\n", node);
164#endif
165 if (node == NULL) {
166 Py_INCREF(Py_None);
167 return(Py_None);
168 }
169 ret = PyCObject_FromVoidPtrAndDesc((void *) node, "xmlNodePtr", NULL);
170 return(ret);
171}
172
173PyObject *
174libxml_xmlNsPtrWrap(xmlNsPtr ns) {
175 PyObject *ret;
176
177#ifdef DEBUG
178 printf("libxml_xmlNsPtrWrap: node = %p\n", ns);
179#endif
180 if (ns == NULL) {
181 Py_INCREF(Py_None);
182 return(Py_None);
183 }
184 ret = PyCObject_FromVoidPtrAndDesc((void *) ns, "xmlNsPtr", NULL);
185 return(ret);
186}
187
188PyObject *
189libxml_xmlAttrPtrWrap(xmlAttrPtr attr) {
190 PyObject *ret;
191
192#ifdef DEBUG
193 printf("libxml_xmlAttrNodePtrWrap: attr = %p\n", attr);
194#endif
195 if (attr == NULL) {
196 Py_INCREF(Py_None);
197 return(Py_None);
198 }
199 ret = PyCObject_FromVoidPtrAndDesc((void *) attr, "xmlAttrPtr", NULL);
200 return(ret);
201}
202
203PyObject *
204libxml_xmlAttributePtrWrap(xmlAttributePtr attr) {
205 PyObject *ret;
206
207#ifdef DEBUG
208 printf("libxml_xmlAttributePtrWrap: attr = %p\n", attr);
209#endif
210 if (attr == NULL) {
211 Py_INCREF(Py_None);
212 return(Py_None);
213 }
214 ret = PyCObject_FromVoidPtrAndDesc((void *) attr, "xmlAttributePtr", NULL);
215 return(ret);
216}
217
218PyObject *
219libxml_xmlElementPtrWrap(xmlElementPtr elem) {
220 PyObject *ret;
221
222#ifdef DEBUG
223 printf("libxml_xmlElementNodePtrWrap: elem = %p\n", elem);
224#endif
225 if (elem == NULL) {
226 Py_INCREF(Py_None);
227 return(Py_None);
228 }
229 ret = PyCObject_FromVoidPtrAndDesc((void *) elem, "xmlElementPtr", NULL);
230 return(ret);
231}
232
233PyObject *
234libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt) {
235 PyObject *ret;
236
237#ifdef DEBUG
238 printf("libxml_xmlXPathContextPtrWrap: ctxt = %p\n", ctxt);
239#endif
240 if (ctxt == NULL) {
241 Py_INCREF(Py_None);
242 return(Py_None);
243 }
244 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt, "xmlXPathContextPtr",
245 NULL);
246 return(ret);
247}
248
249PyObject *
Daniel Veillard7db38712002-02-07 16:39:11 +0000250libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)
251{
252 PyObject *ret;
253
254#ifdef DEBUG
255 printf("libxml_xmlXPathParserContextPtrWrap: ctxt = %p\n", ctxt);
256#endif
257 if (ctxt == NULL) {
258 Py_INCREF(Py_None);
259 return (Py_None);
260 }
261 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt,
262 "xmlXPathParserContextPtr", NULL);
263 return (ret);
264}
265
266PyObject *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000267libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt) {
268 PyObject *ret;
269
270#ifdef DEBUG
271 printf("libxml_xmlParserCtxtPtrWrap: ctxt = %p\n", ctxt);
272#endif
273 if (ctxt == NULL) {
274 Py_INCREF(Py_None);
275 return(Py_None);
276 }
277 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt, "xmlParserCtxtPtr",
278 NULL);
279 return(ret);
280}
281
282PyObject *
283libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj) {
284 PyObject *ret;
285
286#ifdef DEBUG
287 printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj);
288#endif
289 if (obj == NULL) {
290 Py_INCREF(Py_None);
291 return(Py_None);
292 }
293 switch(obj->type) {
294 case XPATH_XSLT_TREE:
295 /* TODO !!!! Allocation problems */
296 case XPATH_NODESET:
297 if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0))
298 ret = PyList_New(0);
299 else {
300 int i;
301 xmlNodePtr node;
302
303 ret = PyList_New(obj->nodesetval->nodeNr);
304 for (i = 0;i < obj->nodesetval->nodeNr;i++) {
305 node = obj->nodesetval->nodeTab[i];
306 /* TODO: try to cast directly to the proper node type */
307 PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
308 }
309 }
310 break;
311 case XPATH_BOOLEAN:
312 ret = PyInt_FromLong((long) obj->boolval);
313 break;
314 case XPATH_NUMBER:
315 ret = PyFloat_FromDouble(obj->floatval);
316 break;
317 case XPATH_STRING:
318 ret = PyString_FromString(obj->stringval);
319 break;
320 case XPATH_POINT:
321 case XPATH_RANGE:
322 case XPATH_LOCATIONSET:
323 default:
324 printf("Unable to convert XPath object type %d\n", obj->type);
325 Py_INCREF(Py_None);
326 ret = Py_None;
327 }
328 xmlXPathFreeObject(obj);
329 return(ret);
330}
331
332xmlXPathObjectPtr
333libxml_xmlXPathObjectPtrConvert(PyObject * obj) {
334 xmlXPathObjectPtr ret;
335
336#ifdef DEBUG
337 printf("libxml_xmlXPathObjectPtrConvert: obj = %p\n", obj);
338#endif
339 if (obj == NULL) {
340 return(NULL);
341 }
342 if PyFloat_Check(obj) {
343 ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj));
344 } else if PyString_Check(obj) {
345 xmlChar *str;
346
347 str = xmlStrndup((const xmlChar *)PyString_AS_STRING(obj),
348 PyString_GET_SIZE(obj));
349 ret = xmlXPathWrapString(str);
350 } else {
351 printf("Unable to convert Python Object to XPath");
352 }
353 Py_DECREF(obj);
354 return(ret);
355}
356
Daniel Veillard7db38712002-02-07 16:39:11 +0000357PyObject *
358libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal) {
359 PyObject *ret;
360
361#ifdef DEBUG
362 printf("libxml_xmlNodePtrWrap: catal = %p\n", catal);
363#endif
364 if (catal == NULL) {
365 Py_INCREF(Py_None);
366 return(Py_None);
367 }
368 ret = PyCObject_FromVoidPtrAndDesc((void *) catal, "xmlCatalogPtr", NULL);
369 return(ret);
370}
371
Daniel Veillard3ce52572002-02-03 15:08:05 +0000372