-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.c3
255 lines (225 loc) · 5.29 KB
/
element.c3
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
module xml;
import std::collections::list;
import std::collections::map;
def XmlElementID = usz;
def ElementListRef = List(<XmlElement*>);
def XmlElementSearch = fn bool (XmlElement*);
enum XmlValueType
{
NONE,
TEXT,
ID,
}
struct XmlValue
{
XmlValueType type;
union
{
String text;
XmlElementID id;
}
}
def ValueList = List(<XmlValue>);
enum XmlNodeType
{
UNKNOWN,
ATTRIBUTE,
CDATA,
COMMENT,
DECLARATION,
DOCUMENT_TYPE,
DOCUMENT,
DOCUMENT_FRAGMENT,
ELEMENT,
ENTITY,
NOTATION,
PROCESSING,
TEXT,
}
struct XmlNode
{
XmlAttribute attributes;
usz num_children;
usz total_children;
usz depth;
}
struct XmlElement
{
inline XmlNode node;
String identity;
ValueList values;
XmlElementID id;
XmlElementID parent;
}
struct XmlIterator
{
ElementList* elements;
usz parent;
usz current;
usz previous;
String name;
XmlElementSearch search;
usz num_children;
usz total_children;
usz current_index;
usz start_id;
bool recursive;
}
fn XmlAttribute* XmlElement.get_atributes(&self)
{
return &self.attributes;
}
fn XmlIterator XmlElement.iterator_by_children(&self, XmlDocument* doc)
{
usz parent = self.id;
usz child = parent + 1; // enter children
return XmlIterator
{
.elements = &doc.elements,
.parent = parent,
.current = child,
.previous = child,
.current_index = child,
.num_children = self.num_children,
.total_children = self.total_children,
.start_id = parent,
};
}
fn XmlIterator XmlElement.iterator_by_element(&self, String name, XmlDocument* doc)
{
XmlIterator iterator = self.iterator_by_children(doc);
iterator.name = name;
return iterator;
}
fn XmlIterator XmlElement.iterator_by_search(
&self,
XmlElementSearch search,
XmlDocument* doc,
bool recursive = false,
) {
XmlIterator iterator = self.iterator_by_children(doc);
iterator.search = search;
iterator.recursive = recursive;
return iterator;
}
fn ElementListRef XmlElement.children_list(
&self,
XmlDocument* doc,
Allocator allocator = allocator::temp()
) {
ElementListRef list = *ElementListRef{}.new_init(self.num_children, allocator);
XmlIterator it = self.iterator_by_children(doc);
while (XmlElement* el = it.next())
{
list.push(el);
}
return list;
}
fn XmlElement* XmlElement.first_element(&self, XmlDocument* doc)
{
if (XmlElement* el = doc.elements.get_ref(self.id + 1)) return el;
return null;
}
fn XmlElement*! XmlElement.get_element(&self, String name, XmlDocument* doc)
{
XmlIterator elements = self.iterator_by_children(doc);
while (XmlElement* el = elements.next())
{
if (el.identity == name) return el;
}
return XmlError.ELEMENT_NOT_FOUND?;
}
fn XmlElement*! XmlElement.find_element(&self, XmlElementSearch search, XmlDocument* doc)
{
XmlIterator elements = self.iterator_by_children(doc);
while (XmlElement* el = elements.next())
{
if (search(el)) return el;
}
return XmlError.ELEMENT_NOT_FOUND?;
}
fn String! XmlElement.get_attribute(&self, String attribute) @inline =>
self.attributes.get(attribute)!;
fn bool XmlElement.has_attribute(&self, String attribute) @inline =>
self.attributes.has(attribute);
fn void XmlIterator.reset(&self)
{
usz child = self.parent + 1;
self.current = child;
self.previous = child;
self.current_index = child;
}
fn XmlElement* XmlIterator.next(&self)
{
if (self.recursive)
{
return self.next_recursive(self.current_index);
}
else
{
while (true)
{
XmlElement* el = self.elements.get_ref(self.current_index);
if (el == null)
{
return null;
}
if (el.parent != self.parent)
{
return null;
}
self.current_index += el.total_children > 0 ? el.total_children + 1 : 1;
self.previous = self.current;
self.current = el.id;
if (self.search != null)
{
if (self.search(el)) return el;
continue;
}
else if (self.name != "")
{
if (el.identity == self.name) return el;
continue;
}
else
{
return el;
}
}
}
}
fn XmlElement* XmlIterator.next_recursive(&self, usz current_id)
{
if (self.current_index > self.start_id + self.total_children)
{
return null;
}
XmlElement* el = self.elements.get_ref(current_id);
if (el == null)
{
return null;
}
self.previous = self.current;
self.current = el.id;
self.current_index += 1;
if (self.search != null)
{
if (self.search(el)) return el;
}
else if (self.name != "")
{
if (el.identity == self.name) return el;
}
else
{
return el; // If no search or name specified, return current element
}
return self.next_recursive(current_id + 1);
}
fn XmlElement* XmlIterator.previous(&self)
{
if (self.current_index == 0) return null;
self.current_index -= 1;
self.current = self.previous;
return self.next();
}