summaryrefslogtreecommitdiff
path: root/hw6/src/sceneIO_xml.cpp
diff options
context:
space:
mode:
author53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
committer53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
commitdb072ad4dc181eca5a1458656b130beb43f475bf (patch)
treea3c03c7f5497cb70503e2486662fa85cfb53415a /hw6/src/sceneIO_xml.cpp
downloadcsci427-master.tar.xz
csci427-master.zip
Diffstat (limited to 'hw6/src/sceneIO_xml.cpp')
-rw-r--r--hw6/src/sceneIO_xml.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/hw6/src/sceneIO_xml.cpp b/hw6/src/sceneIO_xml.cpp
new file mode 100644
index 0000000..47aa94e
--- /dev/null
+++ b/hw6/src/sceneIO_xml.cpp
@@ -0,0 +1,112 @@
+/******************************************************************/
+/* This file is part of the homework assignments for CSCI-427/527 */
+/* at The College of William & Mary and authored by Pieter Peers. */
+/* No part of this file, whether altered or in original form, can */
+/* be distributed or used outside the context of CSCI-427/527 */
+/* without consent of either the College of William & Mary or */
+/* Pieter Peers. */
+/******************************************************************/
+#include <cassert>
+#include "sceneIO_xml.h"
+#include "errorMessage.h"
+
+/////////////////
+// Constructor //
+/////////////////
+XMLNode::XMLNode(const std::string& filename)
+{
+ // open xml
+ _xml = std::make_shared<tinyxml2::XMLDocument>();
+ _xml->LoadFile(filename.c_str());
+
+ // check if open successful
+ if(_xml->ErrorID() != tinyxml2::XML_SUCCESS)
+ errorMessage("Failed to open scene file: '%s'.", filename.c_str());
+
+ // set first node.
+ _element = _xml->FirstChildElement("scene");
+}
+
+
+XMLNode::XMLNode(const XMLNode& node)
+{
+ _element = node._element;
+ _xml = node._xml;
+}
+
+XMLNode::XMLNode(const tinyxml2::XMLElement* element, const std::shared_ptr<tinyxml2::XMLDocument>& xml)
+{
+ _element = element;
+ _xml = xml;
+}
+
+
+/////////////
+// Methods //
+/////////////
+XMLNode XMLNode::firstChild(void) const
+{
+ assert(isValid());
+ return XMLNode(_element->FirstChildElement(), _xml);
+}
+
+
+XMLNode XMLNode::findChild(const std::string& name) const
+{
+ assert(isValid());
+ return XMLNode(_element->FirstChildElement(name.c_str()), _xml);
+}
+
+
+bool XMLNode::isValid(void) const
+{
+ return (_element != NULL);
+}
+
+
+std::string XMLNode::name(void) const
+{
+ assert(isValid());
+ return std::string(_element->Name());
+}
+
+
+std::string XMLNode::attribute(const std::string& attribute) const
+{
+ assert(isValid());
+
+ const char* str = _element->Attribute(attribute.c_str());
+ if(str == NULL) return std::string("");
+
+ // Done.
+ return std::string(str);
+}
+
+
+///////////////
+// Operators //
+///////////////
+XMLNode& XMLNode::operator=(const XMLNode& src)
+{
+ _element = src._element;
+ _xml = src._xml;
+ return *this;
+}
+
+
+XMLNode& XMLNode::operator++(int)
+{
+ assert(_element != NULL);
+ _element = _element->NextSiblingElement();
+ return *this;
+}
+
+
+XMLNode& XMLNode::operator++(void)
+{
+ assert(_element != NULL);
+ _element = _element->NextSiblingElement();
+ return *this;
+}
+
+