summaryrefslogtreecommitdiff
path: root/hw3/src/sceneIO_xml.cpp
blob: 6f9648057180c4ec3fc95217aaac552a92c66abb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/******************************************************************/
/* 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;
}


XMLNode& XMLNode::operator++(int)
{
  assert(_element != NULL);
  _element = _element->NextSiblingElement();
}


XMLNode& XMLNode::operator++(void)
{
  assert(_element != NULL);
  _element = _element->NextSiblingElement();
}