summaryrefslogtreecommitdiff
path: root/core/io/xml_parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/io/xml_parser.cpp')
-rw-r--r--core/io/xml_parser.cpp127
1 files changed, 70 insertions, 57 deletions
diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp
index a4b64bf17c..11f82fef9d 100644
--- a/core/io/xml_parser.cpp
+++ b/core/io/xml_parser.cpp
@@ -38,9 +38,11 @@ VARIANT_ENUM_CAST(XMLParser::NodeType);
static bool _equalsn(const CharType *str1, const CharType *str2, int len) {
int i;
- for (i = 0; i < len && str1[i] && str2[i]; ++i)
- if (str1[i] != str2[i])
+ for (i = 0; i < len && str1[i] && str2[i]; ++i) {
+ if (str1[i] != str2[i]) {
return false;
+ }
+ }
// if one (or both) of the strings was smaller then they
// are only equal if they have the same length
@@ -48,12 +50,12 @@ static bool _equalsn(const CharType *str1, const CharType *str2, int len) {
}
String XMLParser::_replace_special_characters(const String &origstr) {
-
int pos = origstr.find("&");
int oldPos = 0;
- if (pos == -1)
+ if (pos == -1) {
return origstr;
+ }
String newstr;
@@ -84,8 +86,9 @@ String XMLParser::_replace_special_characters(const String &origstr) {
pos = origstr.find("&", pos);
}
- if (oldPos < origstr.length() - 1)
+ if (oldPos < origstr.length() - 1) {
newstr += (origstr.substr(oldPos, origstr.length() - oldPos));
+ }
return newstr;
}
@@ -100,12 +103,15 @@ bool XMLParser::_set_text(char *start, char *end) {
// only white space, so that this text won't be reported
if (end - start < 3) {
char *p = start;
- for (; p != end; ++p)
- if (!_is_white_space(*p))
+ for (; p != end; ++p) {
+ if (!_is_white_space(*p)) {
break;
+ }
+ }
- if (p == end)
+ if (p == end) {
return false;
+ }
}
// set current text to the parsed text, and replace xml special characters
@@ -126,8 +132,9 @@ void XMLParser::_parse_closing_xml_element() {
++P;
const char *pBeginClose = P;
- while (*P != '>')
+ while (*P != '>') {
++P;
+ }
node_name = String::utf8(pBeginClose, (int)(P - pBeginClose));
#ifdef DEBUG_XML
@@ -141,16 +148,17 @@ void XMLParser::_ignore_definition() {
char *F = P;
// move until end marked with '>' reached
- while (*P != '>')
+ while (*P != '>') {
++P;
+ }
node_name.parse_utf8(F, P - F);
++P;
}
bool XMLParser::_parse_cdata() {
-
- if (*(P + 1) != '[')
+ if (*(P + 1) != '[') {
return false;
+ }
node_type = NODE_CDATA;
@@ -161,8 +169,9 @@ bool XMLParser::_parse_cdata() {
++count;
}
- if (!*P)
+ if (!*P) {
return true;
+ }
char *cDataBegin = P;
char *cDataEnd = nullptr;
@@ -178,10 +187,11 @@ bool XMLParser::_parse_cdata() {
++P;
}
- if (cDataEnd)
+ if (cDataEnd) {
node_name = String::utf8(cDataBegin, (int)(cDataEnd - cDataBegin));
- else
+ } else {
node_name = "";
+ }
#ifdef DEBUG_XML
print_line("XML CDATA: " + node_name);
#endif
@@ -190,7 +200,6 @@ bool XMLParser::_parse_cdata() {
}
void XMLParser::_parse_comment() {
-
node_type = NODE_COMMENT;
P += 1;
@@ -200,10 +209,11 @@ void XMLParser::_parse_comment() {
// move until end of comment reached
while (count) {
- if (*P == '>')
+ if (*P == '>') {
--count;
- else if (*P == '<')
+ } else if (*P == '<') {
++count;
+ }
++P;
}
@@ -217,7 +227,6 @@ void XMLParser::_parse_comment() {
}
void XMLParser::_parse_opening_xml_element() {
-
node_type = NODE_ELEMENT;
node_empty = false;
attributes.clear();
@@ -226,46 +235,52 @@ void XMLParser::_parse_opening_xml_element() {
const char *startName = P;
// find end of element
- while (*P != '>' && !_is_white_space(*P))
+ while (*P != '>' && !_is_white_space(*P)) {
++P;
+ }
const char *endName = P;
// find attributes
while (*P != '>') {
- if (_is_white_space(*P))
+ if (_is_white_space(*P)) {
++P;
- else {
+ } else {
if (*P != '/') {
// we've got an attribute
// read the attribute names
const char *attributeNameBegin = P;
- while (!_is_white_space(*P) && *P != '=')
+ while (!_is_white_space(*P) && *P != '=') {
++P;
+ }
const char *attributeNameEnd = P;
++P;
// read the attribute value
// check for quotes and single quotes, thx to murphy
- while ((*P != '\"') && (*P != '\'') && *P)
+ while ((*P != '\"') && (*P != '\'') && *P) {
++P;
+ }
- if (!*P) // malformatted xml file
+ if (!*P) { // malformatted xml file
return;
+ }
const char attributeQuoteChar = *P;
++P;
const char *attributeValueBegin = P;
- while (*P != attributeQuoteChar && *P)
+ while (*P != attributeQuoteChar && *P) {
++P;
+ }
- if (!*P) // malformatted xml file
+ if (!*P) { // malformatted xml file
return;
+ }
const char *attributeValueEnd = P;
++P;
@@ -304,21 +319,23 @@ void XMLParser::_parse_opening_xml_element() {
}
void XMLParser::_parse_current_node() {
-
char *start = P;
node_offset = P - data;
// more forward until '<' found
- while (*P != '<' && *P)
+ while (*P != '<' && *P) {
++P;
+ }
- if (!*P)
+ if (!*P) {
return;
+ }
if (P - start > 0) {
// we found some text, store it
- if (_set_text(start, P))
+ if (_set_text(start, P)) {
return;
+ }
}
++P;
@@ -332,8 +349,9 @@ void XMLParser::_parse_current_node() {
_ignore_definition();
break;
case '!':
- if (!_parse_cdata())
+ if (!_parse_cdata()) {
_parse_comment();
+ }
break;
default:
_parse_opening_xml_element();
@@ -342,12 +360,10 @@ void XMLParser::_parse_current_node() {
}
uint64_t XMLParser::get_node_offset() const {
-
return node_offset;
};
Error XMLParser::seek(uint64_t p_pos) {
-
ERR_FAIL_COND_V(!data, ERR_FILE_EOF);
ERR_FAIL_COND_V(p_pos >= length, ERR_FILE_EOF);
@@ -357,7 +373,6 @@ Error XMLParser::seek(uint64_t p_pos) {
};
void XMLParser::_bind_methods() {
-
ClassDB::bind_method(D_METHOD("read"), &XMLParser::read);
ClassDB::bind_method(D_METHOD("get_node_type"), &XMLParser::get_node_type);
ClassDB::bind_method(D_METHOD("get_node_name"), &XMLParser::get_node_name);
@@ -386,7 +401,6 @@ void XMLParser::_bind_methods() {
};
Error XMLParser::read() {
-
// if not end reached, parse the node
if (P && (P - data) < (int64_t)length - 1 && *P != 0) {
_parse_current_node();
@@ -397,11 +411,10 @@ Error XMLParser::read() {
}
XMLParser::NodeType XMLParser::get_node_type() {
-
return node_type;
}
-String XMLParser::get_node_data() const {
+String XMLParser::get_node_data() const {
ERR_FAIL_COND_V(node_type != NODE_TEXT, "");
return node_name;
}
@@ -410,31 +423,32 @@ String XMLParser::get_node_name() const {
ERR_FAIL_COND_V(node_type == NODE_TEXT, "");
return node_name;
}
-int XMLParser::get_attribute_count() const {
+int XMLParser::get_attribute_count() const {
return attributes.size();
}
-String XMLParser::get_attribute_name(int p_idx) const {
+String XMLParser::get_attribute_name(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, attributes.size(), "");
return attributes[p_idx].name;
}
-String XMLParser::get_attribute_value(int p_idx) const {
+String XMLParser::get_attribute_value(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, attributes.size(), "");
return attributes[p_idx].value;
}
-bool XMLParser::has_attribute(const String &p_name) const {
+bool XMLParser::has_attribute(const String &p_name) const {
for (int i = 0; i < attributes.size(); i++) {
- if (attributes[i].name == p_name)
+ if (attributes[i].name == p_name) {
return true;
+ }
}
return false;
}
-String XMLParser::get_attribute_value(const String &p_name) const {
+String XMLParser::get_attribute_value(const String &p_name) const {
int idx = -1;
for (int i = 0; i < attributes.size(); i++) {
if (attributes[i].name == p_name) {
@@ -449,7 +463,6 @@ String XMLParser::get_attribute_value(const String &p_name) const {
}
String XMLParser::get_attribute_value_safe(const String &p_name) const {
-
int idx = -1;
for (int i = 0; i < attributes.size(); i++) {
if (attributes[i].name == p_name) {
@@ -458,17 +471,17 @@ String XMLParser::get_attribute_value_safe(const String &p_name) const {
}
}
- if (idx < 0)
+ if (idx < 0) {
return "";
+ }
return attributes[idx].value;
}
-bool XMLParser::is_empty() const {
+bool XMLParser::is_empty() const {
return node_empty;
}
Error XMLParser::open_buffer(const Vector<uint8_t> &p_buffer) {
-
ERR_FAIL_COND_V(p_buffer.size() == 0, ERR_INVALID_DATA);
if (data) {
@@ -484,7 +497,6 @@ Error XMLParser::open_buffer(const Vector<uint8_t> &p_buffer) {
}
Error XMLParser::open(const String &p_path) {
-
Error err;
FileAccess *file = FileAccess::open(p_path, FileAccess::READ, &err);
@@ -508,10 +520,10 @@ Error XMLParser::open(const String &p_path) {
}
void XMLParser::skip_section() {
-
// skip if this element is empty anyway.
- if (is_empty())
+ if (is_empty()) {
return;
+ }
// read until we've reached the last element in this section
int tagcount = 1;
@@ -520,15 +532,16 @@ void XMLParser::skip_section() {
if (get_node_type() == XMLParser::NODE_ELEMENT &&
!is_empty()) {
++tagcount;
- } else if (get_node_type() == XMLParser::NODE_ELEMENT_END)
+ } else if (get_node_type() == XMLParser::NODE_ELEMENT_END) {
--tagcount;
+ }
}
}
void XMLParser::close() {
-
- if (data)
+ if (data) {
memdelete_arr(data);
+ }
data = nullptr;
length = 0;
P = nullptr;
@@ -538,7 +551,6 @@ void XMLParser::close() {
}
int XMLParser::get_current_line() const {
-
return 0;
}
@@ -549,8 +561,9 @@ XMLParser::XMLParser() {
special_characters.push_back("\"quot;");
special_characters.push_back("'apos;");
}
-XMLParser::~XMLParser() {
- if (data)
+XMLParser::~XMLParser() {
+ if (data) {
memdelete_arr(data);
+ }
}