summaryrefslogtreecommitdiff
path: root/algo1w4d1/ValidXML.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'algo1w4d1/ValidXML.cpp')
-rw-r--r--algo1w4d1/ValidXML.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/algo1w4d1/ValidXML.cpp b/algo1w4d1/ValidXML.cpp
new file mode 100644
index 0000000..4629b43
--- /dev/null
+++ b/algo1w4d1/ValidXML.cpp
@@ -0,0 +1,49 @@
+#include "ValidXML.h"
+#include <iostream>
+
+ValidXML::ValidXML() {}
+ValidXML::~ValidXML() {}
+
+void ValidXML::tag_finished() {
+ if (_tag_is_closing) {
+ if (_tag_stack.size() == 0) {
+ _valid = false;
+ return;
+ }
+ if (_tag_name.compare(_tag_stack.pop()) != 0) _valid = false;
+ } else {
+ _tag_stack.push(_tag_name);
+ }
+
+ // reset tag parser state
+ _tag_name = "";
+ _tag_is_closing = false;
+ _parsing_tag = false;
+ _tag_counter = 0;
+}
+
+void ValidXML::parse(char input) {
+ if (!_valid) return; // invalid XML can never be valid again
+
+ if (!_parsing_tag) {
+ if (input == '<') _parsing_tag = true; // < starts tag
+ if (input == '>') _valid = false; // double tag close
+ return; // ignore text outside tags
+ } else {
+ if (input == '/') { // closing tag marker
+ if (_tag_counter == 0) _tag_is_closing = true; // only allowed directly after <
+ else _valid = false;
+ return; // prevent increment of tag counter
+ }
+ else if (input == '<') _valid = false; // double tag open
+ else if (input == '>') return tag_finished(); // > closes tag
+ else if (_tag_counter == _tag_name.size() && input != ' ') _tag_name += input; // name is content of tag until space
+
+ _tag_counter++;
+ }
+}
+
+bool ValidXML::input_valid() {
+ // if (_tag_stack.size() > 0) return false;
+ return _valid;
+}