From 2b123922d8804ccc99d8e4ae119a230d36faefb7 Mon Sep 17 00:00:00 2001 From: pixel Date: Tue, 27 Mar 2007 17:04:28 +0000 Subject: Improving LuaXML a bit. --- lib/LuaXML.cc | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/LuaXML.cc b/lib/LuaXML.cc index 606870e..93a3fb7 100644 --- a/lib/LuaXML.cc +++ b/lib/LuaXML.cc @@ -1,8 +1,9 @@ #include "tinyxml.h" +#include #include -void LuaXML_ParseNode (lua_State *L,TiXmlNode* pNode) { PROFILE +void LuaXML_ParseNode (lua_State *L,TiXmlNode* pNode) { if (!pNode) return; // resize stack if neccessary luaL_checkstack(L, 5, "LuaXML_ParseNode : recursion too deep"); @@ -58,7 +59,7 @@ void LuaXML_ParseNode (lua_State *L,TiXmlNode* pNode) { PROFILE } } -static int LuaXML_ParseFile (lua_State *L) { PROFILE +static int LuaXML_ParseFile (lua_State *L) { const char* sFileName = luaL_checkstring(L,1); TiXmlDocument doc(sFileName); doc.LoadFile(); @@ -67,6 +68,40 @@ static int LuaXML_ParseFile (lua_State *L) { PROFILE return 1; } -void RegisterLuaXML (lua_State *L) { - lua_register(L,"LuaXML_ParseFile",LuaXML_ParseFile); +static int LuaXML_ParseString(lua_State *L) { + const char * xml_string = luaL_checkstring(L, 1); + TiXmlDocument doc; + doc.Parse(xml_string); + lua_newtable(L); + LuaXML_ParseNode(L, &doc); +} + +#define XMLBUFSIZ 81920 + +static int LuaXML_ParseHandle(lua_State *__L) { + Lua * L = Lua::find(__L); + Handle * h = (Handle *) LuaObject::getme(L, 1); + TiXmlDocument doc; + char buffer[XMLBUFSIZ + 1]; + int l; + + while (!h->IsClosed()) { + l = h->read(buffer, XMLBUFSIZ); + buffer[l] = 0; + doc.Parse(buffer); + } + + lua_newtable(__L); + LuaXML_ParseNode(__L, &doc); +} + +static const luaL_reg xmllib[] = { + { "LoadString", LuaXML_ParseString }, + { "LoadHandle", LuaXML_ParseHandle }, + { NULL, NULL } +}; + +int luaopen_xml(Lua *L) { + L->openlib("xml", xmllib, 0); + return 1; } -- cgit v1.2.3