[Scummvm-git-logs] scummvm master -> 64681f5ded62cf1ee8c4c49bf5b2b421a766f746

bluegr noreply at scummvm.org
Thu Sep 22 06:40:26 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
64681f5ded COMMON: Add basic text node support to XMLParser


Commit: 64681f5ded62cf1ee8c4c49bf5b2b421a766f746
    https://github.com/scummvm/scummvm/commit/64681f5ded62cf1ee8c4c49bf5b2b421a766f746
Author: Matthew Duggan (mgithub at guarana.org)
Date: 2022-09-22T09:40:21+03:00

Commit Message:
COMMON: Add basic text node support to XMLParser

Changed paths:
    common/xmlparser.cpp
    common/xmlparser.h


diff --git a/common/xmlparser.cpp b/common/xmlparser.cpp
index b50516df00a..aa7471e6878 100644
--- a/common/xmlparser.cpp
+++ b/common/xmlparser.cpp
@@ -340,8 +340,24 @@ bool XMLParser::parse() {
 		case kParserNeedHeader:
 		case kParserNeedKey:
 			if (_char != '<') {
-				parserError("Parser expecting key start.");
-				break;
+				if (_allowText) {
+					Common::String text;
+					do {
+						text += _char;
+						_char = _stream->readByte();
+					} while (_char != '<' && _char);
+					if (!_char) {
+						parserError("Unexpected end of file.");
+						break;
+					}
+					if (!textCallback(text)) {
+						parserError("Failed to process text segment.");
+						break;
+					}
+				} else {
+					parserError("Parser expecting key start.");
+					break;
+				}
 			}
 
 			if ((_char = _stream->readByte()) == 0) {
diff --git a/common/xmlparser.h b/common/xmlparser.h
index 4afb4357596..a896fcbdd46 100644
--- a/common/xmlparser.h
+++ b/common/xmlparser.h
@@ -100,7 +100,7 @@ public:
 	/**
 	 * Parser constructor.
 	 */
-	XMLParser() : _XMLkeys(nullptr), _stream(nullptr) {}
+	XMLParser() : _XMLkeys(nullptr), _stream(nullptr), _allowText(false), _char(0) {}
 
 	virtual ~XMLParser();
 
@@ -213,6 +213,17 @@ public:
 		return child->depth > 0 ? _activeKey[child->depth - 1] : 0;
 	}
 
+	/**
+	 * Allow text nodes (eg <tag>this is a text node</tag>) to appear in the
+	 * document.
+	 *
+	 * By default this parser does not allow text nodes and expects all data
+	 * to appear in attributes.
+	 */
+	void setAllowText() {
+		_allowText = true;
+	}
+
 protected:
 
 	/**
@@ -261,6 +272,15 @@ protected:
 		return true;
 	}
 
+	/**
+	 * Called when a text node is found.  This will only be called if
+	 * setAllowText() has been called, otherwise text nodes are considered
+	 * parse errors.
+	 */
+	virtual bool textCallback(const String &val) {
+		return true;
+	}
+
 	/**
 	 * Called when a node is closed. Manages its cleanup and calls the
 	 * closing callback function if needed.
@@ -346,6 +366,7 @@ protected:
 
 private:
 	char _char;
+	bool _allowText; /** Allow text nodes in the doc (default false) */
 	SeekableReadStream *_stream;
 	String _fileName;
 




More information about the Scummvm-git-logs mailing list