summaryrefslogtreecommitdiff
path: root/lib/LuaXML.cc
blob: 4f9c6bab2451549f041e8180917dff8d93460d76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "tinyxml.h"

#include <Handle.h>
#include <BLua.h>

void LuaXML_ParseNode (lua_State *L,TiXmlNode* pNode) {
    if (!pNode) return;
    // resize stack if neccessary
    luaL_checkstack(L, 5, "LuaXML_ParseNode : recursion too deep");
    
    TiXmlElement* pElem = pNode->ToElement();
    if (pElem) {
        // element name
        lua_pushstring(L,"name");
        lua_pushstring(L,pElem->Value());
        lua_settable(L,-3);
        
        // parse attributes
        TiXmlAttribute* pAttr = pElem->FirstAttribute();
        if (pAttr) {
            lua_pushstring(L,"attr");
            lua_newtable(L);
            for (;pAttr;pAttr = pAttr->Next()) {
                lua_pushstring(L,pAttr->Name());
                lua_pushstring(L,pAttr->Value());
                lua_settable(L,-3);
                
            }
            lua_settable(L,-3);
        }
    }
    
    // children
    TiXmlNode *pChild = pNode->FirstChild();
    if (pChild) {
        int iChildCount = 0;
        for(;pChild;pChild = pChild->NextSibling()) {
            switch (pChild->Type()) {
                case TiXmlNode::DOCUMENT: break;
                case TiXmlNode::ELEMENT: 
                    // normal element, parse recursive
                    lua_newtable(L);
                    LuaXML_ParseNode(L,pChild);
                    lua_rawseti(L,-2,++iChildCount);
                break;
                case TiXmlNode::COMMENT: break;
                case TiXmlNode::TEXT: 
                    // plaintext, push raw
                    lua_pushstring(L,pChild->Value());
                    lua_rawseti(L,-2,++iChildCount);
                break;
                case TiXmlNode::DECLARATION: break;
                case TiXmlNode::UNKNOWN: break;
            };
        }
        lua_pushstring(L,"n");
        lua_pushnumber(L,iChildCount);
        lua_settable(L,-3);
    }
}

static int LuaXML_ParseFile (lua_State *L) {
    const char* sFileName = luaL_checkstring(L,1);
    TiXmlDocument doc(sFileName);
    doc.LoadFile();
    lua_newtable(L);
    LuaXML_ParseNode(L,&doc);
    return 1;
}

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);
    return 1;
}

#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);
        if (l) {
            buffer[l] = 0;
            doc.Parse(buffer);
        }
    }
    
    lua_newtable(__L);
    LuaXML_ParseNode(__L, &doc);
    return 1;
}

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;
}