From 547be65c3ee6e9969afc0b5f69ba7637e8ad78a5 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Thu, 18 May 2017 09:00:27 +0800 Subject: [PATCH] Add more functions to Attr and Node --- MiniEngine_Xml.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++ MiniEngine_Xml.h | 24 +++++++--- 2 files changed, 134 insertions(+), 6 deletions(-) diff --git a/MiniEngine_Xml.cpp b/MiniEngine_Xml.cpp index 0bc26d9..2ea9ab3 100644 --- a/MiniEngine_Xml.cpp +++ b/MiniEngine_Xml.cpp @@ -9,6 +9,102 @@ namespace MiniEngine namespace XML { +void Attribute::_set(XAttr* pattr) +{ + _pattr=pattr; +} + +XAttr* Attribute::_get() const +{ + return _pattr; +} + +void Attribute::_clear() +{ + _pattr=nullptr; +} + +void Attribute::_setdoc(Document* pDoc) +{ + _pdoc=pDoc; +} + +std::string Attribute::getName() const +{ + return std::string(getNameRaw()); +} + +std::string Attribute::getValue() const +{ + return std::string(getValueRaw()); +} + +char* Attribute::getNameRaw() const +{ + return _pattr->name(); +} + +char* Attribute::getValueRaw() const +{ + return _pattr->value(); +} + + +Node::Node() +{ + _pnode=nullptr; + _pdoc=nullptr; +} + +Node::Node(XNode* expNode) +{ + _pnode=expNode; + _pdoc=nullptr; +} + + + +void Node::_set(XNode* node) +{ + _pnode=node; +} + +XNode* Node::_get() const +{ + return _pnode; +} + +void Node::_clear() +{ + _pnode=nullptr; +} + +void Node::_setdoc(Document* pDoc) +{ + _pdoc=pDoc; +} + +std::string Node::getName() const +{ + return std::string(getNameRaw()); +} + +std::string Node::getValue() const +{ + return std::string(getValueRaw()); +} + +char* Node::getNameRaw() const +{ + return _pnode->name(); +} + +char* Node::getValueRaw() const +{ + return _pnode->value(); +} + + void Node::push_front(const Node& node) { _pnode->prepend_node(node._pnode); @@ -79,6 +175,11 @@ void Node::remove_all_attr() _pnode->remove_all_attributes(); } +bool Node::operator==(const Node& node) +{ + return _pnode==node._pnode && _pdoc==node._pdoc; +} + bool Node::hasPrevNode() const { return _pnode->previous_sibling()!=nullptr; @@ -109,6 +210,11 @@ Node Node::getParentNode() const return Node(_pnode->parent()); } +bool Node::valid() +{ + return _pnode!=nullptr && _pdoc!=nullptr; +} + Document::Document() { @@ -180,6 +286,16 @@ Attribute Document::newAttr(const std::string& name,const std::string& value) return attr; } +Node Document::cloneNode(const Node& node) +{ + return Node(_doc.clone_node(node._get())); +} + +void Document::clear() +{ + return _doc.clear(); +} + //protected char* Document::_allocate_string(const std::string& str) { diff --git a/MiniEngine_Xml.h b/MiniEngine_Xml.h index d4838a5..431ce02 100644 --- a/MiniEngine_Xml.h +++ b/MiniEngine_Xml.h @@ -22,6 +22,12 @@ public: XAttr* _get() const; void _clear(); void _setdoc(Document*); + + std::string getName() const; + std::string getValue() const; + + char* getNameRaw() const; + char* getValueRaw() const; private: XAttr* _pattr; Document* _pdoc; @@ -31,13 +37,19 @@ class Node { public: void _set(XNode*); - XNode* _get(); + XNode* _get() const; void _clear(); void _setdoc(Document*); Node(); Node(XNode*); + std::string getName() const; + std::string getValue() const; + + char* getNameRaw() const; + char* getValueRaw() const; + void push_front(const Node&); void push_back(const Node&); void insert(const Node& where,const Node& val); @@ -56,10 +68,7 @@ public: void remove_attr(const Attribute& todelete); void remove_all_attr(); - bool operator == (const Node& node) - { - return _pnode==node._pnode && _pdoc==node._pdoc; - } + bool operator == (const Node& node); bool hasPrevNode() const; bool hasNextNode() const; @@ -68,7 +77,7 @@ public: Node getNextNode() const; Node getParentNode() const; - Node clone(); + bool valid(); private: XNode* _pnode; @@ -85,6 +94,9 @@ public: bool ready(); Node newNode(const std::string& name,const std::string& value); Attribute newAttr(const std::string& name,const std::string& value); + Node cloneNode(const Node&); + void clear(); + protected: char* _allocate_string(const std::string& str); char* _allocate_string(const char* pstr,int sz);