src/lxplib.c

Fri, 23 Apr 2021 18:28:14 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 23 Apr 2021 18:28:14 +0100
changeset 31
ebb63b55bcc1
parent 26
a8caec6c5429
child 32
73adc42f35ce
permissions
-rw-r--r--

Use luaL_register on 5.1 to avoid reimplementing setfuncs

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
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
20 #if (LUA_VERSION_NUM == 501)
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
21 #define lua_getuservalue(L, i) lua_getfenv(L, i)
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
22 #define lua_setuservalue(L, i) lua_setfenv(L, i)
31
ebb63b55bcc1 Use luaL_register on 5.1 to avoid reimplementing setfuncs
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
23 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
24 #endif
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
26 #if !defined(lua_pushliteral)
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
27 #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
28 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
29 #endif
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
30
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 enum XPState {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 XPSpre, /* parser just initialized */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 XPSok, /* state while parsing */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 XPSfinished, /* state after finished parsing */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 XPSerror,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 XPSstring /* state while reading a string */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 struct lxp_userdata {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 lua_State *L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 XML_Parser parser; /* associated expat parser */
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
43 int errorref; /* reference to error message if state is XPSerror */
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 enum XPState state;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 luaL_Buffer *b; /* to concatenate sequences of cdata pieces */
19
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
46 int bufferCharData; /* whether to buffer cdata pieces */
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 typedef struct lxp_userdata lxp_userdata;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 static int reporterror (lxp_userdata *xpu) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 XML_Parser p = xpu->parser;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 lua_pushstring(L, XML_ErrorString(XML_GetErrorCode(p)));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 lua_pushnumber(L, XML_GetCurrentLineNumber(p));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 lua_pushnumber(L, XML_GetCurrentColumnNumber(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 lua_pushnumber(L, XML_GetCurrentByteIndex(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return 5;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 static lxp_userdata *createlxp (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 lxp_userdata *xpu = (lxp_userdata *)lua_newuserdata(L, sizeof(lxp_userdata));
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
66 xpu->errorref = LUA_REFNIL;
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 xpu->parser = NULL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 xpu->L = NULL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 xpu->state = XPSpre;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 luaL_getmetatable(L, ParserType);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 lua_setmetatable(L, -2);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return xpu;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 static void lxpclose (lua_State *L, lxp_userdata *xpu) {
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
77 luaL_unref(L, LUA_REGISTRYINDEX, xpu->errorref);
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
78 xpu->errorref = LUA_REFNIL;
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 if (xpu->parser)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 XML_ParserFree(xpu->parser);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 xpu->parser = NULL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 }
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 ** Auxiliary function to call a Lua handle
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 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
91 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 assert(xpu->state == XPSok);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 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
94 xpu->state = XPSerror;
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
95 xpu->errorref = luaL_ref(L, LUA_REGISTRYINDEX); /* error message */
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 }
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 ** 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
102 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 static void dischargestring (lxp_userdata *xpu) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 assert(xpu->state == XPSstring);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 xpu->state = XPSok;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 luaL_pushresult(xpu->b);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 }
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 ** 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
113 ** 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
114 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 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
116 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 if (xpu->state == XPSstring) dischargestring(xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 if (xpu->state == XPSerror)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 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
120 lua_pushstring(L, handle);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 lua_gettable(L, 3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 if (lua_toboolean(L, -1) == 0) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 if (!lua_isfunction(L, -1)) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 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
128 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 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
130 return 1;
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
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 ** Handles
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 ** =======================================================
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
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 static void f_StartCdata (void *ud) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 if (getHandle(xpu, StartCdataKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 docall(xpu, 0, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147
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 static void f_EndCdataKey (void *ud) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 if (getHandle(xpu, EndCdataKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 docall(xpu, 0, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 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
157 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if (xpu->state == XPSok) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 if (getHandle(xpu, CharDataKey) == 0) return; /* no handle */
19
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
160 if(xpu->bufferCharData != 0) {
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
161 xpu->state = XPSstring;
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
162 luaL_buffinit(xpu->L, xpu->b);
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
163 } else {
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
164 lua_pushlstring(xpu->L, s, len);
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
165 docall(xpu, 1, 0);
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
166 }
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 if (xpu->state == XPSstring)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 luaL_addlstring(xpu->b, s, len);
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 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
174 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 if (getHandle(xpu, CommentKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 lua_pushstring(xpu->L, data);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 docall(xpu, 1, 0);
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 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
182 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 if (getHandle(xpu, DefaultKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 lua_pushlstring(xpu->L, data, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 docall(xpu, 1, 0);
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 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
190 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 if (getHandle(xpu, DefaultExpandKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 lua_pushlstring(xpu->L, data, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 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
198 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 int lastspec = XML_GetSpecifiedAttributeCount(xpu->parser) / 2;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 int i = 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 if (getHandle(xpu, StartElementKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 lua_pushstring(L, name);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 lua_newtable(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 while (*attrs) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 if (i <= lastspec) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 lua_pushnumber(L, i++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 lua_pushstring(L, *attrs);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 lua_settable(L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 lua_pushstring(L, *attrs++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 lua_pushstring(L, *attrs++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 lua_settable(L, -3);
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 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
216 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 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
220 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 if (getHandle(xpu, EndElementKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 lua_pushstring(xpu->L, name);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 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
228 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 const char *publicId) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 lxp_userdata *xpu = (lxp_userdata *)XML_GetUserData(p);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 lxp_userdata *child;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 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
236 child = createlxp(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 child->parser = XML_ExternalEntityParserCreate(p, context, NULL);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 if (!child->parser)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 luaL_error(L, "XML_ParserCreate failed");
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
240 lua_getuservalue(L, 1);
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
241 lua_setuservalue(L, -2); /* 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
242 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 docall(xpu, 4, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 status = lua_toboolean(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 lxpclose(L, child);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 return status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 }
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 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
254 const char *uri) {
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 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 if (getHandle(xpu, StartNamespaceDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 lua_pushstring(L, prefix);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 lua_pushstring(L, uri);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 docall(xpu, 2, 0);
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 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
265 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 if (getHandle(xpu, EndNamespaceDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 lua_pushstring(xpu->L, prefix);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 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
273 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 const char *publicId) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 if (getHandle(xpu, NotationDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 lua_pushstring(L, notationName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 docall(xpu, 4, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285
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 static int f_NotStandalone (void *ud) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 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
292 docall(xpu, 0, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 status = lua_toboolean(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 return status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 }
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 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
300 const char *data) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 if (getHandle(xpu, ProcessingInstructionKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 lua_pushstring(L, target);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 lua_pushstring(L, data);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 docall(xpu, 2, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 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
311 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 const char *publicId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 const char *notationName) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 if (getHandle(xpu, UnparsedEntityDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 lua_pushstring(L, entityName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 lua_pushstring(L, notationName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 docall(xpu, 5, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325
1
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
326 static void f_StartDoctypeDecl (void *ud, const XML_Char *doctypeName,
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
327 const XML_Char *sysid,
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
328 const XML_Char *pubid,
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
329 int has_internal_subset) {
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
330 lxp_userdata *xpu = (lxp_userdata *)ud;
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
331 if (getHandle(xpu, StartDoctypeDeclKey) == 0) return; /* no handle */
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
332 lua_pushstring(xpu->L, doctypeName);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
333 lua_pushstring(xpu->L, sysid);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
334 lua_pushstring(xpu->L, pubid);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
335 lua_pushboolean(xpu->L, has_internal_subset);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
336 docall(xpu, 4, 0);
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
337 }
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
338
18
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
339 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
340 const XML_Char *encoding,
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
341 int standalone) {
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
342 lxp_userdata *xpu = (lxp_userdata *)ud;
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
343 if (getHandle(xpu, XmlDeclKey) == 0) return; /* no handle */
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
344 lua_pushstring(xpu->L, version);
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
345 lua_pushstring(xpu->L, encoding);
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
346 lua_pushboolean(xpu->L, standalone);
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
347 docall(xpu, 3, 0);
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
348 }
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 /* }====================================================== */
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 int hasfield (lua_State *L, const char *fname) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 int res;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 lua_pushstring(L, fname);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 lua_gettable(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 res = !lua_isnil(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 return res;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 static void checkcallbacks (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 static const char *const validkeys[] = {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 "StartCdataSection", "EndCdataSection", "CharacterData", "Comment",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 "Default", "DefaultExpand", "StartElement", "EndElement",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 "ExternalEntityRef", "StartNamespaceDecl", "EndNamespaceDecl",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 "NotationDecl", "NotStandalone", "ProcessingInstruction",
18
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
369 "UnparsedEntityDecl", "StartDoctypeDecl", "XmlDecl", NULL};
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 if (hasfield(L, "_nonstrict")) return;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 while (lua_next(L, 1)) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 lua_pop(L, 1); /* remove value */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 #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
375 if (lua_type(L, -1) != LUA_TSTRING ||
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 luaL_findstring(lua_tostring(L, -1), validkeys) < 0)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 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
378 #else
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 luaL_checkoption(L, -1, NULL, validkeys);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 #endif
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 static int lxp_make_parser (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 XML_Parser p;
19
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
387 int bufferCharData = (lua_type(L, 3) != LUA_TBOOLEAN) || (lua_toboolean(L, 3) != 0);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 char sep = *luaL_optstring(L, 2, "");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 lxp_userdata *xpu = createlxp(L);
19
10c30b63873a Make merging of CharacterData events optional, controlled by the 3rd parameter of lxp.new()
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
390 xpu->bufferCharData = bufferCharData;
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) :
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 XML_ParserCreateNS(NULL, sep);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 if (!p)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 luaL_error(L, "XML_ParserCreate failed");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 luaL_checktype(L, 1, LUA_TTABLE);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 checkcallbacks(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 lua_pushvalue(L, 1);
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
398 lua_setuservalue(L, -2);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 XML_SetUserData(p, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 if (hasfield(L, StartCdataKey) || hasfield(L, EndCdataKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 XML_SetCdataSectionHandler(p, f_StartCdata, f_EndCdataKey);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 if (hasfield(L, CharDataKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 XML_SetCharacterDataHandler(p, f_CharData);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 if (hasfield(L, CommentKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 XML_SetCommentHandler(p, f_Comment);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 if (hasfield(L, DefaultKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 XML_SetDefaultHandler(p, f_Default);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 if (hasfield(L, DefaultExpandKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 XML_SetDefaultHandlerExpand(p, f_DefaultExpand);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 if (hasfield(L, StartElementKey) || hasfield(L, EndElementKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 XML_SetElementHandler(p, f_StartElement, f_EndElement);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 if (hasfield(L, ExternalEntityKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413 XML_SetExternalEntityRefHandler(p, f_ExternaEntity);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414 if (hasfield(L, StartNamespaceDeclKey) || hasfield(L, EndNamespaceDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 XML_SetNamespaceDeclHandler(p, f_StartNamespaceDecl, f_EndNamespaceDecl);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 if (hasfield(L, NotationDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 XML_SetNotationDeclHandler(p, f_NotationDecl);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 if (hasfield(L, NotStandaloneKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419 XML_SetNotStandaloneHandler(p, f_NotStandalone);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420 if (hasfield(L, ProcessingInstructionKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 XML_SetProcessingInstructionHandler(p, f_ProcessingInstruction);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 if (hasfield(L, UnparsedEntityDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl);
1
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
424 if (hasfield(L, StartDoctypeDeclKey))
304b5a6f85e4 Support for StartDoctypeDecl handler
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
425 XML_SetStartDoctypeDeclHandler(p, f_StartDoctypeDecl);
18
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
426 if (hasfield(L, XmlDeclKey))
90da47758f85 Add support for XmlDecl handlers
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
427 XML_SetXmlDeclHandler(p, f_XmlDecl);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 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
433 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
434 luaL_argcheck(L, xpu, idx, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435 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
436 return xpu;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 static int parser_gc (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 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
442 luaL_argcheck(L, xpu, 1, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443 lxpclose(L, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448 static int setbase (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 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
451 luaL_error(L, "no memory to store base");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454
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 static int getbase (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458 lua_pushstring(L, XML_GetBase(xpu->parser));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 static int getcallbacks (lua_State *L) {
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
464 checkparser(L, 1);
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
465 lua_getuservalue(L, 1);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 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
471 size_t len) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 luaL_Buffer b;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 xpu->L = L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 xpu->state = XPSok;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 xpu->b = &b;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 lua_settop(L, 2);
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
478 getcallbacks(L);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 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
480 if (xpu->state == XPSstring) dischargestring(xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
481 if (xpu->state == XPSerror) { /* callback error? */
26
a8caec6c5429 Keep callbacks in an uservalue instead of by reference
Kim Alvefur <zash@zash.se>
parents: 19
diff changeset
482 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->errorref); /* get original msg. */
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483 lua_error(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 if (s == NULL) xpu->state = XPSfinished;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486 if (status) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487 lua_pushboolean(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
489 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
490 else { /* error */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491 return reporterror(xpu);
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 }
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 static int lxp_parse (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 size_t len;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499 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
500 if (xpu->state == XPSfinished && s != NULL) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 lua_pushliteral(L, "cannot parse - document is finished");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503 return 2;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 return parse_aux(L, xpu, s, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 }
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 static int lxp_close (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
510 int status = 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
511 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
512 luaL_argcheck(L, xpu, 1, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
513 if (xpu->state != XPSfinished)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
514 status = parse_aux(L, xpu, NULL, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
515 lxpclose(L, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
516 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
517 lua_tostring(L, -status+1));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
518 return 0;
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522 static int lxp_pos (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 XML_Parser p = xpu->parser;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 lua_pushnumber(L, XML_GetCurrentLineNumber(p));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
526 lua_pushnumber(L, XML_GetCurrentColumnNumber(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
527 lua_pushnumber(L, XML_GetCurrentByteIndex(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
528 return 3;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
529 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
530
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
531
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
532 static int lxp_setencoding (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
533 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
534 const char *encoding = luaL_checkstring(L, 2);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
535 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
536 XML_SetEncoding(xpu->parser, encoding);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
537 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
538 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
539
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
540 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
541 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
542 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
543 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
544 }
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
545
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
546 #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
547 /* Lua 5.0 */
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
548 #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
549 #endif
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
550
17
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
551 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
552 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
553 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
554 return 1;
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
555 }
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
556
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
557 static const struct luaL_Reg lxp_meths[] = {
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
558 {"parse", lxp_parse},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
559 {"close", lxp_close},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
560 {"__gc", parser_gc},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
561 {"pos", lxp_pos},
17
7f0e6f8bb8e0 Expose expat's XML_GetCurrentByteCount() method as parser:getcurrentbytecount()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
562 {"getcurrentbytecount", lxp_getcurrentbytecount},
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
563 {"setencoding", lxp_setencoding},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
564 {"getcallbacks", getcallbacks},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
565 {"getbase", getbase},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
566 {"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
567 {"stop", lxp_stop},
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
568 {NULL, NULL}
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
569 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
570
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
571 static const struct luaL_Reg lxp_funcs[] = {
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
572 {"new", lxp_make_parser},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
573 {NULL, NULL}
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
574 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
575
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
576
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
577 /*
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
578 ** 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
579 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
580 static void set_info (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
581 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
582 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
583 lua_settable (L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
584 lua_pushliteral (L, "_DESCRIPTION");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
585 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
586 lua_settable (L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
587 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
588 lua_pushliteral (L, "LuaExpat 1.3.0");
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
589 lua_settable (L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
590 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
591
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
592 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
593 luaL_newmetatable(L, ParserType);
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_pushliteral(L, "__index");
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
596 lua_pushvalue(L, -2);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
597 lua_rawset(L, -3);
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
598
10
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
599 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
600 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
601
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
602 lua_newtable (L);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
603 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
604 set_info (L);
e981a82571cf Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents: 6
diff changeset
605 return 1;
0
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
606 }

mercurial