1
0
Fork 0

Updated luaexpat

This commit is contained in:
Tiger Wang 2015-05-09 00:00:13 +01:00
parent abd5708e5c
commit 51a118f1ef
2 changed files with 31 additions and 7 deletions

View File

@ -38,6 +38,7 @@ struct lxp_userdata {
int tableref; /* table with callbacks for this parser */
enum XPState state;
luaL_Buffer *b; /* to concatenate sequences of cdata pieces */
int bufferCharData; /* whether to buffer cdata pieces */
};
typedef struct lxp_userdata lxp_userdata;
@ -152,8 +153,13 @@ static void f_CharData (void *ud, const char *s, int len) {
lxp_userdata *xpu = (lxp_userdata *)ud;
if (xpu->state == XPSok) {
if (getHandle(xpu, CharDataKey) == 0) return; /* no handle */
xpu->state = XPSstring;
luaL_buffinit(xpu->L, xpu->b);
if(xpu->bufferCharData != 0) {
xpu->state = XPSstring;
luaL_buffinit(xpu->L, xpu->b);
} else {
lua_pushlstring(xpu->L, s, len);
docall(xpu, 1, 0);
}
}
if (xpu->state == XPSstring)
luaL_addlstring(xpu->b, s, len);
@ -326,6 +332,16 @@ static void f_StartDoctypeDecl (void *ud, const XML_Char *doctypeName,
docall(xpu, 4, 0);
}
static void f_XmlDecl (void *ud, const XML_Char *version,
const XML_Char *encoding,
int standalone) {
lxp_userdata *xpu = (lxp_userdata *)ud;
if (getHandle(xpu, XmlDeclKey) == 0) return; /* no handle */
lua_pushstring(xpu->L, version);
lua_pushstring(xpu->L, encoding);
lua_pushboolean(xpu->L, standalone);
docall(xpu, 3, 0);
}
/* }====================================================== */
@ -346,7 +362,7 @@ static void checkcallbacks (lua_State *L) {
"Default", "DefaultExpand", "StartElement", "EndElement",
"ExternalEntityRef", "StartNamespaceDecl", "EndNamespaceDecl",
"NotationDecl", "NotStandalone", "ProcessingInstruction",
"UnparsedEntityDecl", "StartDoctypeDecl", NULL};
"UnparsedEntityDecl", "StartDoctypeDecl", "XmlDecl", NULL};
if (hasfield(L, "_nonstrict")) return;
lua_pushnil(L);
while (lua_next(L, 1)) {
@ -364,8 +380,10 @@ static void checkcallbacks (lua_State *L) {
static int lxp_make_parser (lua_State *L) {
XML_Parser p;
int bufferCharData = (lua_type(L, 3) != LUA_TBOOLEAN) || (lua_toboolean(L, 3) != 0);
char sep = *luaL_optstring(L, 2, "");
lxp_userdata *xpu = createlxp(L);
xpu->bufferCharData = bufferCharData;
p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) :
XML_ParserCreateNS(NULL, sep);
if (!p)
@ -401,6 +419,8 @@ static int lxp_make_parser (lua_State *L) {
XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl);
if (hasfield(L, StartDoctypeDeclKey))
XML_SetStartDoctypeDeclHandler(p, f_StartDoctypeDecl);
if (hasfield(L, XmlDeclKey))
XML_SetXmlDeclHandler(p, f_XmlDecl);
return 1;
}
@ -524,11 +544,18 @@ static int lxp_stop (lua_State *L) {
#define luaL_Reg luaL_reg
#endif
static int lxp_getcurrentbytecount (lua_State* L) {
lxp_userdata *xpu = checkparser(L, 1);
lua_pushinteger(L, XML_GetCurrentByteCount(xpu->parser));
return 1;
}
static const struct luaL_Reg lxp_meths[] = {
{"parse", lxp_parse},
{"close", lxp_close},
{"__gc", parser_gc},
{"pos", lxp_pos},
{"getcurrentbytecount", lxp_getcurrentbytecount},
{"setencoding", lxp_setencoding},
{"getcallbacks", getcallbacks},
{"getbase", getbase},
@ -588,12 +615,8 @@ int luaopen_lxp (lua_State *L) {
luaL_setfuncs (L, lxp_meths, 0);
lua_pop (L, 1); /* remove metatable */
// _X 2013_04_09: Modified to allow embedding
luaL_openlib (L, "lxp", lxp_funcs, 0);
/*
lua_newtable (L);
luaL_setfuncs (L, lxp_funcs, 0);
*/
set_info (L);
return 1;
}

View File

@ -20,5 +20,6 @@
#define ProcessingInstructionKey "ProcessingInstruction"
#define UnparsedEntityDeclKey "UnparsedEntityDecl"
#define StartDoctypeDeclKey "StartDoctypeDecl"
#define XmlDeclKey "XmlDecl"
int luaopen_lxp (lua_State *L);