Tester for loading document in XML format
authorSam Moore <[email protected]>
Wed, 30 Apr 2014 13:36:27 +0000 (21:36 +0800)
committerSam Moore <[email protected]>
Wed, 30 Apr 2014 13:36:27 +0000 (21:36 +0800)
Inspired by the fact that SVG and HTML are XML and I was trying to write about them in the Literature Notes.

The pugixml library for C++ seems actually usable.
Could do a number of fancy things using it, but really should get back to that Literature Review :S

src/tests/test.xml [new file with mode: 0644]
src/tests/xml.cpp [new file with mode: 0644]

diff --git a/src/tests/test.xml b/src/tests/test.xml
new file mode 100644 (file)
index 0000000..997638c
--- /dev/null
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<!-- This is a test to see how hard it is to work with XML and pugixml -->
+<rect x="0.5" y="0.5" w="0.5" h="0.5" outline="true"></rect>
+<rect x="0.25" y="0.25" w="0.5" h="0.5"></rect>
diff --git a/src/tests/xml.cpp b/src/tests/xml.cpp
new file mode 100644 (file)
index 0000000..a9335cd
--- /dev/null
@@ -0,0 +1,47 @@
+#include "main.h"
+#include "../../contrib/pugixml-1.4/src/pugixml.hpp"
+#include "../../contrib/pugixml-1.4/src/pugixml.cpp"
+
+using namespace std;
+
+/**
+ * This tester reads XML and uses it to produce rectangles in our viewer
+ * We use pugixml because I really didn't feel like implementing an XML parser
+ */
+int main(int argc, char ** argv)
+{
+       pugi::xml_document doc_xml;
+       Document doc;
+       pugi::xml_parse_result result = doc_xml.load(cin); // load from stdin
+       
+       if (!result)
+       {
+               Fatal("XML from stdin has errors: %s", result.description());
+       }
+
+       Debug("pugixml load result was: %s", result.description());
+       int count = 0;
+       for (pugi::xml_node rect : doc_xml.children("rect"))
+       {
+                       
+               Real coords[4];
+               const char * attrib_names[] = {"x", "y", "w", "h"};
+               for (size_t i = 0; i < sizeof(attrib_names)/sizeof(char*); ++i)
+               {
+                       coords[i] = rect.attribute(attrib_names[i]).as_float();
+               }
+
+               bool outline = false;
+               outline = rect.attribute("outline").as_bool();
+
+               Debug("rect node %d is {%lf, %lf, %lf, %lf} (%s)", count++, coords[0], coords[1], coords[2], coords[3], (outline) ? "outline" : "filled");
+               doc.Add(outline?RECT_OUTLINE:RECT_FILLED, Rect(coords[0], coords[1], coords[2], coords[3]));
+       }
+
+
+       MainLoop(doc);
+
+       
+
+       return 0;
+}

UCC git Repository :: git.ucc.asn.au