Skip to content

Instantly share code, notes, and snippets.

@torus
Created March 10, 2010 23:33
Show Gist options
  • Save torus/328590 to your computer and use it in GitHub Desktop.
Save torus/328590 to your computer and use it in GitHub Desktop.
SWIG interface file for LibXML2 tree API
// use SWIG version >= 1.3.40
%module libxml2
%{
#include <libxml2/libxml/tree.h>
#include <stdio.h>
%}
%native(nullNs) int getNullNs (lua_State *L);
%{
int getNullNs (lua_State *L)
{
lua_pushlightuserdata (L, NULL);
return 1;
}
%}
%typemap(in) xmlChar* {
$1 = (xmlChar*) (lua_tostring (L, $input));
}
%typemap(in) xmlNs* {
$1 = (xmlNs*) (lua_topointer (L, $input));
}
%typemap(out) xmlChar* {
lua_pushstring (L, (const char*)$1);
SWIG_arg ++;
}
%typemap(in, numinputs=0) (xmlChar ** mem, int * size) (xmlChar *temp, int templen) {
$1 = &temp;
$2 = &templen;
}
%typemap(argout) (xmlChar ** mem, int * size) {
// Append output value $1 to $result
lua_pushlstring (L, *$1, *$2);
SWIG_arg ++;
}
%include </usr/include/libxml2/libxml/xmlexports.h>
%include </usr/include/libxml2/libxml/xmlversion.h>
%include </usr/include/libxml2/libxml/tree.h>
require "xml"
local doc = xml.NewDoc "1.0"
local root = xml.NewNode (xml.nullNs, "root")
local child = xml.NewNode (xml.nullNs, "child")
xml.AddChild (root, child)
xml.DocSetRootElement (doc, root)
xml.NodeAddContent (child, "text content")
xml.SetProp (child, "attr", "value")
local str = xml.DocDumpMemory (doc)
print (str)
libxml2.so: libxml2_wrap.c
gcc -g -shared -fPIC -I/usr/include/libxml2 -o $@ $< -L/usr/lib/libxml2 -lxml2
libxml2_wrap.c: libxml2.i
swig -lua -o $@ $<
require "libxml2"
xml = {}
setmetatable (xml, {__index = function (tbl, key) return libxml2["xml" .. key] end})
xml.nullNs = libxml2.nullNs ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment