summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/saxParse-persistence.cpp50
-rw-r--r--lib/saxParse-persistence.h43
2 files changed, 93 insertions, 0 deletions
diff --git a/lib/saxParse-persistence.cpp b/lib/saxParse-persistence.cpp
new file mode 100644
index 0000000..a6a0d23
--- /dev/null
+++ b/lib/saxParse-persistence.cpp
@@ -0,0 +1,50 @@
+#include "saxParse-persistence.h"
+
+namespace Persistence {
+
+ void
+ SAXParsePersistence::loadStateInternal(FILE * in)
+ {
+ stk.top()->beforeValue(stk);
+ stk.top()->beginObject(stk);
+ parseFile(in);
+ stk.pop();
+ stk.pop();
+ }
+
+ void
+ SAXParsePersistence::elementOpen(mxml_node_t * n)
+ {
+ stk.push(stk.top()->select(mxmlGetElement(n)));
+ stk.top()->beforeValue(stk);
+ stk.top()->beginObject(stk);
+ for (int attrCount = mxmlElementGetAttrCount(n), attrIdx {0}; attrIdx < attrCount; ++attrIdx) {
+ const char *name, *value = mxmlElementGetAttrByIndex(n, attrIdx, &name);
+ auto sel = stk.top()->select(name);
+ sel->beforeValue(stk);
+ sel->setValue(std::string {value});
+ }
+ }
+
+ void
+ SAXParsePersistence::elementClose(mxml_node_t *)
+ {
+ stk.top()->endObject(stk);
+ stk.top()->endObject(stk);
+ }
+
+ void
+ SAXParsePersistence::data(mxml_node_t *)
+ {
+ }
+
+ void
+ SAXParsePersistence::directive(mxml_node_t *)
+ {
+ }
+
+ void
+ SAXParsePersistence::cdata(mxml_node_t *)
+ {
+ }
+}
diff --git a/lib/saxParse-persistence.h b/lib/saxParse-persistence.h
new file mode 100644
index 0000000..91daecc
--- /dev/null
+++ b/lib/saxParse-persistence.h
@@ -0,0 +1,43 @@
+#pragma once
+
+#include "persistence.h"
+#include "saxParse.h"
+#include <cstdio>
+#include <mxml.h>
+
+namespace Persistence {
+ class SAXParsePersistence : public SAXParse {
+ private:
+ template<typename T> struct Root : public Persistable {
+ T t {};
+ bool
+ persist(PersistenceStore & store)
+ {
+ return STORE_TYPE && STORE_NAME_MEMBER("ilt", t);
+ }
+ };
+
+ void loadStateInternal(FILE * in);
+
+ public:
+ template<typename T>
+ auto
+ loadState(FILE * in)
+ {
+ std::unique_ptr<Root<T>> root;
+ stk.push(std::make_unique<SelectionT<decltype(root)>>(std::ref(root)));
+ loadStateInternal(in);
+ return std::move(root->t);
+ }
+
+ protected:
+ void elementOpen(mxml_node_t * n) override;
+ void elementClose(mxml_node_t *) override;
+ void data(mxml_node_t *) override;
+ void directive(mxml_node_t *) override;
+ void cdata(mxml_node_t *) override;
+
+ private:
+ Stack stk;
+ };
+}