Add XML Objects

Document,Node,Attribute...
This commit is contained in:
Kirigaya Kazuto 2017-05-17 21:40:33 +08:00
parent f9c2e872df
commit fc4940c09b
2 changed files with 182 additions and 1 deletions

View File

@ -1 +1,97 @@
#include "MiniEngine_Xml.h" #include "MiniEngine_Xml.h"
#include "rapidxml/rapidxml_print.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include <fstream>
namespace MiniEngine
{
namespace XML
{
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;
node._pnode=_doc.allocate_node(rapidxml::node_type::node_element,
_allocate_string(name),_allocate_string(value));
node._pdoc=this;
return node;
}
Attribute Document::newAttr(const std::string& name,const std::string& value)
{
Attribute attr;
attr._pattr=_doc.allocate_attribute(_allocate_string(name),_allocate_string(value));
attr._pdoc=this;
return attr;
}
//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

View File

@ -1,3 +1,88 @@
#pragma once #pragma once
#include "MiniEngine.h" #include "rapidxml/rapidxml.hpp"
#include <string>
namespace MiniEngine
{
namespace XML
{
typedef rapidxml::xml_node<> XNode;
typedef rapidxml::xml_attribute<> XAttr;
typedef rapidxml::xml_document<> XDoc;
/// Fwd Decl
class Document;
class Attribute
{
private:
XAttr* _pattr;
Document* _pdoc;
friend class Document;
};
class Node
{
public:
void push_front(const Node&);
void push_back(const Node&);
void insert(const Node& where,const Node& val);
void remove_first_node();
void remove_last_node();
void remove_node(const Node& todelete);
void remove_all_node();
void push_front(const Attribute&);
void push_back(const Attribute&);
void insert(const Attribute& where,const Attribute& val);
void remove_first_attr();
void remove_last_attr();
void remove_attr(const Attribute& todelete);
void remove_all_attr();
bool operator == (const Node& node)
{
return _pnode==node._pnode && _pdoc==node._pdoc;
}
bool hasPrevNode() const;
bool hasNextNode() const;
bool hasParentNode() const;
Node getPrevNode() const;
Node getNextNode() const;
Node getParentNode() const;
private:
XNode* _pnode;
Document* _pdoc;
friend class Document;
};
class Document
{
public:
Document();
Document(const std::string& filename);
int loadFrom(const std::string& filename,bool clearCurrent=true);
int saveTo(const std::string& filename);
bool ready();
Node newNode(const std::string& name,const std::string& value);
Attribute newAttr(const std::string& name,const std::string& value);
protected:
char* _allocate_string(const std::string& str);
char* _allocate_string(const char* pstr,int sz);
private:
XDoc _doc;
bool _is_ready;
};
}/// End of namespace MiniEngine::XML
}/// End of namespace MiniEngine