void OTMLNode::addChild(const OTMLNodePtr& newChild) { // replace is needed when the tag is marked as unique if(newChild->hasTag()) { for(const OTMLNodePtr& node : m_children) { if(node->tag() == newChild->tag() && (node->isUnique() || newChild->isUnique())) { newChild->setUnique(true); if(node->hasChildren() && newChild->hasChildren()) { OTMLNodePtr tmpNode = node->clone(); tmpNode->merge(newChild); newChild->copy(tmpNode); } replaceChild(node, newChild); // remove any other child with the same tag auto it = m_children.begin(); while(it != m_children.end()) { OTMLNodePtr node = (*it); if(node != newChild && node->tag() == newChild->tag()) { it = m_children.erase(it); } else ++it; } return; } } } m_children.push_back(newChild); }
// otml nodes void push_otml_subnode_luavalue(const OTMLNodePtr& node) { if(node->hasValue()) { union { bool b; double d; long l; }; std::string value = node->rawValue(); if(stdext::cast(value, b)) g_lua.pushBoolean(b); else if(stdext::cast(value, l)) g_lua.pushInteger(l); else if(stdext::cast(value, d)) g_lua.pushNumber(d); else g_lua.pushString(value); } else if(node->hasChildren()) { g_lua.newTable(); bool pushedChild = false; int currentIndex = 1; for(const OTMLNodePtr& cnode : node->children()) { push_otml_subnode_luavalue(cnode); if(!g_lua.isNil()) { if(cnode->isUnique()) { g_lua.pushString(cnode->tag()); g_lua.insert(-2); g_lua.rawSet(); } else g_lua.rawSeti(currentIndex++); pushedChild = true; } else g_lua.pop(); } if(!pushedChild) { g_lua.pop(); g_lua.pushNil(); } } else g_lua.pushNil(); }