From bf0f1cab25fa460ad4716ca60c11abab78a52cbd Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Wed, 30 Apr 2014 21:36:27 +0800 Subject: [PATCH] Tester for loading document in XML format 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 | 4 ++++ src/tests/xml.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/tests/test.xml create mode 100644 src/tests/xml.cpp diff --git a/src/tests/test.xml b/src/tests/test.xml new file mode 100644 index 0000000..997638c --- /dev/null +++ b/src/tests/test.xml @@ -0,0 +1,4 @@ + + + + diff --git a/src/tests/xml.cpp b/src/tests/xml.cpp new file mode 100644 index 0000000..a9335cd --- /dev/null +++ b/src/tests/xml.cpp @@ -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; +} -- 2.20.1