-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parser.cpp
260 lines (224 loc) · 6.23 KB
/
Parser.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//Parser.cpp: implements Parser.h and contains parseElement() and parseXML()
//These parse through the XML and create XMLSerializable* for vGameWorld.
#include "Weapon.h"
#include "Parser.h"
#include "XMLSerializable.h"
#include "Item.h"
#include "Creature.h"
#include "XMLSerializable.h"
#include "Armor.h"
#include "Potion.h"
#include "Scroll.h"
#include "Entity.h"
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//parseElement: Written by Professor David Brown, modified
//by Aaron Stromberg
// parseElement:
// returns a bool indicating success; false if invalid XML
// was encountered, true if the XML was parsed correctly
//
// arguments:
// istream & input - an input stream of the XML file
// being processed
//
// string sHierarchy - the current position in the XML
// document's object hierarchy which is being processed
//
// note: this does *not* properly check for end of input
// in the XML document!
//
// This function assumes that we are on the first character
// AFTER the opening <.
bool parseElement(istream & input, XMLSerializable* element, vector<XMLSerializable*> & vGameWorld)
{
// char to hold data as we process it
char c;
// The name of the element; initialized to
// an empty string (as all strings are by default);
// we get this by reading in the XML
string sElementName;
// Read in the XML one character at a time, checking
// for the > at the end of the tag
do
{
// Get the character off the stream
c = input.get();
// If it's not the end tag, add it to the element name
if( c != '>' )
sElementName.push_back(c);
} while( c != '>' );
// cout << "Element Name: " << sElementName << endl;
if (sElementName == "Entity")
{
element = new Entity();
vGameWorld.push_back(element);
}
else if (sElementName == "Item")
{
element = new Item();
vGameWorld.push_back(element);
}
else if (sElementName == "Creature")
{
element = new Creature();
vGameWorld.push_back(element);
}
else if (sElementName == "Weapon")
{
element = new Weapon("Punch", 5);
vGameWorld.push_back(element);
}
else if (sElementName == "Armor")
{
element = new Armor("Nothing", 0);
vGameWorld.push_back(element);
}
else if (sElementName == "Potion")
{
element = new Potion();
vGameWorld.push_back(element);
}
else if (sElementName == "Scroll")
{
element = new Scroll();
vGameWorld.push_back(element);
}
// Holds the non-element content of the element
string sContent = "";
// while(true) can be dangerous, but we do have paths
// out of the function.
while( true )
{
// Read a character off the stream
c = input.get();
// The important thing is to check to see if
// it is an open angled bracket.
if( c == '<' )
{
// If it is, we have two possibilities (assuming
// the XML is valid):
//
// Either this is the start tag for a new element
// contained in the current element, or it's
// the end tag for our current element.
//
// Note that if it is an end tag -- and the XML is
// valid -- it MUST be the end tag of the current element
// as elements are not allowed to overlap.
// So we check for the first character
// being a / -- which indicated an end tag
if( input.peek() == '/' )
{
// We must burn off the / since
// we only peeked it, and haven't
// gotten it yet!
input.get();
// Variable to hold the end tag as
// we read it in
string sEndTag;
// Read in the end tag until we get
// to the > at the end of the tag
do
{
c = input.get();
if( c != '>' )
sEndTag.push_back(c);
} while( c != '>' );
// Now, we test for the validity of the XML -- the
// end tag's name must match the element's name...
if( sEndTag != sElementName )
{
cout << "Tag name mismatch" << endl;
return false;
}
// Output what we know to the console --
// the hierarchy (where we are in the document),
// the current element, and its content
if (element != NULL)
{
element->setElementData(sElementName, sContent);
}
// And since we have fully parsed an element, we
// return to whatever called us in the first place
return true;
}
else
{
// In this branch, we have already read in a <,
// but it was NOT an end tag -- the input file
// is currently positioned on the first character
// after the opening <, so we can call parseElement
// on it...
//
// Here we're passing the hierarchy we know plus
// the current element name, so this next element
// knows where it is in the overal document
// hiearchy
if( !parseElement(input, element, vGameWorld) )
return false;
}
}
else
{
// In this branch, we have read in a character inside
// the element which is not a < -- since it's not
// part of an interior element, it's content, so
// we add it to our variable which stores the
// content (ignoring end-of-line characters).
if( c != '\n' )
sContent.push_back(c);
}
}
return true;
}
//parseXML: written by Professor David Brown, modified
//by Aaron Stromberg
// parseXML -- parses an XML document. First it
// makes a very half-hearted check for the validity
// of the XML header, then it parses the root
// element of the document.
bool parseXML(istream & input, vector<XMLSerializable*> & vGameWorld)
{
char c;
// Read in the XML, one character at a time,
// until we hit a <.
do
{
c = input.get();
} while( c != '<' );
// Check the character after the < -- if it's
// not a ?, we aren't seeing a valid XML header
if( input.get() != '?' )
{
cout << "Invalid XML header" << endl;
return false;
}
// Burn off the rest of the header, looking for
// a ?
do
{
c = input.get();
} while( c != '?' );
// Then we check for a > -- which tests for
// the header ending with ?>
if( input.get() != '>' )
{
cout << "Invalid XML header" << endl;
return false;
}
// Now burn off characters until we get to the first
// tag after the XML header...
do
{
c = input.get();
} while( c != '<' );
// And so, we're now on the first character after
// the opening < -- which is exactly what parseElement
// expects. So we call it.
XMLSerializable* element = new XMLSerializable();
return parseElement(input, element, vGameWorld);
}