summaryrefslogblamecommitdiff
path: root/hw6/src/sceneIO_xml.cpp
blob: 47aa94e4e4909248c6bc6e24888325c78bb8095a (plain) (tree)















































































































                                                                                                        
/******************************************************************/
/* 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;
}