| 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 |
#pragma once |
| 32 |
|
| 33 |
#include "core/object/ref_counted.h" |
| 34 |
#include "core/string/ustring.h" |
| 35 |
#include "core/templates/vector.h" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class XMLParser : public RefCounted { |
| 42 |
GDCLASS(XMLParser, RefCounted); |
| 43 |
|
| 44 |
public: |
| 45 |
|
| 46 |
enum SourceFormat { |
| 47 |
SOURCE_ASCII, |
| 48 |
SOURCE_UTF8, |
| 49 |
SOURCE_UTF16_BE, |
| 50 |
SOURCE_UTF16_LE, |
| 51 |
SOURCE_UTF32_BE, |
| 52 |
SOURCE_UTF32_LE |
| 53 |
}; |
| 54 |
|
| 55 |
enum NodeType { |
| 56 |
NODE_NONE, |
| 57 |
NODE_ELEMENT, |
| 58 |
NODE_ELEMENT_END, |
| 59 |
NODE_TEXT, |
| 60 |
NODE_COMMENT, |
| 61 |
NODE_CDATA, |
| 62 |
NODE_UNKNOWN |
| 63 |
}; |
| 64 |
|
| 65 |
private: |
| 66 |
char *data_copy = nullptr; |
| 67 |
const char *data = nullptr; |
| 68 |
const char *P = nullptr; |
| 69 |
uint64_t length = 0; |
| 70 |
uint64_t current_line = 0; |
| 71 |
String node_name; |
| 72 |
bool node_empty = false; |
| 73 |
NodeType node_type = NODE_NONE; |
| 74 |
uint64_t node_offset = 0; |
| 75 |
|
| 76 |
struct Attribute { |
| 77 |
String name; |
| 78 |
String value; |
| 79 |
}; |
| 80 |
|
| 81 |
Vector<Attribute> attributes; |
| 82 |
|
| 83 |
bool _set_text(const char *start, const char *end); |
| 84 |
void _parse_closing_xml_element(); |
| 85 |
void _ignore_definition(); |
| 86 |
bool _parse_cdata(); |
| 87 |
void _parse_comment(); |
| 88 |
void _parse_opening_xml_element(); |
| 89 |
void _parse_current_node(); |
| 90 |
|
| 91 |
_FORCE_INLINE_ void next_char() { |
| 92 |
if (*P == '\n') { |
| 93 |
current_line++; |
| 94 |
} |
| 95 |
P++; |
| 96 |
} |
| 97 |
|
| 98 |
static void _bind_methods(); |
| 99 |
|
| 100 |
public: |
| 101 |
Error read(); |
| 102 |
NodeType get_node_type(); |
| 103 |
String get_node_name() const; |
| 104 |
String get_node_data() const; |
| 105 |
uint64_t get_node_offset() const; |
| 106 |
int get_attribute_count() const; |
| 107 |
String get_attribute_name(int p_idx) const; |
| 108 |
String get_attribute_value(int p_idx) const; |
| 109 |
bool has_attribute(const String &p_name) const; |
| 110 |
String get_named_attribute_value(const String &p_name) const; |
| 111 |
String get_named_attribute_value_safe(const String &p_name) const; |
| 112 |
bool is_empty() const; |
| 113 |
int get_current_line() const; |
| 114 |
|
| 115 |
void skip_section(); |
| 116 |
Error seek(uint64_t p_pos); |
| 117 |
|
| 118 |
Error open(const String &p_path); |
| 119 |
Error open_buffer(const Vector<uint8_t> &p_buffer); |
| 120 |
Error _open_buffer(const uint8_t *p_buffer, size_t p_size); |
| 121 |
|
| 122 |
void close(); |
| 123 |
|
| 124 |
~XMLParser(); |
| 125 |
}; |
| 126 |
|
| 127 |
VARIANT_ENUM_CAST(XMLParser::NodeType); |