bool LUAXML::LUAToXMLElement(Exception& ex, lua_State* pState, PacketWriter& writer) { // -1 => table lua_pushstring(pState, "__name"); lua_rawget(pState, -2); const char* name(lua_tostring(pState, -1)); lua_pop(pState, 1); if (!name) { ex.set(Exception::APPLICATION, "Impossible to write a XML element without name (__name)"); return false; } // write attributes writer.write("<").write(name).write(" "); lua_pushnil(pState); // first key while (lua_next(pState, -2) != 0) { // uses 'key' (at index -2) and 'value' (at index -1) if (lua_type(pState, -2) == LUA_TSTRING && strcmp(lua_tostring(pState, -2),"__name")!=0 && lua_isstring(pState, -1)) writer.write(lua_tostring(pState, -2)).write("=\"").write(lua_tostring(pState, -1)).write("\" "); lua_pop(pState, 1); } if (lua_objlen(pState, -1) == 0) { writer.write("/>"); return true; } writer.write(">"); // write elements lua_pushnil(pState); // first key while (lua_next(pState, -2) != 0) { // uses 'key' (at index -2) and 'value' (at index -1) if (lua_isnumber(pState, -2)) { if (lua_istable(pState, -1)) LUAToXMLElement(ex, pState, writer); // table child else if (lua_isstring(pState, -1)) writer.write(lua_tostring(pState, -1), lua_objlen(pState, -1)); // __value else ex.set(Exception::APPLICATION, "Impossible to convert the value of type ", lua_typename(pState, lua_type(pState, -1)), "to a correct XML value of ",name); } else if (lua_type(pState, -2) != LUA_TSTRING) ex.set(Exception::APPLICATION, "Impossible to convert the key of type ",lua_typename(pState,lua_type(pState,-2)),"to a correct XML element of ",name); lua_pop(pState, 1); } writer.write("</").write(name).write(">"); return true; }
void dump_to_file(PacketWriter &writer, int packet_count, PDU &packet) { while(packet_count--) { writer.write(packet); } }