00001 #include "lugre_prefix.h"
00002 #include "lugre_luaxml.h"
00003 #include "tinyxml.h"
00004 #include <string>
00005
00006
00007 namespace Lugre {
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 extern "C" {
00047 #include "lua.h"
00048 #include "lauxlib.h"
00049 #include "lualib.h"
00050 }
00051
00070 void LuaXML_FillNode (lua_State *L,int iTableIndex,TiXmlNode* pParent) { PROFILE
00071
00072 luaL_checkstack(L, 5, "LuaXML_ParseNode : recursion too deep");
00073 if (!lua_istable(L,iTableIndex)) { printf("LuaXML_FillNode : no table\n"); return; }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 lua_getfield(L,iTableIndex,"name");
00089
00090 std::string sName = lua_tostring(L,-1);
00091
00092 lua_pop(L,1);
00093
00094 TiXmlElement* pElem = new TiXmlElement(sName.c_str());
00095
00096
00097 lua_getfield(L,iTableIndex,"attr");
00098
00099 if (lua_istable(L,-1)) {
00100 lua_pushnil(L);
00101 while (lua_next(L,-2) != 0) {
00102
00103
00104
00105
00106 std::string sAttrName = lua_tostring(L,-2);
00107
00108 std::string sAttrValue = lua_tostring(L,-1);
00109
00110 pElem->SetAttribute(sAttrName.c_str(),sAttrValue.c_str());
00111 lua_pop(L, 1);
00112 }
00113 }
00114
00115 lua_pop(L,1);
00116
00117
00118
00119 lua_getfield(L,iTableIndex,"n");
00120 int n = (int)lua_tonumber(L,-1);
00121
00122 lua_pop(L,1);
00123
00124
00125 for (int i=1;i<=n;++i) {
00126 lua_rawgeti(L,iTableIndex,i);
00127
00128
00129 if (lua_isstring(L,-1)) {
00130
00131 std::string sText = lua_tostring(L,-1);
00132
00133 pElem->LinkEndChild( new TiXmlText( sText.c_str() ) );
00134 }
00135 if (lua_istable(L,-1)) {
00136
00137 LuaXML_FillNode(L,-1,pElem);
00138
00139 }
00140
00141 lua_pop(L,1);
00142 }
00143
00144 pParent->LinkEndChild( pElem );
00145 }
00146
00147 void LuaXML_ParseNode (lua_State *L,TiXmlNode* pNode) { PROFILE
00148 if (!pNode) return;
00149
00150 luaL_checkstack(L, 5, "LuaXML_ParseNode : recursion too deep");
00151
00152 TiXmlElement* pElem = pNode->ToElement();
00153 if (pElem) {
00154
00155 lua_pushstring(L,"name");
00156 lua_pushstring(L,pElem->Value());
00157 lua_settable(L,-3);
00158
00159
00160
00161 TiXmlAttribute* pAttr = pElem->FirstAttribute();
00162 if (pAttr) {
00163 lua_pushstring(L,"attr");
00164 lua_newtable(L);
00165 for (;pAttr;pAttr = pAttr->Next()) {
00166 lua_pushstring(L,pAttr->Name());
00167 lua_pushstring(L,pAttr->Value());
00168
00169 lua_settable(L,-3);
00170
00171 }
00172
00173 lua_settable(L,-3);
00174 }
00175 }
00176
00177
00178 TiXmlNode *pChild = pNode->FirstChild();
00179 if (pChild) {
00180 int iChildCount = 0;
00181 for(;pChild;pChild = pChild->NextSibling()) {
00182 switch (pChild->Type()) {
00183 case TiXmlNode::DOCUMENT: break;
00184 case TiXmlNode::ELEMENT:
00185
00186 lua_newtable(L);
00187 LuaXML_ParseNode(L,pChild);
00188 lua_rawseti(L,-2,++iChildCount);
00189 break;
00190 case TiXmlNode::COMMENT: break;
00191 case TiXmlNode::TEXT:
00192
00193 lua_pushstring(L,pChild->Value());
00194 lua_rawseti(L,-2,++iChildCount);
00195 break;
00196 case TiXmlNode::DECLARATION: break;
00197 case TiXmlNode::UNKNOWN: break;
00198 };
00199 }
00200 lua_pushstring(L,"n");
00201 lua_pushnumber(L,iChildCount);
00202
00203 lua_settable(L,-3);
00204 }
00205 }
00206
00207 static int LuaXML_ParseFile (lua_State *L) { PROFILE
00208 const char* sFileName = luaL_checkstring(L,1);
00209 TiXmlDocument doc(sFileName);
00210 doc.LoadFile();
00211 lua_newtable(L);
00212 LuaXML_ParseNode(L,&doc);
00213 return 1;
00214 }
00215
00216 static int LuaXML_ParseString (lua_State *L) { PROFILE
00217 const char* sString = luaL_checkstring(L,1);
00218 TiXmlDocument doc;
00219 doc.Parse(sString,0,TIXML_DEFAULT_ENCODING);
00220 lua_newtable(L);
00221 LuaXML_ParseNode(L,&doc);
00222 return 1;
00223 }
00224
00226 static int LuaXML_SaveFile (lua_State *L) { PROFILE
00227 std::string sFileName = luaL_checkstring(L,1);
00228 TiXmlDocument doc;
00229 LuaXML_FillNode(L,2,&doc);
00230 doc.SaveFile(sFileName.c_str());
00231 return 1;
00232 }
00233
00234 void RegisterLuaXML (lua_State *L) {
00235 lua_register(L,"LuaXML_ParseFile",LuaXML_ParseFile);
00236 lua_register(L,"LuaXML_ParseString",LuaXML_ParseString);
00237 lua_register(L,"LuaXML_SaveFile",LuaXML_SaveFile);
00238 }
00239
00240 };