set eol-style:native
[reactos.git] / reactos / lib / libxml2 / python / types.c
1 /*
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
11 PyObject *
12 libxml_intWrap(int val)
13 {
14 PyObject *ret;
15
16 #ifdef DEBUG
17 printf("libxml_intWrap: val = %d\n", val);
18 #endif
19 ret = PyInt_FromLong((long) val);
20 return (ret);
21 }
22
23 PyObject *
24 libxml_longWrap(long val)
25 {
26 PyObject *ret;
27
28 #ifdef DEBUG
29 printf("libxml_longWrap: val = %ld\n", val);
30 #endif
31 ret = PyInt_FromLong(val);
32 return (ret);
33 }
34
35 PyObject *
36 libxml_doubleWrap(double val)
37 {
38 PyObject *ret;
39
40 #ifdef DEBUG
41 printf("libxml_doubleWrap: val = %f\n", val);
42 #endif
43 ret = PyFloat_FromDouble((double) val);
44 return (ret);
45 }
46
47 PyObject *
48 libxml_charPtrWrap(char *str)
49 {
50 PyObject *ret;
51
52 #ifdef DEBUG
53 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
54 #endif
55 if (str == NULL) {
56 Py_INCREF(Py_None);
57 return (Py_None);
58 }
59 /* TODO: look at deallocation */
60 ret = PyString_FromString(str);
61 xmlFree(str);
62 return (ret);
63 }
64
65 PyObject *
66 libxml_charPtrConstWrap(const char *str)
67 {
68 PyObject *ret;
69
70 #ifdef DEBUG
71 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
72 #endif
73 if (str == NULL) {
74 Py_INCREF(Py_None);
75 return (Py_None);
76 }
77 /* TODO: look at deallocation */
78 ret = PyString_FromString(str);
79 return (ret);
80 }
81
82 PyObject *
83 libxml_xmlCharPtrWrap(xmlChar * str)
84 {
85 PyObject *ret;
86
87 #ifdef DEBUG
88 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
89 #endif
90 if (str == NULL) {
91 Py_INCREF(Py_None);
92 return (Py_None);
93 }
94 /* TODO: look at deallocation */
95 ret = PyString_FromString((char *) str);
96 xmlFree(str);
97 return (ret);
98 }
99
100 PyObject *
101 libxml_xmlCharPtrConstWrap(const xmlChar * str)
102 {
103 PyObject *ret;
104
105 #ifdef DEBUG
106 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
107 #endif
108 if (str == NULL) {
109 Py_INCREF(Py_None);
110 return (Py_None);
111 }
112 /* TODO: look at deallocation */
113 ret = PyString_FromString((char *) str);
114 return (ret);
115 }
116
117 PyObject *
118 libxml_constcharPtrWrap(const char *str)
119 {
120 PyObject *ret;
121
122 #ifdef DEBUG
123 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
124 #endif
125 if (str == NULL) {
126 Py_INCREF(Py_None);
127 return (Py_None);
128 }
129 /* TODO: look at deallocation */
130 ret = PyString_FromString(str);
131 return (ret);
132 }
133
134 PyObject *
135 libxml_constxmlCharPtrWrap(const xmlChar * str)
136 {
137 PyObject *ret;
138
139 #ifdef DEBUG
140 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
141 #endif
142 if (str == NULL) {
143 Py_INCREF(Py_None);
144 return (Py_None);
145 }
146 /* TODO: look at deallocation */
147 ret = PyString_FromString((char *) str);
148 return (ret);
149 }
150
151 PyObject *
152 libxml_xmlDocPtrWrap(xmlDocPtr doc)
153 {
154 PyObject *ret;
155
156 #ifdef DEBUG
157 printf("libxml_xmlDocPtrWrap: doc = %p\n", doc);
158 #endif
159 if (doc == NULL) {
160 Py_INCREF(Py_None);
161 return (Py_None);
162 }
163 /* TODO: look at deallocation */
164 ret =
165 PyCObject_FromVoidPtrAndDesc((void *) doc, (char *) "xmlDocPtr",
166 NULL);
167 return (ret);
168 }
169
170 PyObject *
171 libxml_xmlNodePtrWrap(xmlNodePtr node)
172 {
173 PyObject *ret;
174
175 #ifdef DEBUG
176 printf("libxml_xmlNodePtrWrap: node = %p\n", node);
177 #endif
178 if (node == NULL) {
179 Py_INCREF(Py_None);
180 return (Py_None);
181 }
182 ret =
183 PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "xmlNodePtr",
184 NULL);
185 return (ret);
186 }
187
188 PyObject *
189 libxml_xmlURIPtrWrap(xmlURIPtr uri)
190 {
191 PyObject *ret;
192
193 #ifdef DEBUG
194 printf("libxml_xmlURIPtrWrap: uri = %p\n", uri);
195 #endif
196 if (uri == NULL) {
197 Py_INCREF(Py_None);
198 return (Py_None);
199 }
200 ret =
201 PyCObject_FromVoidPtrAndDesc((void *) uri, (char *) "xmlURIPtr",
202 NULL);
203 return (ret);
204 }
205
206 PyObject *
207 libxml_xmlNsPtrWrap(xmlNsPtr ns)
208 {
209 PyObject *ret;
210
211 #ifdef DEBUG
212 printf("libxml_xmlNsPtrWrap: node = %p\n", ns);
213 #endif
214 if (ns == NULL) {
215 Py_INCREF(Py_None);
216 return (Py_None);
217 }
218 ret =
219 PyCObject_FromVoidPtrAndDesc((void *) ns, (char *) "xmlNsPtr",
220 NULL);
221 return (ret);
222 }
223
224 PyObject *
225 libxml_xmlAttrPtrWrap(xmlAttrPtr attr)
226 {
227 PyObject *ret;
228
229 #ifdef DEBUG
230 printf("libxml_xmlAttrNodePtrWrap: attr = %p\n", attr);
231 #endif
232 if (attr == NULL) {
233 Py_INCREF(Py_None);
234 return (Py_None);
235 }
236 ret =
237 PyCObject_FromVoidPtrAndDesc((void *) attr, (char *) "xmlAttrPtr",
238 NULL);
239 return (ret);
240 }
241
242 PyObject *
243 libxml_xmlAttributePtrWrap(xmlAttributePtr attr)
244 {
245 PyObject *ret;
246
247 #ifdef DEBUG
248 printf("libxml_xmlAttributePtrWrap: attr = %p\n", attr);
249 #endif
250 if (attr == NULL) {
251 Py_INCREF(Py_None);
252 return (Py_None);
253 }
254 ret =
255 PyCObject_FromVoidPtrAndDesc((void *) attr,
256 (char *) "xmlAttributePtr", NULL);
257 return (ret);
258 }
259
260 PyObject *
261 libxml_xmlElementPtrWrap(xmlElementPtr elem)
262 {
263 PyObject *ret;
264
265 #ifdef DEBUG
266 printf("libxml_xmlElementNodePtrWrap: elem = %p\n", elem);
267 #endif
268 if (elem == NULL) {
269 Py_INCREF(Py_None);
270 return (Py_None);
271 }
272 ret =
273 PyCObject_FromVoidPtrAndDesc((void *) elem,
274 (char *) "xmlElementPtr", NULL);
275 return (ret);
276 }
277
278 PyObject *
279 libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt)
280 {
281 PyObject *ret;
282
283 #ifdef DEBUG
284 printf("libxml_xmlXPathContextPtrWrap: ctxt = %p\n", ctxt);
285 #endif
286 if (ctxt == NULL) {
287 Py_INCREF(Py_None);
288 return (Py_None);
289 }
290 ret =
291 PyCObject_FromVoidPtrAndDesc((void *) ctxt,
292 (char *) "xmlXPathContextPtr", NULL);
293 return (ret);
294 }
295
296 PyObject *
297 libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)
298 {
299 PyObject *ret;
300
301 #ifdef DEBUG
302 printf("libxml_xmlXPathParserContextPtrWrap: ctxt = %p\n", ctxt);
303 #endif
304 if (ctxt == NULL) {
305 Py_INCREF(Py_None);
306 return (Py_None);
307 }
308 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt,
309 (char *) "xmlXPathParserContextPtr",
310 NULL);
311 return (ret);
312 }
313
314 PyObject *
315 libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
316 {
317 PyObject *ret;
318
319 #ifdef DEBUG
320 printf("libxml_xmlParserCtxtPtrWrap: ctxt = %p\n", ctxt);
321 #endif
322 if (ctxt == NULL) {
323 Py_INCREF(Py_None);
324 return (Py_None);
325 }
326
327 ret =
328 PyCObject_FromVoidPtrAndDesc((void *) ctxt,
329 (char *) "xmlParserCtxtPtr", NULL);
330 return (ret);
331 }
332
333 PyObject *
334 libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
335 {
336 PyObject *ret;
337
338 #ifdef DEBUG
339 printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj);
340 #endif
341 if (obj == NULL) {
342 Py_INCREF(Py_None);
343 return (Py_None);
344 }
345 switch (obj->type) {
346 case XPATH_XSLT_TREE: {
347 if ((obj->nodesetval == NULL) ||
348 (obj->nodesetval->nodeNr == 0) ||
349 (obj->nodesetval->nodeTab == NULL)) {
350 ret = PyList_New(0);
351 } else {
352 int i, len = 0;
353 xmlNodePtr node;
354
355 node = obj->nodesetval->nodeTab[0]->children;
356 while (node != NULL) {
357 len++;
358 node = node->next;
359 }
360 ret = PyList_New(len);
361 node = obj->nodesetval->nodeTab[0]->children;
362 for (i = 0;i < len;i++) {
363 PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
364 node = node->next;
365 }
366 }
367 /*
368 * Return now, do not free the object passed down
369 */
370 return (ret);
371 }
372 case XPATH_NODESET:
373 if ((obj->nodesetval == NULL)
374 || (obj->nodesetval->nodeNr == 0)) {
375 ret = PyList_New(0);
376 } else {
377 int i;
378 xmlNodePtr node;
379
380 ret = PyList_New(obj->nodesetval->nodeNr);
381 for (i = 0; i < obj->nodesetval->nodeNr; i++) {
382 node = obj->nodesetval->nodeTab[i];
383 /* TODO: try to cast directly to the proper node type */
384 PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
385 }
386 }
387 break;
388 case XPATH_BOOLEAN:
389 ret = PyInt_FromLong((long) obj->boolval);
390 break;
391 case XPATH_NUMBER:
392 ret = PyFloat_FromDouble(obj->floatval);
393 break;
394 case XPATH_STRING:
395 ret = PyString_FromString((char *) obj->stringval);
396 break;
397 case XPATH_POINT:
398 case XPATH_RANGE:
399 case XPATH_LOCATIONSET:
400 default:
401 printf("Unable to convert XPath object type %d\n", obj->type);
402 Py_INCREF(Py_None);
403 ret = Py_None;
404 }
405 xmlXPathFreeObject(obj);
406 return (ret);
407 }
408
409 xmlXPathObjectPtr
410 libxml_xmlXPathObjectPtrConvert(PyObject * obj)
411 {
412 xmlXPathObjectPtr ret = NULL;
413
414 #ifdef DEBUG
415 printf("libxml_xmlXPathObjectPtrConvert: obj = %p\n", obj);
416 #endif
417 if (obj == NULL) {
418 return (NULL);
419 }
420 if PyFloat_Check
421 (obj) {
422 ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj));
423 } else if PyString_Check
424 (obj) {
425 xmlChar *str;
426
427 str = xmlStrndup((const xmlChar *) PyString_AS_STRING(obj),
428 PyString_GET_SIZE(obj));
429 ret = xmlXPathWrapString(str);
430 } else if PyList_Check
431 (obj) {
432 int i;
433 PyObject *node;
434 xmlNodePtr cur;
435 xmlNodeSetPtr set;
436
437 set = xmlXPathNodeSetCreate(NULL);
438
439 for (i = 0; i < PyList_Size(obj); i++) {
440 node = PyList_GetItem(obj, i);
441 if ((node == NULL) || (node->ob_type == NULL))
442 continue;
443
444 cur = NULL;
445 if (PyCObject_Check(node)) {
446 printf("Got a CObject\n");
447 cur = PyxmlNode_Get(node);
448 } else if (PyInstance_Check(node)) {
449 PyInstanceObject *inst = (PyInstanceObject *) node;
450 PyObject *name = inst->in_class->cl_name;
451
452 if PyString_Check
453 (name) {
454 char *type = PyString_AS_STRING(name);
455 PyObject *wrapper;
456
457 if (!strcmp(type, "xmlNode")) {
458 wrapper =
459 PyObject_GetAttrString(node, (char *) "_o");
460 if (wrapper != NULL) {
461 cur = PyxmlNode_Get(wrapper);
462 }
463 }
464 }
465 } else {
466 printf("Unknown object in Python return list\n");
467 }
468 if (cur != NULL) {
469 xmlXPathNodeSetAdd(set, cur);
470 }
471 }
472 ret = xmlXPathWrapNodeSet(set);
473 } else {
474 printf("Unable to convert Python Object to XPath");
475 }
476 Py_DECREF(obj);
477 return (ret);
478 }
479
480 PyObject *
481 libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid)
482 {
483 PyObject *ret;
484
485 #ifdef DEBUG
486 printf("libxml_xmlValidCtxtPtrWrap: valid = %p\n", valid);
487 #endif
488 if (valid == NULL) {
489 Py_INCREF(Py_None);
490 return (Py_None);
491 }
492
493 ret =
494 PyCObject_FromVoidPtrAndDesc((void *) valid,
495 (char *) "xmlValidCtxtPtr", NULL);
496
497 return (ret);
498 }
499
500 PyObject *
501 libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal)
502 {
503 PyObject *ret;
504
505 #ifdef DEBUG
506 printf("libxml_xmlNodePtrWrap: catal = %p\n", catal);
507 #endif
508 if (catal == NULL) {
509 Py_INCREF(Py_None);
510 return (Py_None);
511 }
512 ret =
513 PyCObject_FromVoidPtrAndDesc((void *) catal,
514 (char *) "xmlCatalogPtr", NULL);
515 return (ret);
516 }
517
518 PyObject *
519 libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer)
520 {
521 PyObject *ret;
522
523 #ifdef DEBUG
524 printf("libxml_xmlOutputBufferPtrWrap: buffer = %p\n", buffer);
525 #endif
526 if (buffer == NULL) {
527 Py_INCREF(Py_None);
528 return (Py_None);
529 }
530 ret =
531 PyCObject_FromVoidPtrAndDesc((void *) buffer,
532 (char *) "xmlOutputBufferPtr", NULL);
533 return (ret);
534 }
535
536 PyObject *
537 libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer)
538 {
539 PyObject *ret;
540
541 #ifdef DEBUG
542 printf("libxml_xmlParserInputBufferPtrWrap: buffer = %p\n", buffer);
543 #endif
544 if (buffer == NULL) {
545 Py_INCREF(Py_None);
546 return (Py_None);
547 }
548 ret =
549 PyCObject_FromVoidPtrAndDesc((void *) buffer,
550 (char *) "xmlParserInputBufferPtr", NULL);
551 return (ret);
552 }
553
554 #ifdef LIBXML_REGEXP_ENABLED
555 PyObject *
556 libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp)
557 {
558 PyObject *ret;
559
560 #ifdef DEBUG
561 printf("libxml_xmlRegexpPtrWrap: regexp = %p\n", regexp);
562 #endif
563 if (regexp == NULL) {
564 Py_INCREF(Py_None);
565 return (Py_None);
566 }
567 ret =
568 PyCObject_FromVoidPtrAndDesc((void *) regexp,
569 (char *) "xmlRegexpPtr", NULL);
570 return (ret);
571 }
572 #endif /* LIBXML_REGEXP_ENABLED */
573
574 PyObject *
575 libxml_xmlTextReaderPtrWrap(xmlTextReaderPtr reader)
576 {
577 PyObject *ret;
578
579 #ifdef DEBUG
580 printf("libxml_xmlTextReaderPtrWrap: reader = %p\n", reader);
581 #endif
582 if (reader == NULL) {
583 Py_INCREF(Py_None);
584 return (Py_None);
585 }
586 ret =
587 PyCObject_FromVoidPtrAndDesc((void *) reader,
588 (char *) "xmlTextReaderPtr", NULL);
589 return (ret);
590 }
591
592 PyObject *
593 libxml_xmlTextReaderLocatorPtrWrap(xmlTextReaderLocatorPtr locator)
594 {
595 PyObject *ret;
596
597 #ifdef DEBUG
598 printf("libxml_xmlTextReaderLocatorPtrWrap: locator = %p\n", locator);
599 #endif
600 if (locator == NULL) {
601 Py_INCREF(Py_None);
602 return (Py_None);
603 }
604 ret =
605 PyCObject_FromVoidPtrAndDesc((void *) locator,
606 (char *) "xmlTextReaderLocatorPtr", NULL);
607 return (ret);
608 }
609
610 #ifdef LIBXML_SCHEMAS_ENABLED
611 PyObject *
612 libxml_xmlRelaxNGPtrWrap(xmlRelaxNGPtr ctxt)
613 {
614 PyObject *ret;
615
616 #ifdef DEBUG
617 printf("libxml_xmlRelaxNGPtrWrap: ctxt = %p\n", ctxt);
618 #endif
619 if (ctxt == NULL) {
620 Py_INCREF(Py_None);
621 return (Py_None);
622 }
623 ret =
624 PyCObject_FromVoidPtrAndDesc((void *) ctxt,
625 (char *) "xmlRelaxNGPtr", NULL);
626 return (ret);
627 }
628
629 PyObject *
630 libxml_xmlRelaxNGParserCtxtPtrWrap(xmlRelaxNGParserCtxtPtr ctxt)
631 {
632 PyObject *ret;
633
634 #ifdef DEBUG
635 printf("libxml_xmlRelaxNGParserCtxtPtrWrap: ctxt = %p\n", ctxt);
636 #endif
637 if (ctxt == NULL) {
638 Py_INCREF(Py_None);
639 return (Py_None);
640 }
641 ret =
642 PyCObject_FromVoidPtrAndDesc((void *) ctxt,
643 (char *) "xmlRelaxNGParserCtxtPtr", NULL);
644 return (ret);
645 }
646 PyObject *
647 libxml_xmlRelaxNGValidCtxtPtrWrap(xmlRelaxNGValidCtxtPtr valid)
648 {
649 PyObject *ret;
650
651 #ifdef DEBUG
652 printf("libxml_xmlRelaxNGValidCtxtPtrWrap: valid = %p\n", valid);
653 #endif
654 if (valid == NULL) {
655 Py_INCREF(Py_None);
656 return (Py_None);
657 }
658 ret =
659 PyCObject_FromVoidPtrAndDesc((void *) valid,
660 (char *) "xmlRelaxNGValidCtxtPtr", NULL);
661 return (ret);
662 }
663
664 PyObject *
665 libxml_xmlSchemaPtrWrap(xmlSchemaPtr ctxt)
666 {
667 PyObject *ret;
668
669 #ifdef DEBUG
670 printf("libxml_xmlSchemaPtrWrap: ctxt = %p\n", ctxt);
671 #endif
672 if (ctxt == NULL) {
673 Py_INCREF(Py_None);
674 return (Py_None);
675 }
676 ret =
677 PyCObject_FromVoidPtrAndDesc((void *) ctxt,
678 (char *) "xmlSchemaPtr", NULL);
679 return (ret);
680 }
681
682 PyObject *
683 libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt)
684 {
685 PyObject *ret;
686
687 #ifdef DEBUG
688 printf("libxml_xmlSchemaParserCtxtPtrWrap: ctxt = %p\n", ctxt);
689 #endif
690 if (ctxt == NULL) {
691 Py_INCREF(Py_None);
692 return (Py_None);
693 }
694 ret =
695 PyCObject_FromVoidPtrAndDesc((void *) ctxt,
696 (char *) "xmlSchemaParserCtxtPtr", NULL);
697
698 return (ret);
699 }
700
701 PyObject *
702 libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid)
703 {
704 PyObject *ret;
705
706 #ifdef DEBUG
707 printf("libxml_xmlSchemaValidCtxtPtrWrap: valid = %p\n", valid);
708 #endif
709 if (valid == NULL) {
710 Py_INCREF(Py_None);
711 return (Py_None);
712 }
713
714 ret =
715 PyCObject_FromVoidPtrAndDesc((void *) valid,
716 (char *) "xmlSchemaValidCtxtPtr", NULL);
717
718 return (ret);
719 }
720 #endif /* LIBXML_SCHEMAS_ENABLED */
721
722 PyObject *
723 libxml_xmlErrorPtrWrap(xmlErrorPtr error)
724 {
725 PyObject *ret;
726
727 #ifdef DEBUG
728 printf("libxml_xmlErrorPtrWrap: error = %p\n", error);
729 #endif
730 if (error == NULL) {
731 Py_INCREF(Py_None);
732 return (Py_None);
733 }
734 ret =
735 PyCObject_FromVoidPtrAndDesc((void *) error,
736 (char *) "xmlErrorPtr", NULL);
737 return (ret);
738 }