src/lxplib.c

Wed, 02 Apr 2014 20:56:58 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 02 Apr 2014 20:56:58 +0100
changeset 18
90da47758f85
parent 17
7f0e6f8bb8e0
child 19
10c30b63873a
permissions
-rw-r--r--

Add support for XmlDecl handlers

0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 ** $Id: lxplib.c,v 1.16 2007/06/05 20:03:12 carregal Exp $
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 ** LuaExpat: Lua bind for Expat library
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 ** See Copyright Notice in license.html
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 #include <assert.h>
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 #include <stdlib.h>
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 #include <string.h>
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 #include "expat.h"
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 #include "lua.h"
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 #include "lauxlib.h"
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 #include "lxplib.h"
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
21 #if !defined(lua_pushliteral)
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
22 #define lua_pushliteral(L, s) \
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
23 lua_pushstring(L, "" s, (sizeof(s)/sizeof(char))-1)
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
24 #endif
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
25
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 enum XPState {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 XPSpre, /* parser just initialized */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 XPSok, /* state while parsing */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 XPSfinished, /* state after finished parsing */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 XPSerror,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 XPSstring /* state while reading a string */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 struct lxp_userdata {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 lua_State *L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 XML_Parser parser; /* associated expat parser */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 int tableref; /* table with callbacks for this parser */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 enum XPState state;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 luaL_Buffer *b; /* to concatenate sequences of cdata pieces */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 typedef struct lxp_userdata lxp_userdata;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 static int reporterror (lxp_userdata *xpu) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 XML_Parser p = xpu->parser;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 lua_pushstring(L, XML_ErrorString(XML_GetErrorCode(p)));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 lua_pushnumber(L, XML_GetCurrentLineNumber(p));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 lua_pushnumber(L, XML_GetCurrentColumnNumber(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 lua_pushnumber(L, XML_GetCurrentByteIndex(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 return 5;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 static lxp_userdata *createlxp (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 lxp_userdata *xpu = (lxp_userdata *)lua_newuserdata(L, sizeof(lxp_userdata));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 xpu->tableref = LUA_REFNIL; /* in case of errors... */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 xpu->parser = NULL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 xpu->L = NULL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 xpu->state = XPSpre;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 luaL_getmetatable(L, ParserType);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 lua_setmetatable(L, -2);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return xpu;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 static void lxpclose (lua_State *L, lxp_userdata *xpu) {
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
71 luaL_unref(L, LUA_REGISTRYINDEX, xpu->tableref);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 xpu->tableref = LUA_REFNIL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if (xpu->parser)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 XML_ParserFree(xpu->parser);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 xpu->parser = NULL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 ** Auxiliary function to call a Lua handle
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 static void docall (lxp_userdata *xpu, int nargs, int nres) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 assert(xpu->state == XPSok);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if (lua_pcall(L, nargs + 1, nres, 0) != 0) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 xpu->state = XPSerror;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 luaL_unref(L, LUA_REGISTRYINDEX, xpu->tableref);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 xpu->tableref = luaL_ref(L, LUA_REGISTRYINDEX); /* error message */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 ** Check whether there is pending Cdata, and call its handle if necessary
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 static void dischargestring (lxp_userdata *xpu) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 assert(xpu->state == XPSstring);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 xpu->state = XPSok;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 luaL_pushresult(xpu->b);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 ** Check whether there is a Lua handle for a given event: If so,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 ** put it on the stack (to be called later), and also push `self'
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 static int getHandle (lxp_userdata *xpu, const char *handle) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 if (xpu->state == XPSstring) dischargestring(xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 if (xpu->state == XPSerror)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 return 0; /* some error happened before; skip all handles */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 lua_pushstring(L, handle);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 lua_gettable(L, 3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 if (lua_toboolean(L, -1) == 0) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 if (!lua_isfunction(L, -1)) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 luaL_error(L, "lxp `%s' callback is not a function", handle);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 lua_pushvalue(L, 1); /* first argument in every call (self) */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 ** {======================================================
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 ** Handles
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 ** =======================================================
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 static void f_StartCdata (void *ud) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 if (getHandle(xpu, StartCdataKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 docall(xpu, 0, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 static void f_EndCdataKey (void *ud) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 if (getHandle(xpu, EndCdataKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 docall(xpu, 0, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 static void f_CharData (void *ud, const char *s, int len) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 if (xpu->state == XPSok) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 if (getHandle(xpu, CharDataKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 xpu->state = XPSstring;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 luaL_buffinit(xpu->L, xpu->b);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if (xpu->state == XPSstring)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 luaL_addlstring(xpu->b, s, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 static void f_Comment (void *ud, const char *data) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 if (getHandle(xpu, CommentKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 lua_pushstring(xpu->L, data);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 static void f_Default (void *ud, const char *data, int len) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 if (getHandle(xpu, DefaultKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 lua_pushlstring(xpu->L, data, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 static void f_DefaultExpand (void *ud, const char *data, int len) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 if (getHandle(xpu, DefaultExpandKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 lua_pushlstring(xpu->L, data, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 static void f_StartElement (void *ud, const char *name, const char **attrs) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 int lastspec = XML_GetSpecifiedAttributeCount(xpu->parser) / 2;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 int i = 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 if (getHandle(xpu, StartElementKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 lua_pushstring(L, name);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 lua_newtable(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 while (*attrs) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 if (i <= lastspec) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 lua_pushnumber(L, i++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 lua_pushstring(L, *attrs);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 lua_settable(L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 lua_pushstring(L, *attrs++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 lua_pushstring(L, *attrs++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 lua_settable(L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 docall(xpu, 2, 0); /* call function with self, name, and attributes */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 static void f_EndElement (void *ud, const char *name) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 if (getHandle(xpu, EndElementKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 lua_pushstring(xpu->L, name);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 static int f_ExternaEntity (XML_Parser p, const char *context,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 const char *publicId) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 lxp_userdata *xpu = (lxp_userdata *)XML_GetUserData(p);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 lxp_userdata *child;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 if (getHandle(xpu, ExternalEntityKey) == 0) return 1; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 child = createlxp(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 child->parser = XML_ExternalEntityParserCreate(p, context, NULL);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 if (!child->parser)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 luaL_error(L, "XML_ParserCreate failed");
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
230 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /*lua_getref(L, xpu->tableref); */ /* child uses the same table of its father */
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 child->tableref = luaL_ref(L, LUA_REGISTRYINDEX);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 docall(xpu, 4, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 status = lua_toboolean(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 lxpclose(L, child);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 return status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 static void f_StartNamespaceDecl (void *ud, const char *prefix,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 const char *uri) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 if (getHandle(xpu, StartNamespaceDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 lua_pushstring(L, prefix);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 lua_pushstring(L, uri);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 docall(xpu, 2, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 static void f_EndNamespaceDecl (void *ud, const char *prefix) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 if (getHandle(xpu, EndNamespaceDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 lua_pushstring(xpu->L, prefix);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 static void f_NotationDecl (void *ud, const char *notationName,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 const char *publicId) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 if (getHandle(xpu, NotationDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 lua_pushstring(L, notationName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 docall(xpu, 4, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 static int f_NotStandalone (void *ud) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 if (getHandle(xpu, NotStandaloneKey) == 0) return 1; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 docall(xpu, 0, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 status = lua_toboolean(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 return status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 static void f_ProcessingInstruction (void *ud, const char *target,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 const char *data) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 if (getHandle(xpu, ProcessingInstructionKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 lua_pushstring(L, target);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 lua_pushstring(L, data);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 docall(xpu, 2, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 static void f_UnparsedEntityDecl (void *ud, const char *entityName,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 const char *publicId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 const char *notationName) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 if (getHandle(xpu, UnparsedEntityDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 lua_pushstring(L, entityName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 lua_pushstring(L, notationName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 docall(xpu, 5, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315
1
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
316 static void f_StartDoctypeDecl (void *ud, const XML_Char *doctypeName,
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
317 const XML_Char *sysid,
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
318 const XML_Char *pubid,
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
319 int has_internal_subset) {
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
320 lxp_userdata *xpu = (lxp_userdata *)ud;
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
321 if (getHandle(xpu, StartDoctypeDeclKey) == 0) return; /* no handle */
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
322 lua_pushstring(xpu->L, doctypeName);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
323 lua_pushstring(xpu->L, sysid);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
324 lua_pushstring(xpu->L, pubid);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
325 lua_pushboolean(xpu->L, has_internal_subset);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
326 docall(xpu, 4, 0);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
327 }
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
328
18
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
329 static void f_XmlDecl (void *ud, const XML_Char *version,
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
330 const XML_Char *encoding,
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
331 int standalone) {
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
332 lxp_userdata *xpu = (lxp_userdata *)ud;
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
333 if (getHandle(xpu, XmlDeclKey) == 0) return; /* no handle */
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
334 lua_pushstring(xpu->L, version);
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
335 lua_pushstring(xpu->L, encoding);
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
336 lua_pushboolean(xpu->L, standalone);
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
337 docall(xpu, 3, 0);
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
338 }
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 /* }====================================================== */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 static int hasfield (lua_State *L, const char *fname) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 int res;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 lua_pushstring(L, fname);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 lua_gettable(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 res = !lua_isnil(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 return res;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 static void checkcallbacks (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 static const char *const validkeys[] = {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 "StartCdataSection", "EndCdataSection", "CharacterData", "Comment",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 "Default", "DefaultExpand", "StartElement", "EndElement",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 "ExternalEntityRef", "StartNamespaceDecl", "EndNamespaceDecl",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 "NotationDecl", "NotStandalone", "ProcessingInstruction",
18
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
359 "UnparsedEntityDecl", "StartDoctypeDecl", "XmlDecl", NULL};
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 if (hasfield(L, "_nonstrict")) return;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 while (lua_next(L, 1)) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 lua_pop(L, 1); /* remove value */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 #if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 if (lua_type(L, -1) != LUA_TSTRING ||
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 luaL_findstring(lua_tostring(L, -1), validkeys) < 0)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 luaL_error(L, "invalid key `%s' in callback table", lua_tostring(L, -1));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 #else
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 luaL_checkoption(L, -1, NULL, validkeys);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 #endif
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 static int lxp_make_parser (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 XML_Parser p;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 char sep = *luaL_optstring(L, 2, "");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 lxp_userdata *xpu = createlxp(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) :
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 XML_ParserCreateNS(NULL, sep);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 if (!p)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 luaL_error(L, "XML_ParserCreate failed");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 luaL_checktype(L, 1, LUA_TTABLE);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 checkcallbacks(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 lua_pushvalue(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 xpu->tableref = luaL_ref(L, LUA_REGISTRYINDEX);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 XML_SetUserData(p, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 if (hasfield(L, StartCdataKey) || hasfield(L, EndCdataKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 XML_SetCdataSectionHandler(p, f_StartCdata, f_EndCdataKey);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390 if (hasfield(L, CharDataKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 XML_SetCharacterDataHandler(p, f_CharData);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 if (hasfield(L, CommentKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 XML_SetCommentHandler(p, f_Comment);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 if (hasfield(L, DefaultKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 XML_SetDefaultHandler(p, f_Default);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 if (hasfield(L, DefaultExpandKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 XML_SetDefaultHandlerExpand(p, f_DefaultExpand);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 if (hasfield(L, StartElementKey) || hasfield(L, EndElementKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 XML_SetElementHandler(p, f_StartElement, f_EndElement);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 if (hasfield(L, ExternalEntityKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 XML_SetExternalEntityRefHandler(p, f_ExternaEntity);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 if (hasfield(L, StartNamespaceDeclKey) || hasfield(L, EndNamespaceDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 XML_SetNamespaceDeclHandler(p, f_StartNamespaceDecl, f_EndNamespaceDecl);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 if (hasfield(L, NotationDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 XML_SetNotationDeclHandler(p, f_NotationDecl);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 if (hasfield(L, NotStandaloneKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 XML_SetNotStandaloneHandler(p, f_NotStandalone);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 if (hasfield(L, ProcessingInstructionKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 XML_SetProcessingInstructionHandler(p, f_ProcessingInstruction);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 if (hasfield(L, UnparsedEntityDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl);
1
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
412 if (hasfield(L, StartDoctypeDeclKey))
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
413 XML_SetStartDoctypeDeclHandler(p, f_StartDoctypeDecl);
18
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
414 if (hasfield(L, XmlDeclKey))
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
415 XML_SetXmlDeclHandler(p, f_XmlDecl);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420 static lxp_userdata *checkparser (lua_State *L, int idx) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 lxp_userdata *xpu = (lxp_userdata *)luaL_checkudata(L, idx, ParserType);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 luaL_argcheck(L, xpu, idx, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 luaL_argcheck(L, xpu->parser, idx, "parser is closed");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 return xpu;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 static int parser_gc (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 lxp_userdata *xpu = (lxp_userdata *)luaL_checkudata(L, 1, ParserType);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430 luaL_argcheck(L, xpu, 1, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431 lxpclose(L, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436 static int setbase (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 if (XML_SetBase(xpu->parser, luaL_checkstring(L, 2)) == 0)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439 luaL_error(L, "no memory to store base");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
442
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 static int getbase (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446 lua_pushstring(L, XML_GetBase(xpu->parser));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 static int getcallbacks (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458 static int parse_aux (lua_State *L, lxp_userdata *xpu, const char *s,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 size_t len) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 luaL_Buffer b;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 xpu->L = L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 xpu->state = XPSok;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 xpu->b = &b;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 lua_settop(L, 2);
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
466 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /*lua_getref(L, xpu->tableref);*/ /* to be used by handlers */
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 status = XML_Parse(xpu->parser, s, (int)len, s == NULL);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468 if (xpu->state == XPSstring) dischargestring(xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 if (xpu->state == XPSerror) { /* callback error? */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /* get original msg. */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
471 lua_error(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 if (s == NULL) xpu->state = XPSfinished;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 if (status) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 lua_pushboolean(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478 else { /* error */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 return reporterror(xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
480 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
481 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
482
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 static int lxp_parse (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486 size_t len;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487 const char *s = luaL_optlstring(L, 2, NULL, &len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488 if (xpu->state == XPSfinished && s != NULL) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
489 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
490 lua_pushliteral(L, "cannot parse - document is finished");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491 return 2;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493 return parse_aux(L, xpu, s, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
495
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 static int lxp_close (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 int status = 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499 lxp_userdata *xpu = (lxp_userdata *)luaL_checkudata(L, 1, ParserType);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500 luaL_argcheck(L, xpu, 1, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 if (xpu->state != XPSfinished)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 status = parse_aux(L, xpu, NULL, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503 lxpclose(L, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 if (status > 1) luaL_error(L, "error closing parser: %s",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 lua_tostring(L, -status+1));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
507 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
508
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
509
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
510 static int lxp_pos (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
511 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
512 XML_Parser p = xpu->parser;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
513 lua_pushnumber(L, XML_GetCurrentLineNumber(p));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
514 lua_pushnumber(L, XML_GetCurrentColumnNumber(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
515 lua_pushnumber(L, XML_GetCurrentByteIndex(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
516 return 3;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
517 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
518
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
519
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
520 static int lxp_setencoding (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522 const char *encoding = luaL_checkstring(L, 2);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523 luaL_argcheck(L, xpu->state == XPSpre, 1, "invalid parser state");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 XML_SetEncoding(xpu->parser, encoding);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
526 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
527
2
95f4883da11f Support for parser:stop() to abort processing (doesn't support suspend/resume yet for simplicity)
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
528 static int lxp_stop (lua_State *L) {
95f4883da11f Support for parser:stop() to abort processing (doesn't support suspend/resume yet for simplicity)
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
529 lxp_userdata *xpu = checkparser(L, 1);
95f4883da11f Support for parser:stop() to abort processing (doesn't support suspend/resume yet for simplicity)
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
530 lua_pushboolean(L, XML_StopParser(xpu->parser, XML_FALSE) == XML_STATUS_OK);
95f4883da11f Support for parser:stop() to abort processing (doesn't support suspend/resume yet for simplicity)
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
531 return 1;
95f4883da11f Support for parser:stop() to abort processing (doesn't support suspend/resume yet for simplicity)
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
532 }
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
533
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
534 #if !defined LUA_VERSION_NUM
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
535 /* Lua 5.0 */
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
536 #define luaL_Reg luaL_reg
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
537 #endif
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
538
17
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
539 static int lxp_getcurrentbytecount (lua_State* L) {
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
540 lxp_userdata *xpu = checkparser(L, 1);
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
541 lua_pushinteger(L, XML_GetCurrentByteCount(xpu->parser));
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
542 return 1;
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
543 }
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
544
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
545 static const struct luaL_Reg lxp_meths[] = {
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
546 {"parse", lxp_parse},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
547 {"close", lxp_close},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
548 {"__gc", parser_gc},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
549 {"pos", lxp_pos},
17
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
550 {"getcurrentbytecount", lxp_getcurrentbytecount},
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
551 {"setencoding", lxp_setencoding},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
552 {"getcallbacks", getcallbacks},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
553 {"getbase", getbase},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
554 {"setbase", setbase},
2
95f4883da11f Support for parser:stop() to abort processing (doesn't support suspend/resume yet for simplicity)
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
555 {"stop", lxp_stop},
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
556 {NULL, NULL}
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
557 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
558
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
559 static const struct luaL_Reg lxp_funcs[] = {
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
560 {"new", lxp_make_parser},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
561 {NULL, NULL}
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
562 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
563
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
564
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
565 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
566 ** Assumes the table is on top of the stack.
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
567 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
568 static void set_info (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
569 lua_pushliteral (L, "_COPYRIGHT");
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
570 lua_pushliteral (L, "Copyright (C) 2003-2012 Kepler Project");
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
571 lua_settable (L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
572 lua_pushliteral (L, "_DESCRIPTION");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
573 lua_pushliteral (L, "LuaExpat is a SAX XML parser based on the Expat library");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
574 lua_settable (L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
575 lua_pushliteral (L, "_VERSION");
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
576 lua_pushliteral (L, "LuaExpat 1.3.0");
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
577 lua_settable (L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
578 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
579
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
580
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
581 #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
582 /*
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
583 ** Adapted from Lua 5.2.0
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
584 */
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
585 static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
586 luaL_checkstack(L, nup, "too many upvalues");
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
587 for (; l->name != NULL; l++) { /* fill the table with given functions */
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
588 int i;
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
589 for (i = 0; i < nup; i++) /* copy upvalues to the top */
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
590 lua_pushvalue(L, -nup);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
591 lua_pushstring(L, l->name);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
592 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
593 lua_settable(L, -(nup + 3));
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
594 }
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
595 lua_pop(L, nup); /* remove upvalues */
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
596 }
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
597 #endif
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
598
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
599
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
600 int luaopen_lxp (lua_State *L) {
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
601 luaL_newmetatable(L, ParserType);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
602
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
603 lua_pushliteral(L, "__index");
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
604 lua_pushvalue(L, -2);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
605 lua_rawset(L, -3);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
606
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
607 luaL_setfuncs (L, lxp_meths, 0);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
608 lua_pop (L, 1); /* remove metatable */
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
609
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
610 lua_newtable (L);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
611 luaL_setfuncs (L, lxp_funcs, 0);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
612 set_info (L);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
613 return 1;
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
614 }

mercurial