From 38de66122cc36d5f1d457760864d0931696ed06e Mon Sep 17 00:00:00 2001 From: dakkar Date: Tue, 15 Sep 2015 16:38:40 +0100 Subject: stupidest possible libxml2 bindings? --- lib/XML/LibXML.pm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/XML/LibXML.pm (limited to 'lib/XML/LibXML.pm') diff --git a/lib/XML/LibXML.pm b/lib/XML/LibXML.pm new file mode 100644 index 0000000..a9c8fbb --- /dev/null +++ b/lib/XML/LibXML.pm @@ -0,0 +1,50 @@ +# -*- mode: perl6 -*- +use NativeCall; + +class XML::LibXML::Node is repr('CStruct') { + sub xmlFreeNode(XML::LibXML::Node $doc) is native('libxml2') {*} + submethod DESTROY { xmlFreeNode(self) } + + has Pointer $!_private; + has int $.xmlElementType; + has Str $.name; + # etc etc, don't try to create this +} + +class XML::LibXML::Document is repr('CPointer') { + sub xmlFreeDoc(XML::LibXML::Document $doc) is native('libxml2') {*} + submethod DESTROY { xmlFreeDoc(self) } + + sub xmlDocGetRootElement(XML::LibXML::Document $doc) + returns XML::LibXML::Node is native('libxml2') {*} + method rootElement() { + return xmlDocGetRootElement(self); + } +} + +class XML::LibXML::Context is repr('CStruct') { + sub xmlFreeParserCtxt(XML::LibXML::Context $ctxt) is native('libxml2') {*} + submethod DESTROY { xmlFreeParserCtxt(self) } + + sub xmlParseDocument(XML::LibXML::Context $ctxt) is native('libxml2') {*} + method parse() { xmlParseDocument(self) } + + has Pointer $!sax; # currently unused + has Pointer $!userData; # useless? + has XML::LibXML::Document $.myDoc; + # etc etc, don't try to create this +} + +class XML::LibXML { + sub xmlCreateMemoryParserCtxt(Blob $docbuf, int $len) returns XML::LibXML::Context is native('libxml2') {*} + + method parse(Str $docstring) { + my Blob $docbuf = $docstring.encode('utf8'); + my $ctxt = xmlCreateMemoryParserCtxt( + $docbuf, + $docbuf.bytes, + ); + $ctxt.parse(); + return $ctxt.myDoc; + } +} -- cgit v1.2.3