From db072ad4dc181eca5a1458656b130beb43f475bf Mon Sep 17 00:00:00 2001 From: 53hornet <53hornet@gmail.com> Date: Sat, 2 Feb 2019 23:33:15 -0500 Subject: Init. --- hw4/src/sceneIO_xml.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 hw4/src/sceneIO_xml.cpp (limited to 'hw4/src/sceneIO_xml.cpp') diff --git a/hw4/src/sceneIO_xml.cpp b/hw4/src/sceneIO_xml.cpp new file mode 100644 index 0000000..47aa94e --- /dev/null +++ b/hw4/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 +#include "sceneIO_xml.h" +#include "errorMessage.h" + +///////////////// +// Constructor // +///////////////// +XMLNode::XMLNode(const std::string& filename) +{ + // open xml + _xml = std::make_shared(); + _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& 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; +} + + -- cgit v1.2.3