[gambit-list] problem with ffi and libxml2

Cyrille Duret cduret at gmail.com
Tue Jul 17 04:13:10 EDT 2012

I have problem to parse xml string with ffi and libxml2.

My code is in the test file libxml-raw.scm :

(c-declare #<<end

#include <stdlib.h>
#include <libxml/xmlreader.h>

xmlTextReaderPtr create_reader(const char* buffer, size_t len) {
  return xmlReaderForMemory(buffer, len, "_.xml", NULL, 0);

int first_type_prime(xmlTextReaderPtr reader) {
  int type;
  if( reader != NULL ) {
    int cont = xmlTextReaderRead(reader);
    if( cont ) {
      type = xmlTextReaderNodeType(reader);
      printf("read %d\n", type);
      return type;
    return -1;

int first_type(const char* s, size_t len) {
  return first_type_prime(create_reader(s, len));


(c-define-type xmlTextReader "xmlTextReader")
(c-define-type xmlTextReaderPtr (pointer xmlTextReader))

(define create-reader
  (c-lambda (nonnull-char-string unsigned-int) xmlTextReaderPtr
(define first-type-prime
  (c-lambda (xmlTextReaderPtr) int "first_type_prime"))
(define first-type
  (c-lambda (nonnull-char-string unsigned-int) int "first_type"))

Two test functions are defined here :

The first-type function take xml body and create the parser within the c
The first-type-prime uses a pointer on xmlTextReader structure stored and
the scheme-side.

The first-type-prime function fails and I don't know what's going on :

@ gsc-gambit
Gambit v4.6.4

> (compile-file "libxml-raw.scm" 'cc-options: "-Wno-write-strings
-U___SINGLE_HOST -I/usr/local/include/libxml2" 'ld-options:
"-L/usr/local/lib/ -lxml2")
> (load "libxml-raw")

> (define r (create-reader "<xml>toto</xml>" 15))

> r
#<xmlTextReader* #2 0x801781300>
> (first-type-prime r)

_.xml:1: parser error : Char 0x0 out of allowed range
_.xml:1: parser error : Couldn't find end of Start Tag xml
read 0

while this call works :

> (first-type "<xml>toto</xml>" 15)
read 1

Any idea what's happened ?
thanks a lot
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20120717/aca2e6e8/attachment.htm>

More information about the Gambit-list mailing list