hello, 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); xmlFreeTextReader(reader); return type; } xmlFreeTextReader(reader); return -1; } }
int first_type(const char* s, size_t len) { return first_type_prime(create_reader(s, len)); }
end )
(c-define-type xmlTextReader "xmlTextReader") (c-define-type xmlTextReaderPtr (pointer xmlTextReader))
(define create-reader (c-lambda (nonnull-char-string unsigned-int) xmlTextReaderPtr "create_reader")) (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 body. 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") "/usr/users/cydu/programming/scheme/modules/xml/libxml-raw.o1"
(load "libxml-raw")
"/usr/users/cydu/programming/scheme/modules/xml/libxml-raw.o1"
(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 ^ _.xml:1: parser error : Couldn't find end of Start Tag xml <xml ^ read 0 0
while this call works :
(first-type "<xml>toto</xml>" 15)
read 1 1
Any idea what's happened ? thanks a lot cyrille
Afficher les réponses par date
From: Cyrille Duret cduret@gmail.com Subject: [gambit-list] problem with ffi and libxml2 Date: Tue, 17 Jul 2012 10:13:10 +0200
hello, 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); }
What are the major advantages of using libxml2 xmlreader with ffi over using sxml?
Klaus Schilling
I have created a module system for my needs far more simpler than blackhole and I cannot integrate sxml in my module framework. libxml2 is the best option for me.
On Tue, Jul 17, 2012 at 11:55 AM, Klaus Schilling schilling.klaus@web.dewrote:
From: Cyrille Duret cduret@gmail.com Subject: [gambit-list] problem with ffi and libxml2 Date: Tue, 17 Jul 2012 10:13:10 +0200
hello, 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); }
What are the major advantages of using libxml2 xmlreader with ffi over using sxml?
Klaus Schilling
I think the string you are pointing to in create_reader is allowed to be moved by the garbage collector when you return to scheme land. In first-type, there is no return to scheme so you string constant stays in the same place until after first_type_prime completes. I think if you search the list you can see an example of where Marc makes an object immovable by the garbage collector. Alternatively you can copy the string in the create_reader call. -Fred
On Tue, Jul 17, 2012 at 7:37 PM, Cyrille Duret cduret@gmail.com wrote:
I have created a module system for my needs far more simpler than blackhole and I cannot integrate sxml in my module framework. libxml2 is the best option for me.
On Tue, Jul 17, 2012 at 11:55 AM, Klaus Schilling schilling.klaus@web.de wrote:
From: Cyrille Duret cduret@gmail.com Subject: [gambit-list] problem with ffi and libxml2 Date: Tue, 17 Jul 2012 10:13:10 +0200
hello, 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); }
What are the major advantages of using libxml2 xmlreader with ffi over using sxml?
Klaus Schilling
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
Yes thank u, I made it works by creating a char* buffer from the C world and copy scheme string to that buffer : ie: (define create-buffer (c-lambda (char-string) (pointer char) " int len = strlen(___arg1); char* buffer = (char*)malloc((len+1)*sizeof(char)); strncpy(buffer, ___arg1, len); ___result_voidstar = buffer; ")) (define release-buffer (c-lambda ((pointer char)) void "free(___arg1);"))
The solution of making a still string object seems more tricky to me because the memory management between C and scheme is still not clear enough to me. I have to dive more into gambit internals..
cyrille
On Wed, Jul 18, 2012 at 4:32 AM, Frederick LeMaster <fred.lemaster@gmail.com
wrote:
I think the string you are pointing to in create_reader is allowed to be moved by the garbage collector when you return to scheme land. In first-type, there is no return to scheme so you string constant stays in the same place until after first_type_prime completes. I think if you search the list you can see an example of where Marc makes an object immovable by the garbage collector. Alternatively you can copy the string in the create_reader call. -Fred
On Tue, Jul 17, 2012 at 7:37 PM, Cyrille Duret cduret@gmail.com wrote:
I have created a module system for my needs far more simpler than
blackhole
and I cannot integrate sxml in my module framework. libxml2 is the best option for me.
On Tue, Jul 17, 2012 at 11:55 AM, Klaus Schilling <
schilling.klaus@web.de>
wrote:
From: Cyrille Duret cduret@gmail.com Subject: [gambit-list] problem with ffi and libxml2 Date: Tue, 17 Jul 2012 10:13:10 +0200
hello, 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); }
What are the major advantages of using libxml2 xmlreader with ffi over using sxml?
Klaus Schilling
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
Below, I will understand that the use of SXML is made with the SSAX-SXML library, which has several parts (serialization, sxpath etc.) and is distributed for lots of Scheme implementations on a public domain basis. Essential parts of it are available as Black Hole modules for anyone for whom that might be of interest. Now:
What is advantageous of using SXML or LIBXML depends on the application.
Motivations for using a C library for a particular mechanism that is already implemented in very very well in Scheme would be, that you are using some code that requires you to use that C library anyhow, or, that there would happen to be some routines implemented in that C library that you need, are not in the Scheme library, and by some reason would be very time-consuming to implement. Also of course, like Cyrille points out, there can be occasions when you want or need a specific library choice due to reasons relating to how you handle the code in your application, or the specific computer environment in which your application will execute.
From a very wide perspective, I relate to such occasions as very rare.
SXML is a data format standard that expresses the entire XML format in S-expression form. For several purposes this is highly usable.
As a general rule, processing Scheme objects rather than FFI objects carries many benefits with it: the language and routines for processing and manipulation available is extremely rich already (sporadic examples: iteration and serialization), your need of custom code for many operations is zero, and also as a sidenote the memory handling is implicit in all steps, including safety of memory fragmentation.
SXML is, generally speaking, an excellent example of when this applies. The only thing that SXML does not include, which a typical XML library does include, is a parent element attribute on (SXML/XML) elements. For the one for whom that would show to be relevant though, it's easy enough to implement, didn't hear anyone ask for that up to now though.
If you have further thoughts or questions please feel free to share, Mikael
2012/7/17 Klaus Schilling schilling.klaus@web.de
What are the major advantages of using libxml2 xmlreader with ffi over using sxml?
Klaus Schilling