MiniEngine/MiniEngine_Xml.cpp

383 lines
6.0 KiB
C++
Raw Normal View History

2017-04-13 16:19:35 +08:00
#include "MiniEngine_Xml.h"
#include "rapidxml/rapidxml_print.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include <fstream>
namespace MiniEngine
{
namespace XML
{
2017-05-18 09:00:27 +08:00
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;
}
2017-05-20 16:00:19 +08:00
Attribute::Attribute()
{
_pattr=nullptr;
_pdoc=nullptr;
}
Attribute::Attribute(XAttr* pAttr)
{
_pattr=pAttr;
_pdoc=nullptr;
}
2017-05-18 09:00:27 +08:00
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();
}
2017-05-20 16:00:19 +08:00
bool Attribute::hasPrevAttr() const
{
return _pattr->previous_attribute()!=nullptr;
}
bool Attribute::hasNextAttr() const
{
return _pattr->next_attribute()!=nullptr;
}
Attribute Attribute::getPrevAttr() const
{
return Attribute(_pattr->previous_attribute());
}
Attribute Attribute::getNextAttr() const
{
return Attribute(_pattr->next_attribute());
}
2017-05-18 09:00:27 +08:00
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();
}
2017-05-20 16:00:19 +08:00
Node& Node::push_front(const Node& node)
2017-05-17 23:24:43 +08:00
{
_pnode->prepend_node(node._pnode);
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::push_back(const Node& node)
2017-05-17 23:24:43 +08:00
{
_pnode->append_node(node._pnode);
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::insert(const Node& where, const Node& val)
2017-05-17 23:24:43 +08:00
{
_pnode->insert_node(where._pnode,val._pnode);
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::remove_first_node()
2017-05-17 23:24:43 +08:00
{
_pnode->remove_first_node();
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::remove_last_node()
2017-05-17 23:24:43 +08:00
{
_pnode->remove_last_node();
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::remove_node(const Node& todelete)
2017-05-17 23:24:43 +08:00
{
_pnode->remove_node(todelete._pnode);
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::remove_all_node()
2017-05-17 23:24:43 +08:00
{
_pnode->remove_all_nodes();
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::push_front(const Attribute& attr)
2017-05-17 23:24:43 +08:00
{
_pnode->prepend_attribute(attr._get());
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::push_back(const Attribute& attr)
2017-05-17 23:24:43 +08:00
{
_pnode->append_attribute(attr._get());
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::insert(const Attribute& where, const Attribute& val)
2017-05-17 23:24:43 +08:00
{
_pnode->insert_attribute(where._get(),val._get());
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::remove_first_attr()
2017-05-17 23:24:43 +08:00
{
_pnode->remove_first_attribute();
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::remove_last_attr()
2017-05-17 23:24:43 +08:00
{
_pnode->remove_last_attribute();
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::remove_attr(const Attribute& todelete)
2017-05-17 23:24:43 +08:00
{
_pnode->remove_attribute(todelete._get());
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-20 16:00:19 +08:00
Node& Node::remove_all_attr()
2017-05-17 23:24:43 +08:00
{
_pnode->remove_all_attributes();
2017-05-20 16:00:19 +08:00
return *this;
2017-05-17 23:24:43 +08:00
}
2017-05-18 09:00:27 +08:00
bool Node::operator==(const Node& node)
{
return _pnode==node._pnode && _pdoc==node._pdoc;
}
2017-05-17 23:24:43 +08:00
bool Node::hasPrevNode() const
{
return _pnode->previous_sibling()!=nullptr;
}
bool Node::hasNextNode() const
{
return _pnode->next_sibling()!=nullptr;
}
bool Node::hasParentNode() const
{
return _pnode->parent()!=nullptr;
}
Node Node::getPrevNode() const
{
return Node(_pnode->previous_sibling());
}
Node Node::getNextNode() const
{
return Node(_pnode->next_sibling());
}
2017-05-20 16:00:19 +08:00
Node Node::getPrevNode(const std::string& name) const
{
return Node(_pnode->previous_sibling(name.c_str()));
}
Node Node::getNextNode(const std::string& name) const
{
return Node(_pnode->next_sibling(name.c_str()));
}
2017-05-17 23:24:43 +08:00
Node Node::getParentNode() const
{
return Node(_pnode->parent());
}
2017-05-18 09:00:27 +08:00
bool Node::valid()
{
return _pnode!=nullptr && _pdoc!=nullptr;
}
2017-05-17 23:24:43 +08:00
2017-05-20 16:00:19 +08:00
Node Node::getChild() const
{
return _pnode->first_node();
}
Node Node::getChild(const std::string& nodename) const
{
return _pnode->first_node(nodename.c_str());
}
Document::Document()
{
_is_ready=false;
}
Document::Document(const std::string& filename)
{
if(loadFrom(filename,false)!=0)
{
_is_ready=false;
}
else
{
_is_ready=true;
}
}
int Document::loadFrom(const std::string& filename, bool clearCurrent)
{
std::ifstream ifs(filename);
if(!ifs)
{
/// File Read Error.
return -1;
}
rapidxml::file<> infilereader(ifs);
if(clearCurrent)
{
_doc.clear();
}
_doc.parse<0>(infilereader.data());
return 0;
}
int Document::saveTo(const std::string& filename)
{
std::string ans;
rapidxml::print(std::back_inserter(ans),_doc,0);
std::ofstream ofs(filename);
if(!ofs) return -1;
ofs<<ans;
return 0;
}
bool Document::ready()
{
return _is_ready;
}
Node Document::newNode(const std::string& name,const std::string& value)
{
Node node;
2017-05-17 23:24:43 +08:00
node._set(_doc.allocate_node(rapidxml::node_type::node_element,
_allocate_string(name),_allocate_string(value)));
node._setdoc(this);
return node;
}
Attribute Document::newAttr(const std::string& name,const std::string& value)
{
Attribute attr;
2017-05-17 23:24:43 +08:00
attr._set(_doc.allocate_attribute(_allocate_string(name),_allocate_string(value)));
attr._setdoc(this);
return attr;
}
2017-05-18 09:00:27 +08:00
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)
{
return _doc.allocate_string(str.c_str(),str.size());
}
//protected
char* Document::_allocate_string(const char* source,int sz)
{
return _doc.allocate_string(source,sz);
}
}/// End of namespace MiniEngine::XML
}/// End of namespace MiniEngine