int tixmlnode_close(lua_State *L) { TiXmlNode *xmlnode; TiXmlNode_ud* xmlnode_userdata = (TiXmlNode_ud *) luaL_checkudata(L, 1, "TiXmlNode"); xmlnode = xmlnode_userdata->xmlnode; lua_pop(L, 1); // decrease refcount for corresponding document if (xmlnode) decrease_tixmldocument_refcount(L, xmlnode->GetDocument()); xmlnode_userdata->xmlnode = NULL; return 0; }
int l_select(lua_State *L) { const char *expr = luaL_checkstring(L, 1); TiXmlNode *node = (TiXmlNode *) lua_touserdata(L, 2); TinyXPath::xpath_processor xpp(node, expr); TinyXPath::expression_result res(xpp.er_compute_xpath()); if (xpp.e_error != TinyXPath::xpath_processor::e_no_error) { string errmsg("error while computing xpath query `"); errmsg += expr; errmsg += "'"; return luaL_error(L, errmsg.c_str()); } switch (res.e_type) { case TinyXPath::e_string: lua_pushstring(L, res.cp_get_string()); return 1; case TinyXPath::e_node_set: { TinyXPath::node_set* ns; const TiXmlNode* node; ns = res.nsp_get_node_set(); int nns(ns->u_get_nb_node_in_set()); if (nns == 0) { lua_pushnil(L); return 1; } lua_createtable(L, nns, 0); for (int i=0; i<nns; i++) { lua_pushinteger(L, i+1); if (ns->o_is_attrib(i)) { std::string attr_value = ns->S_get_value(i); lua_pushstring(L, attr_value.c_str()); } else { node = ns->XNp_get_node_in_set(i); if (node->Type() == TiXmlNode::TEXT) { lua_pushstring(L, node->Value()); break; } else { TiXmlNode_ud* node_userdata = (TiXmlNode_ud *) lua_newuserdata(L, sizeof(TiXmlNode_ud)); node_userdata->xmlnode = const_cast<TiXmlNode *>(node); //->Clone(); luaL_newmetatable(L, "TiXmlNode"); lua_setmetatable(L, -2); increase_tixmldocument_refcount(L, node->GetDocument()); } } lua_settable(L, -3); } return 1; } case TinyXPath::e_bool: lua_pushboolean(L, res.o_get_bool()); return 1; case TinyXPath::e_int: lua_pushinteger(L, res.i_get_int()); return 1; case TinyXPath::e_double: lua_pushnumber(L, res.d_get_double()); return 1; case TinyXPath::e_invalid: return luaL_error(L, "invalid xpath expression"); default: lua_pushnil(L); return 1; } }