added support for XML reading/writing
new serialization interfaces new helper methods new wifi models
This commit is contained in:
14
data/XMLload.h
Normal file
14
data/XMLload.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef XMLLOAD_H
|
||||
#define XMLLOAD_H
|
||||
|
||||
#include "xml.h"
|
||||
|
||||
class XMLload {
|
||||
|
||||
public:
|
||||
|
||||
virtual void readFromXML(XMLDoc* doc, XMLElem* src) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif // XMLLOAD_H
|
||||
14
data/XMLsave.h
Normal file
14
data/XMLsave.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef XMLSAVE_H
|
||||
#define XMLSAVE_H
|
||||
|
||||
#include "xml.h"
|
||||
|
||||
class XMLsave {
|
||||
|
||||
public:
|
||||
|
||||
virtual void writeToXML(XMLDoc* doc, XMLElem* dst) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif // XMLSAVE_H
|
||||
44
data/XMLserialize.h
Normal file
44
data/XMLserialize.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef XMLSERIALIZE_H
|
||||
#define XMLSERIALIZE_H
|
||||
|
||||
#include "XMLload.h"
|
||||
#include "XMLsave.h"
|
||||
#include "xml.h"
|
||||
|
||||
class XMLserialize : public XMLload, public XMLsave {
|
||||
|
||||
public:
|
||||
|
||||
void loadXML(const std::string& file) {
|
||||
|
||||
XMLDoc doc;
|
||||
assertOK(doc.LoadFile(file.c_str()), doc, "error while loading file");
|
||||
XMLElem* root = doc.FirstChildElement("root");
|
||||
readFromXML(&doc, root);
|
||||
|
||||
}
|
||||
|
||||
void saveXML(const std::string& file) {
|
||||
|
||||
XMLDoc doc;
|
||||
XMLElem* root = doc.NewElement("root");
|
||||
doc.InsertFirstChild(root);
|
||||
writeToXML(&doc, root);
|
||||
assertOK(doc.SaveFile(file.c_str()), doc, "error while saving file");
|
||||
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
|
||||
static void assertOK(XMLErr res, XMLDoc& doc, const std::string& txt) {
|
||||
if (res != tinyxml2::XMLError::XML_SUCCESS) {
|
||||
const std::string err = doc.ErrorName();
|
||||
const std::string add = doc.GetErrorStr1();
|
||||
throw Exception(txt + ": " + err + " - " + add);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // XMLSERIALIZE_H
|
||||
33
data/xml.h
Normal file
33
data/xml.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef DATA_XML_H
|
||||
#define DATA_XML_H
|
||||
|
||||
#include "../lib/tinyxml/tinyxml2.h"
|
||||
|
||||
using XMLDoc = tinyxml2::XMLDocument;
|
||||
using XMLErr = tinyxml2::XMLError;
|
||||
using XMLNode = tinyxml2::XMLNode;
|
||||
using XMLElem = tinyxml2::XMLElement;
|
||||
using XMLAttr = tinyxml2::XMLAttribute;
|
||||
using XMLText = tinyxml2::XMLText;
|
||||
|
||||
#define XML_FOREACH_ATTR(attr, root) \
|
||||
for (const XMLAttr* attr = root->FirstAttribute(); attr != nullptr; attr = attr->Next())
|
||||
|
||||
#define XML_FOREACH_NODE(sub, root) \
|
||||
for (const XMLNode* sub = root->FirstChild(); sub != nullptr; sub = sub->NextSibling())
|
||||
|
||||
#define XML_FOREACH_ELEM(sub, root) \
|
||||
for (XMLElem* sub = (XMLElem*)root->FirstChild(); sub != nullptr; sub = (XMLElem*)sub->NextSibling())
|
||||
|
||||
#define XML_FOREACH_ELEM_NAMED(name, sub, root) \
|
||||
for (XMLElem* sub = root->FirstChildElement(name); sub != nullptr; sub = sub->NextSiblingElement(name))
|
||||
|
||||
#define XML_ID(node) node->Attribute("xml:id")
|
||||
#define XML_WITH_ID(node) node->Attribute("with_id")
|
||||
|
||||
#define XML_ASSERT_NODE_NAME(name, node) if (std::string(name) != std::string(node->Name())) {throw Exception("expected " + std::string(name) + " got " + node->Name());}
|
||||
|
||||
#define XML_MANDATORY_ATTR(node, attr) (node->Attribute(attr) ? node->Attribute(attr) : throw Exception(std::string("missing XML attribute: ") + attr));
|
||||
|
||||
|
||||
#endif // DATA_XML_H
|
||||
Reference in New Issue
Block a user