1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-08 12:40:44 +00:00

Implement XMLParser.get_current_line()

This commit is contained in:
kobewi
2022-07-05 01:23:04 +02:00
parent 100d223736
commit 415c7dda37
3 changed files with 31 additions and 26 deletions

View File

@@ -72,11 +72,11 @@ void XMLParser::_parse_closing_xml_element() {
node_empty = false; node_empty = false;
attributes.clear(); attributes.clear();
++P; next_char();
const char *pBeginClose = P; const char *pBeginClose = P;
while (*P && *P != '>') { while (*P && *P != '>') {
++P; next_char();
} }
node_name = String::utf8(pBeginClose, (int)(P - pBeginClose)); node_name = String::utf8(pBeginClose, (int)(P - pBeginClose));
@@ -85,7 +85,7 @@ void XMLParser::_parse_closing_xml_element() {
#endif #endif
if (*P) { if (*P) {
++P; next_char();
} }
} }
@@ -95,12 +95,12 @@ void XMLParser::_ignore_definition() {
char *F = P; char *F = P;
// move until end marked with '>' reached // move until end marked with '>' reached
while (*P && *P != '>') { while (*P && *P != '>') {
++P; next_char();
} }
node_name.parse_utf8(F, P - F); node_name.parse_utf8(F, P - F);
if (*P) { if (*P) {
++P; next_char();
} }
} }
@@ -114,7 +114,7 @@ bool XMLParser::_parse_cdata() {
// skip '<![CDATA[' // skip '<![CDATA['
int count = 0; int count = 0;
while (*P && count < 8) { while (*P && count < 8) {
++P; next_char();
++count; ++count;
} }
@@ -134,7 +134,7 @@ bool XMLParser::_parse_cdata() {
cDataEnd = P - 2; cDataEnd = P - 2;
} }
++P; next_char();
} }
if (cDataEnd) { if (cDataEnd) {
@@ -180,7 +180,7 @@ void XMLParser::_parse_comment() {
} else if (*P == '<') { } else if (*P == '<') {
++count; ++count;
} }
++P; next_char();
} }
if (count) { if (count) {
@@ -206,7 +206,7 @@ void XMLParser::_parse_opening_xml_element() {
// find end of element // find end of element
while (*P && *P != '>' && !_is_white_space(*P)) { while (*P && *P != '>' && !_is_white_space(*P)) {
++P; next_char();
} }
const char *endName = P; const char *endName = P;
@@ -214,7 +214,7 @@ void XMLParser::_parse_opening_xml_element() {
// find attributes // find attributes
while (*P && *P != '>') { while (*P && *P != '>') {
if (_is_white_space(*P)) { if (_is_white_space(*P)) {
++P; next_char();
} else { } else {
if (*P != '/') { if (*P != '/') {
// we've got an attribute // we've got an attribute
@@ -223,7 +223,7 @@ void XMLParser::_parse_opening_xml_element() {
const char *attributeNameBegin = P; const char *attributeNameBegin = P;
while (*P && !_is_white_space(*P) && *P != '=') { while (*P && !_is_white_space(*P) && *P != '=') {
++P; next_char();
} }
if (!*P) { if (!*P) {
@@ -231,12 +231,12 @@ void XMLParser::_parse_opening_xml_element() {
} }
const char *attributeNameEnd = P; const char *attributeNameEnd = P;
++P; next_char();
// read the attribute value // read the attribute value
// check for quotes and single quotes, thx to murphy // check for quotes and single quotes, thx to murphy
while ((*P != '\"') && (*P != '\'') && *P) { while ((*P != '\"') && (*P != '\'') && *P) {
++P; next_char();
} }
if (!*P) { // malformatted xml file if (!*P) { // malformatted xml file
@@ -245,16 +245,16 @@ void XMLParser::_parse_opening_xml_element() {
const char attributeQuoteChar = *P; const char attributeQuoteChar = *P;
++P; next_char();
const char *attributeValueBegin = P; const char *attributeValueBegin = P;
while (*P != attributeQuoteChar && *P) { while (*P != attributeQuoteChar && *P) {
++P; next_char();
} }
const char *attributeValueEnd = P; const char *attributeValueEnd = P;
if (*P) { if (*P) {
++P; next_char();
} }
Attribute attr; Attribute attr;
@@ -268,7 +268,7 @@ void XMLParser::_parse_opening_xml_element() {
attributes.push_back(attr); attributes.push_back(attr);
} else { } else {
// tag is closed directly // tag is closed directly
++P; next_char();
node_empty = true; node_empty = true;
break; break;
} }
@@ -288,7 +288,7 @@ void XMLParser::_parse_opening_xml_element() {
#endif #endif
if (*P) { if (*P) {
++P; next_char();
} }
} }
@@ -298,7 +298,7 @@ void XMLParser::_parse_current_node() {
// more forward until '<' found // more forward until '<' found
while (*P != '<' && *P) { while (*P != '<' && *P) {
++P; next_char();
} }
if (P - start > 0) { if (P - start > 0) {
@@ -312,7 +312,7 @@ void XMLParser::_parse_current_node() {
return; return;
} }
++P; next_char();
// based on current token, parse and report next element // based on current token, parse and report next element
switch (*P) { switch (*P) {
@@ -487,6 +487,7 @@ Error XMLParser::open(const String &p_path) {
file->get_buffer((uint8_t *)data, length); file->get_buffer((uint8_t *)data, length);
data[length] = 0; data[length] = 0;
P = data; P = data;
current_line = 0;
return OK; return OK;
} }
@@ -523,10 +524,7 @@ void XMLParser::close() {
} }
int XMLParser::get_current_line() const { int XMLParser::get_current_line() const {
return 0; return current_line;
}
XMLParser::XMLParser() {
} }
XMLParser::~XMLParser() { XMLParser::~XMLParser() {

View File

@@ -68,6 +68,7 @@ private:
char *data = nullptr; char *data = nullptr;
char *P = nullptr; char *P = nullptr;
uint64_t length = 0; uint64_t length = 0;
uint64_t current_line = 0;
String node_name; String node_name;
bool node_empty = false; bool node_empty = false;
NodeType node_type = NODE_NONE; NodeType node_type = NODE_NONE;
@@ -88,6 +89,13 @@ private:
void _parse_opening_xml_element(); void _parse_opening_xml_element();
void _parse_current_node(); void _parse_current_node();
_FORCE_INLINE_ void next_char() {
if (*P == '\n') {
current_line++;
}
P++;
}
static void _bind_methods(); static void _bind_methods();
public: public:
@@ -113,7 +121,6 @@ public:
void close(); void close();
XMLParser();
~XMLParser(); ~XMLParser();
}; };

View File

@@ -32,7 +32,7 @@
<method name="get_current_line" qualifiers="const"> <method name="get_current_line" qualifiers="const">
<return type="int" /> <return type="int" />
<description> <description>
Gets the current line in the parsed file (currently not implemented). Gets the current line in the parsed file, counting from 0.
</description> </description>
</method> </method>
<method name="get_named_attribute_value" qualifiers="const"> <method name="get_named_attribute_value" qualifiers="const">