Add XMLDB and XMLController

This commit is contained in:
Kirigaya Kazuto 2017-04-13 20:28:33 +08:00
parent f9c2e872df
commit 93b2edf0f0
2 changed files with 196 additions and 0 deletions

View File

@ -1 +1,145 @@
#include "MiniEngine_Xml.h"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"
namespace MiniEngine
{
XMLDB::XMLDB()
{
_opened=false;
}
XMLDB::~XMLDB()
{
close();
}
int XMLDB::open(const std::string& XMLFileName)
{
if(_opened) return -2;
try
{
rapidxml::file<> temp(XMLFileName.c_str());
_doc.parse<0>(temp.data());
_filename=XMLFileName;
_opened=true;
_changed=false;
}
catch(...)
{
return -1;
}
return 0;
}
int XMLDB::save()
{
if(!_opened) return -2;
if(!_changed) return 0;
std::ofstream ofs(_filename);
ofs<<_doc;
return 0;
}
int XMLDB::close()
{
if(!_opened) return 0;
_doc.clear();
_filename.clear();
_opened=false;
return 0;
}
bool XMLDB::isopen() const
{
return _opened;
}
rapidxml::xml_node<>* XMLDB::getDBroot(const std::string& dbname)
{
for(rapidxml::xml_node<>* root=_doc.first_node("Database");root!=nullptr;root=root->next_sibling("Database"))
{
rapidxml::xml_attribute<>* attr=root->first_attribute("name");
if(attr==nullptr||dbname!=attr->value()) continue;
else return root;
}
return nullptr;
}
rapidxml::xml_document<>* XMLDB::getdoc()
{
return &_doc;
}
XMLController::XMLController()
{
_pdb=nullptr;
}
XMLController::~XMLController()
{
}
int XMLController::connect(const XMLDB& xmldb)
{
if(!xmldb.isopen())
{
return -1;
}
_pdb=const_cast<XMLDB*>(&xmldb);
return 0;
}
int XMLController::usedb(const std::string& dbname)
{
rapidxml::xml_node<>* node=_pdb->getDBroot(dbname);
if(node==nullptr)
{
return -1;
}
else
{
_dbroot=node;
return 0;
}
return 0;
}
int XMLController::createdb(const std::string& dbname,std::vector<std::string>& colname)
{
if(dbname.empty()) return -1;
if(_pdb->getDBroot(dbname)!=nullptr)
{
/// Invalid Name (Same as another database).
return -2;
}
rapidxml::xml_node<>* newnode=_pdb->getdoc()->allocate_node(rapidxml::node_element,"Database");
if(newnode==nullptr) return -3;
rapidxml::xml_attribute<>* newattr=_pdb->getdoc()->allocate_attribute("name",dbname.c_str());
if(newattr==nullptr)
{
/// FIXME: What will happen to newnode?
return -4;
}
rapidxml::xml_node<>* dbinfonode=_pdb->getdoc()->allocate_node(rapidxml::node_element,"DBInfo");
if(dbinfonode==nullptr) return -5;
for(size_t i=0;i<colname.size();i++)
{
rapidxml::xml_node<>* ntnode=_pdb->getdoc()->allocate_node(rapidxml::node_element,"colname",colname.at(i).c_str());
if(ntnode==nullptr) return -6;
dbinfonode->append_node(ntnode);
}
newnode->append_node(dbinfonode);
newnode->append_attribute(newattr);
return 0;
}
}///End of namespace MiniEngine

View File

@ -1,3 +1,55 @@
#pragma once
#include "MiniEngine.h"
#include <vector>
#include <string>
#include "rapidxml/rapidxml.hpp"
/** MiniEngine XML-Engine
* MiniEngine XML-Engine is based on rapidXML.
*/
namespace MiniEngine
{
class XMLDB : public MiniEngine::NonCopyable
{
public:
XMLDB();
~XMLDB();
int open(const std::string& XMLFileName);
int save();
int close();
bool isopen() const;
rapidxml::xml_node<>* getDBroot(const std::string& dbname);
/// Directly Get Document
rapidxml::xml_document<>* getdoc();
private:
rapidxml::xml_document<> _doc;
bool _changed;
bool _opened;
std::string _filename;
};
class XMLController
{
public:
XMLController();
~XMLController();
int connect(const XMLDB& xmldb);
int usedb(const std::string& dbname);
int createdb(const std::string& dbname,std::vector<std::string>& colname);
int dropdb(const std::string& dbname);
int close();
int execute(const std::string& command);
int execute(const std::string& command,std::vector<std::string>& vec);
private:
XMLDB* _pdb;
std::string _dbname;
rapidxml::xml_node<>* _dbroot;
};
}/// End of namespace MiniEngine