summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/xml.c91
-rw-r--r--lib/xml.h48
2 files changed, 139 insertions, 0 deletions
diff --git a/lib/xml.c b/lib/xml.c
new file mode 100644
index 0000000..2ea13ed
--- /dev/null
+++ b/lib/xml.c
@@ -0,0 +1,91 @@
+/*
+ *
+ * xml.h
+ *
+ * (c) 2017 Jörg Wendel
+ *
+ * This code is distributed under the terms and conditions of the
+ * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
+ */
+
+//***************************************************************************
+// Include
+//***************************************************************************
+
+#include "xml.h"
+
+//***************************************************************************
+// ctor
+//***************************************************************************
+
+cXml::cXml()
+ : printer(0, yes)
+{
+ root = 0;
+}
+
+int cXml::create(const char* rootName)
+{
+ root = doc.NewElement(rootName);
+ doc.InsertFirstChild(root);
+
+ return success;
+}
+
+int cXml::set(const char* data)
+{
+ if (doc.Parse(data) != XML_SUCCESS)
+ return fail;
+
+ root = doc.FirstChild();
+
+ return root ? success : fail;
+}
+
+//***************************************************************************
+// Append Element
+//***************************************************************************
+
+XMLNode* cXml::appendElement(const char* name, const char* value, XMLNode* node)
+{
+ XMLElement* element = doc.NewElement(name);
+
+ element->SetText(value);
+ node = node ? node : root;
+ node->InsertEndChild(element);
+
+ return element;
+}
+
+XMLNode* cXml::appendElement(const char* name, int value, XMLNode* node)
+{
+ XMLElement* element = doc.NewElement(name);
+
+ element->SetText(value);
+ node = node ? node : root;
+ node->InsertEndChild(element);
+
+ return element;
+}
+
+//***************************************************************************
+// To Text
+//***************************************************************************
+
+const char* cXml::toText()
+{
+ doc.Accept(&printer);
+
+ return printer.CStr();
+}
+
+XMLElement* cXml::getFirst(XMLNode* node)
+{
+ node = node ? node : root;
+ return node->FirstChildElement();
+}
+
+XMLElement* cXml::getNext(XMLNode* node)
+{
+ return node->NextSiblingElement();
+}
diff --git a/lib/xml.h b/lib/xml.h
new file mode 100644
index 0000000..ae4a628
--- /dev/null
+++ b/lib/xml.h
@@ -0,0 +1,48 @@
+/*
+ *
+ * xml.h
+ *
+ * (c) 2017 Jörg Wendel
+ *
+ * This code is distributed under the terms and conditions of the
+ * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
+ */
+
+//***************************************************************************
+// Include
+//***************************************************************************
+
+#include <tinyxml2.h>
+
+#include "common.h"
+
+using namespace tinyxml2;
+
+//***************************************************************************
+// Class cXml
+//***************************************************************************
+
+class cXml
+{
+ public:
+
+ cXml();
+
+ int create(const char* rootName);
+ int set(const char* data);
+
+ XMLNode* appendElement(const char* name, const char* value, XMLNode* node = 0);
+ XMLNode* appendElement(const char* name, int value, XMLNode* node = 0);
+
+ XMLNode* getRoot() { return root; }
+ XMLElement* getFirst(XMLNode* node = 0);
+ XMLElement* getNext(XMLNode* node);
+
+ const char* toText();
+
+ private:
+
+ XMLDocument doc;
+ XMLNode* root;
+ XMLPrinter printer;
+};