src/lxplib.c

Wed, 01 Jun 2011 22:14:25 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 01 Jun 2011 22:14:25 +0100
changeset 0
24d141cb2d1e
child 1
304b5a6f85e4
permissions
-rw-r--r--

Initial commit of LuaExpat 1.1.0

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 #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
17 #include "compat-5.1.h"
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 #endif
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 #include "lxplib.h"
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 enum XPState {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 XPSpre, /* parser just initialized */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 XPSok, /* state while parsing */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 XPSfinished, /* state after finished parsing */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 XPSerror,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 XPSstring /* state while reading a string */
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 struct lxp_userdata {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 lua_State *L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 XML_Parser parser; /* associated expat parser */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 int tableref; /* table with callbacks for this parser */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 enum XPState state;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 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
39 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 typedef struct lxp_userdata lxp_userdata;
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 static int reporterror (lxp_userdata *xpu) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 XML_Parser p = xpu->parser;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 lua_pushstring(L, XML_ErrorString(XML_GetErrorCode(p)));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 lua_pushnumber(L, XML_GetCurrentLineNumber(p));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 lua_pushnumber(L, XML_GetCurrentColumnNumber(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 lua_pushnumber(L, XML_GetCurrentByteIndex(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return 5;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
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 static lxp_userdata *createlxp (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 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
58 xpu->tableref = LUA_REFNIL; /* in case of errors... */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 xpu->parser = NULL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 xpu->L = NULL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 xpu->state = XPSpre;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 luaL_getmetatable(L, ParserType);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 lua_setmetatable(L, -2);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 return xpu;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
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 static void lxpclose (lua_State *L, lxp_userdata *xpu) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 lua_unref(L, xpu->tableref);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 xpu->tableref = LUA_REFNIL;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if (xpu->parser)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 XML_ParserFree(xpu->parser);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 xpu->parser = NULL;
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
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 ** Auxiliary function to call a Lua handle
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 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
83 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 assert(xpu->state == XPSok);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 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
86 xpu->state = XPSerror;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 luaL_unref(L, LUA_REGISTRYINDEX, xpu->tableref);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 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
89 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 }
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 ** 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
95 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 static void dischargestring (lxp_userdata *xpu) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 assert(xpu->state == XPSstring);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 xpu->state = XPSok;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 luaL_pushresult(xpu->b);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 }
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
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 ** 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
106 ** 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
107 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 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
109 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 if (xpu->state == XPSstring) dischargestring(xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 if (xpu->state == XPSerror)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 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
113 lua_pushstring(L, handle);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 lua_gettable(L, 3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 if (lua_toboolean(L, -1) == 0) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 if (!lua_isfunction(L, -1)) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 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
121 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 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
123 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 }
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
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 ** Handles
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 static void f_StartCdata (void *ud) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 if (getHandle(xpu, StartCdataKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 docall(xpu, 0, 0);
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_EndCdataKey (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, EndCdataKey) == 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_CharData (void *ud, const char *s, int len) {
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 (xpu->state == XPSok) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 if (getHandle(xpu, CharDataKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 xpu->state = XPSstring;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 luaL_buffinit(xpu->L, xpu->b);
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 if (xpu->state == XPSstring)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 luaL_addlstring(xpu->b, s, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159
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 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
162 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 if (getHandle(xpu, CommentKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 lua_pushstring(xpu->L, data);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 }
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 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
170 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 if (getHandle(xpu, DefaultKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 lua_pushlstring(xpu->L, data, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
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 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
178 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 if (getHandle(xpu, DefaultExpandKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 lua_pushlstring(xpu->L, data, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
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 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
186 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 int lastspec = XML_GetSpecifiedAttributeCount(xpu->parser) / 2;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 int i = 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 if (getHandle(xpu, StartElementKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 lua_pushstring(L, name);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 lua_newtable(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 while (*attrs) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 if (i <= lastspec) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 lua_pushnumber(L, i++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 lua_pushstring(L, *attrs);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 lua_settable(L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 lua_pushstring(L, *attrs++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 lua_pushstring(L, *attrs++);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 lua_settable(L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 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
204 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
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 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
208 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 if (getHandle(xpu, EndElementKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 lua_pushstring(xpu->L, name);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213
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 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
216 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 const char *publicId) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 lxp_userdata *xpu = (lxp_userdata *)XML_GetUserData(p);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 lxp_userdata *child;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 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
224 child = createlxp(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 child->parser = XML_ExternalEntityParserCreate(p, context, NULL);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 if (!child->parser)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 luaL_error(L, "XML_ParserCreate failed");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 lua_getref(L, xpu->tableref); /* child uses the same table of its father */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 child->tableref = luaL_ref(L, LUA_REGISTRYINDEX);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 docall(xpu, 4, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 status = lua_toboolean(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 lxpclose(L, child);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 return status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239
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 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
242 const char *uri) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 if (getHandle(xpu, StartNamespaceDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 lua_pushstring(L, prefix);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 lua_pushstring(L, uri);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 docall(xpu, 2, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 }
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 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
253 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 if (getHandle(xpu, EndNamespaceDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 lua_pushstring(xpu->L, prefix);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 docall(xpu, 1, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258
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 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
261 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 const char *publicId) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 if (getHandle(xpu, NotationDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 lua_pushstring(L, notationName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 docall(xpu, 4, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273
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 static int f_NotStandalone (void *ud) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 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
280 docall(xpu, 0, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 status = lua_toboolean(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 return status;
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 void f_ProcessingInstruction (void *ud, const char *target,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 const char *data) {
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, ProcessingInstructionKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 lua_pushstring(L, target);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 lua_pushstring(L, data);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 docall(xpu, 2, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 }
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 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
299 const char *base,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 const char *systemId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 const char *publicId,
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 const char *notationName) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 lxp_userdata *xpu = (lxp_userdata *)ud;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 lua_State *L = xpu->L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 if (getHandle(xpu, UnparsedEntityDeclKey) == 0) return; /* no handle */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 lua_pushstring(L, entityName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 lua_pushstring(L, base);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 lua_pushstring(L, systemId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 lua_pushstring(L, publicId);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 lua_pushstring(L, notationName);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 docall(xpu, 5, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 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
319 int res;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 lua_pushstring(L, fname);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 lua_gettable(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 res = !lua_isnil(L, -1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 lua_pop(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 return res;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 static void checkcallbacks (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 static const char *const validkeys[] = {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 "StartCdataSection", "EndCdataSection", "CharacterData", "Comment",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 "Default", "DefaultExpand", "StartElement", "EndElement",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 "ExternalEntityRef", "StartNamespaceDecl", "EndNamespaceDecl",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 "NotationDecl", "NotStandalone", "ProcessingInstruction",
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 "UnparsedEntityDecl", NULL};
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 if (hasfield(L, "_nonstrict")) return;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 while (lua_next(L, 1)) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 lua_pop(L, 1); /* remove value */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 #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
340 if (lua_type(L, -1) != LUA_TSTRING ||
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 luaL_findstring(lua_tostring(L, -1), validkeys) < 0)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 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
343 #else
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 luaL_checkoption(L, -1, NULL, validkeys);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 #endif
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348
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 static int lxp_make_parser (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 XML_Parser p;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 char sep = *luaL_optstring(L, 2, "");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 lxp_userdata *xpu = createlxp(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) :
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 XML_ParserCreateNS(NULL, sep);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 if (!p)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 luaL_error(L, "XML_ParserCreate failed");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 luaL_checktype(L, 1, LUA_TTABLE);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 checkcallbacks(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 lua_pushvalue(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 xpu->tableref = luaL_ref(L, LUA_REGISTRYINDEX);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 XML_SetUserData(p, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 if (hasfield(L, StartCdataKey) || hasfield(L, EndCdataKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 XML_SetCdataSectionHandler(p, f_StartCdata, f_EndCdataKey);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 if (hasfield(L, CharDataKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 XML_SetCharacterDataHandler(p, f_CharData);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 if (hasfield(L, CommentKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 XML_SetCommentHandler(p, f_Comment);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 if (hasfield(L, DefaultKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 XML_SetDefaultHandler(p, f_Default);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 if (hasfield(L, DefaultExpandKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 XML_SetDefaultHandlerExpand(p, f_DefaultExpand);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 if (hasfield(L, StartElementKey) || hasfield(L, EndElementKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 XML_SetElementHandler(p, f_StartElement, f_EndElement);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 if (hasfield(L, ExternalEntityKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 XML_SetExternalEntityRefHandler(p, f_ExternaEntity);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 if (hasfield(L, StartNamespaceDeclKey) || hasfield(L, EndNamespaceDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 XML_SetNamespaceDeclHandler(p, f_StartNamespaceDecl, f_EndNamespaceDecl);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 if (hasfield(L, NotationDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 XML_SetNotationDeclHandler(p, f_NotationDecl);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 if (hasfield(L, NotStandaloneKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 XML_SetNotStandaloneHandler(p, f_NotStandalone);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 if (hasfield(L, ProcessingInstructionKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 XML_SetProcessingInstructionHandler(p, f_ProcessingInstruction);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 if (hasfield(L, UnparsedEntityDeclKey))
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 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
392 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
393 luaL_argcheck(L, xpu, idx, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 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
395 return xpu;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 static int parser_gc (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 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
401 luaL_argcheck(L, xpu, 1, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 lxpclose(L, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 static int setbase (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 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
410 luaL_error(L, "no memory to store base");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 static int getbase (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 lua_pushstring(L, XML_GetBase(xpu->parser));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 return 1;
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 static int getcallbacks (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 return 1;
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 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
430 size_t len) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431 luaL_Buffer b;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 int status;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433 xpu->L = L;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434 xpu->state = XPSok;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435 xpu->b = &b;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436 lua_settop(L, 2);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 lua_getref(L, xpu->tableref); /* to be used by handlers */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 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
439 if (xpu->state == XPSstring) dischargestring(xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 if (xpu->state == XPSerror) { /* callback error? */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 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
442 lua_error(L);
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 if (s == NULL) xpu->state = XPSfinished;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 if (status) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446 lua_pushboolean(L, 1);
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 else { /* error */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 return reporterror(xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 }
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 static int lxp_parse (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 size_t len;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458 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
459 if (xpu->state == XPSfinished && s != NULL) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 lua_pushnil(L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 lua_pushliteral(L, "cannot parse - document is finished");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 return 2;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 return parse_aux(L, xpu, s, len);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466
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 static int lxp_close (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 int status = 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 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
471 luaL_argcheck(L, xpu, 1, "expat parser expected");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 if (xpu->state != XPSfinished)
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 status = parse_aux(L, xpu, NULL, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 lxpclose(L, xpu);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 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
476 lua_tostring(L, -status+1));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479
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 static int lxp_pos (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
482 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483 XML_Parser p = xpu->parser;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 lua_pushnumber(L, XML_GetCurrentLineNumber(p));
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 lua_pushnumber(L, XML_GetCurrentColumnNumber(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486 lua_pushnumber(L, XML_GetCurrentByteIndex(p) + 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487 return 3;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488 }
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491 static int lxp_setencoding (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 lxp_userdata *xpu = checkparser(L, 1);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493 const char *encoding = luaL_checkstring(L, 2);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 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
495 XML_SetEncoding(xpu->parser, encoding);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 return 0;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 }
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500 static const struct luaL_reg lxp_meths[] = {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 {"parse", lxp_parse},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 {"close", lxp_close},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503 {"__gc", parser_gc},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 {"pos", lxp_pos},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 {"setencoding", lxp_setencoding},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 {"getcallbacks", getcallbacks},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
507 {"getbase", getbase},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
508 {"setbase", setbase},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
509 {NULL, NULL}
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
510 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
511
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
512 static const struct luaL_reg lxp_funcs[] = {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
513 {"new", lxp_make_parser},
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
514 {NULL, NULL}
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
515 };
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
516
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 ** 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
520 */
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521 static void set_info (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522 lua_pushliteral (L, "_COPYRIGHT");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523 lua_pushliteral (L, "Copyright (C) 2003-2007 Kepler Project");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 lua_settable (L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 lua_pushliteral (L, "_DESCRIPTION");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
526 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
527 lua_settable (L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
528 lua_pushliteral (L, "_VERSION");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
529 lua_pushliteral (L, "LuaExpat 1.1.0");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
530 lua_settable (L, -3);
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
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
533
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
534 int luaopen_lxp (lua_State *L) {
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
535 luaL_newmetatable(L, ParserType);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
536 lua_pushliteral(L, "__index");
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
537 lua_pushvalue(L, -2);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
538 lua_rawset(L, -3);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
539 luaL_openlib (L, NULL, lxp_meths, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
540 luaL_openlib (L, "lxp", lxp_funcs, 0);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
541 set_info (L);
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
542
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
543 return 1;
24d141cb2d1e Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
544 }

mercurial