Debian Codegraph @

core/io/xml_parser.h

raw symbols SCIP
1 /**************************************************************************/
2 /* xml_parser.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
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 Based on irrXML (see their zlib license). Added mainly for compatibility with their Collada loader.
39 */
40
41 class XMLParser : public RefCounted {
42 GDCLASS(XMLParser, RefCounted);
43
44 public:
45 //! Enumeration of all supported source text file formats
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; // do not print error if doesn't exist
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);