blob: 56f472f3b86e14ab4e328eee5663d07402e1dbbb [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 *
62libxml_xmlCharPtrWrap(xmlChar *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 xmlFree(str);
75 return(ret);
76}
77
78PyObject *
79libxml_constcharPtrWrap(const char *str) {
80 PyObject *ret;
81
82#ifdef DEBUG
83 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
84#endif
85 if (str == NULL) {
86 Py_INCREF(Py_None);
87 return(Py_None);
88 }
89 /* TODO: look at deallocation */
90 ret = PyString_FromString(str);
91 return(ret);
92}
93
94PyObject *
95libxml_constxmlCharPtrWrap(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 *
111libxml_xmlDocPtrWrap(xmlDocPtr doc) {
112 PyObject *ret;
113
114#ifdef DEBUG
115 printf("libxml_xmlDocPtrWrap: doc = %p\n", doc);
116#endif
117 if (doc == NULL) {
118 Py_INCREF(Py_None);
119 return(Py_None);
120 }
121 /* TODO: look at deallocation */
122 ret = PyCObject_FromVoidPtrAndDesc((void *) doc, "xmlDocPtr", NULL);
123 return(ret);
124}
125
126PyObject *
127libxml_xmlNodePtrWrap(xmlNodePtr node) {
128 PyObject *ret;
129
130#ifdef DEBUG
131 printf("libxml_xmlNodePtrWrap: node = %p\n", node);
132#endif
133 if (node == NULL) {
134 Py_INCREF(Py_None);
135 return(Py_None);
136 }
137 ret = PyCObject_FromVoidPtrAndDesc((void *) node, "xmlNodePtr", NULL);
138 return(ret);
139}
140
141PyObject *
142libxml_xmlNsPtrWrap(xmlNsPtr ns) {
143 PyObject *ret;
144
145#ifdef DEBUG
146 printf("libxml_xmlNsPtrWrap: node = %p\n", ns);
147#endif
148 if (ns == NULL) {
149 Py_INCREF(Py_None);
150 return(Py_None);
151 }
152 ret = PyCObject_FromVoidPtrAndDesc((void *) ns, "xmlNsPtr", NULL);
153 return(ret);
154}
155
156PyObject *
157libxml_xmlAttrPtrWrap(xmlAttrPtr attr) {
158 PyObject *ret;
159
160#ifdef DEBUG
161 printf("libxml_xmlAttrNodePtrWrap: attr = %p\n", attr);
162#endif
163 if (attr == NULL) {
164 Py_INCREF(Py_None);
165 return(Py_None);
166 }
167 ret = PyCObject_FromVoidPtrAndDesc((void *) attr, "xmlAttrPtr", NULL);
168 return(ret);
169}
170
171PyObject *
172libxml_xmlAttributePtrWrap(xmlAttributePtr attr) {
173 PyObject *ret;
174
175#ifdef DEBUG
176 printf("libxml_xmlAttributePtrWrap: attr = %p\n", attr);
177#endif
178 if (attr == NULL) {
179 Py_INCREF(Py_None);
180 return(Py_None);
181 }
182 ret = PyCObject_FromVoidPtrAndDesc((void *) attr, "xmlAttributePtr", NULL);
183 return(ret);
184}
185
186PyObject *
187libxml_xmlElementPtrWrap(xmlElementPtr elem) {
188 PyObject *ret;
189
190#ifdef DEBUG
191 printf("libxml_xmlElementNodePtrWrap: elem = %p\n", elem);
192#endif
193 if (elem == NULL) {
194 Py_INCREF(Py_None);
195 return(Py_None);
196 }
197 ret = PyCObject_FromVoidPtrAndDesc((void *) elem, "xmlElementPtr", NULL);
198 return(ret);
199}
200
201PyObject *
202libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt) {
203 PyObject *ret;
204
205#ifdef DEBUG
206 printf("libxml_xmlXPathContextPtrWrap: ctxt = %p\n", ctxt);
207#endif
208 if (ctxt == NULL) {
209 Py_INCREF(Py_None);
210 return(Py_None);
211 }
212 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt, "xmlXPathContextPtr",
213 NULL);
214 return(ret);
215}
216
217PyObject *
218libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt) {
219 PyObject *ret;
220
221#ifdef DEBUG
222 printf("libxml_xmlParserCtxtPtrWrap: ctxt = %p\n", ctxt);
223#endif
224 if (ctxt == NULL) {
225 Py_INCREF(Py_None);
226 return(Py_None);
227 }
228 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt, "xmlParserCtxtPtr",
229 NULL);
230 return(ret);
231}
232
233PyObject *
234libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj) {
235 PyObject *ret;
236
237#ifdef DEBUG
238 printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj);
239#endif
240 if (obj == NULL) {
241 Py_INCREF(Py_None);
242 return(Py_None);
243 }
244 switch(obj->type) {
245 case XPATH_XSLT_TREE:
246 /* TODO !!!! Allocation problems */
247 case XPATH_NODESET:
248 if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0))
249 ret = PyList_New(0);
250 else {
251 int i;
252 xmlNodePtr node;
253
254 ret = PyList_New(obj->nodesetval->nodeNr);
255 for (i = 0;i < obj->nodesetval->nodeNr;i++) {
256 node = obj->nodesetval->nodeTab[i];
257 /* TODO: try to cast directly to the proper node type */
258 PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
259 }
260 }
261 break;
262 case XPATH_BOOLEAN:
263 ret = PyInt_FromLong((long) obj->boolval);
264 break;
265 case XPATH_NUMBER:
266 ret = PyFloat_FromDouble(obj->floatval);
267 break;
268 case XPATH_STRING:
269 ret = PyString_FromString(obj->stringval);
270 break;
271 case XPATH_POINT:
272 case XPATH_RANGE:
273 case XPATH_LOCATIONSET:
274 default:
275 printf("Unable to convert XPath object type %d\n", obj->type);
276 Py_INCREF(Py_None);
277 ret = Py_None;
278 }
279 xmlXPathFreeObject(obj);
280 return(ret);
281}
282
283xmlXPathObjectPtr
284libxml_xmlXPathObjectPtrConvert(PyObject * obj) {
285 xmlXPathObjectPtr ret;
286
287#ifdef DEBUG
288 printf("libxml_xmlXPathObjectPtrConvert: obj = %p\n", obj);
289#endif
290 if (obj == NULL) {
291 return(NULL);
292 }
293 if PyFloat_Check(obj) {
294 ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj));
295 } else if PyString_Check(obj) {
296 xmlChar *str;
297
298 str = xmlStrndup((const xmlChar *)PyString_AS_STRING(obj),
299 PyString_GET_SIZE(obj));
300 ret = xmlXPathWrapString(str);
301 } else {
302 printf("Unable to convert Python Object to XPath");
303 }
304 Py_DECREF(obj);
305 return(ret);
306}
307
308