-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.h
456 lines (406 loc) · 13.7 KB
/
xml.h
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// MIT License
// Copyright (c) 2024 Vlad Krupinskii <[email protected]>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// xml.h - Header-only C/C++ library to parse XML.
#ifndef XML_H
#define XML_H
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
// The main object to interact with parsed XML nodes. Represents single XML tag.
typedef struct XMLNode {
// Tag string.
char *tag;
// Inner text of the tag. NULL if tag has no inner text.
char *text;
// List of tag attributes. Check "node->attrs->len" if it has items.
struct XMLList *attrs;
// Node's parent node. NULL for the root node.
struct XMLNode *parent;
// List of tag's sub-tags. Check "node->children->len" if it has items.
struct XMLList *children;
} XMLNode;
// Tags attribute containing key and value.
// Like: <tag foo_attr="foo_value" bar_attr="bar_value" />
typedef struct XMLAttr {
char *key;
char *value;
} XMLAttr;
// Simple dynamic list that only can grow in size.
// Used in XMLNode for list of children and list of tag attributes.
typedef struct XMLList {
// Capacity of the list in bytes.
size_t size;
// Length of the list.
size_t len;
// List of pointers to list items.
void **data;
} XMLList;
// Parse XML string and return root XMLNode.
// Returns NULL for error.
static inline XMLNode *xml_parse_string(const char *xml);
// Parse XML file for given path and return root XMLNode.
// Returns NULL for error.
static inline XMLNode *xml_parse_file(const char *path);
// Get child of the node at index.
// Returns NULL if not found.
static inline XMLNode *xml_node_child_at(XMLNode *node, size_t idx);
// Find xml tag by path like "div/p/href"
static inline XMLNode *xml_node_find_by_path(XMLNode *root, const char *path, bool exact);
// Get first matching tag in the tree.
// If exact is "false" - will return first tag which name contains "tag"
static inline XMLNode *xml_node_find_tag(XMLNode *node, const char *tag, bool exact);
// Get value of the tag attribute.
// Returns NULL if not found.
static inline const char *xml_node_attr(XMLNode *node, const char *attr_key);
// Cleanup node and all it's children.
static inline void xml_node_free(XMLNode *node);
// Macro for looping node children
#define for_child(node, idx) for (size_t idx = 0; idx < node->children->len; idx++)
// --------------------------------------------------------------------------------------------- //
// FUNCTIONS IMPLEMENTATIONS //
// --------------------------------------------------------------------------------------------- //
// Print debug message. Only if XML_H_DEBUG is defined.
#ifdef XML_H_DEBUG
#define LOG_DEBUG(format, ...) fprintf(stderr, "[xml.h] " format "\n", ##__VA_ARGS__)
#else
#define LOG_DEBUG(format, ...)
#endif
// ------ LIST ------ //
// Create new dynamic array
static inline XMLList *xml_list_new() {
XMLList *list = (XMLList *)malloc(sizeof(XMLList));
list->len = 0;
list->size = 1;
list->data = (void **)malloc(sizeof(void *) * list->size);
return list;
}
// Add element to the end of the array. Grow if needed.
static inline void xml_list_add(XMLList *list, void *data) {
if (list->len >= list->size)
list->data = (void **)realloc(list->data, ++list->size * sizeof(void *));
list->data[list->len++] = data;
}
// ------ NODE ------ //
// Create new XMLNode. Free with xml_node_free().
static XMLNode *xml_node_new(XMLNode *parent) {
XMLNode *node = (XMLNode *)malloc(sizeof(XMLNode));
node->parent = parent;
node->tag = NULL;
node->text = NULL;
node->children = xml_list_new();
node->attrs = xml_list_new();
if (parent)
xml_list_add(parent->children, node);
return node;
}
// Add XMLAttr to the node's list of attributes
static void xml_node_add_attr(XMLNode *node, char *key, char *value) {
XMLAttr *attr = (XMLAttr *)malloc(sizeof(XMLAttr));
attr->key = key;
attr->value = value;
xml_list_add(node->attrs, attr);
}
static inline XMLNode *xml_node_child_at(XMLNode *node, size_t index) {
if (index > node->children->len - 1)
return NULL;
return (XMLNode *)node->children->data[index];
}
static inline XMLNode *xml_node_find_tag(XMLNode *node, const char *tag, bool exact) {
if (!node || !tag)
return NULL; // Invalid input
// Check if the current node matches the tag
if (node->tag &&
((exact && strcmp(node->tag, tag) == 0) || (!exact && strstr(node->tag, tag) != NULL)))
return node;
// Recursively search through the children of the node
for (size_t i = 0; i < node->children->len; i++) {
XMLNode *child = (XMLNode *)node->children->data[i];
XMLNode *result = xml_node_find_tag(child, tag, exact);
if (result)
return result; // Return the first match found
}
// No match found in this subtree
return NULL;
}
static inline XMLNode *xml_node_find_by_path(XMLNode *root, const char *path, bool exact) {
if (!root || !path)
return NULL; // Invalid input
char *tokenized_path = strdup(path); // Copy to avoid modifying the input
if (!tokenized_path)
return NULL; // Memory allocation failure
char *segment = strtok(tokenized_path, "/");
XMLNode *current = root;
while (segment && current) {
bool found = false;
for (size_t i = 0; i < current->children->len; i++) {
XMLNode *child = (XMLNode *)current->children->data[i];
if ((exact && strcmp(child->tag, segment) == 0) ||
(!exact && strstr(child->tag, segment) != NULL)) {
current = child;
found = true;
break;
}
}
if (!found) {
free(tokenized_path);
return NULL; // Path not found
}
segment = strtok(NULL, "/");
}
free(tokenized_path);
return current;
}
static inline const char *xml_node_attr(XMLNode *node, const char *attr_key) {
if (!node || !attr_key)
return NULL;
for (size_t i = 0; i < node->attrs->len; i++) {
XMLAttr *attr = (XMLAttr *)node->attrs->data[i];
if (!strcmp(attr->key, attr_key))
return attr->value;
}
return NULL;
}
static inline void xml_node_free(XMLNode *node) {
if (node == NULL)
return;
// Free text
if (node->text)
free(node->text);
// Free the attributes
for (size_t i = 0; i < node->attrs->len; i++) {
XMLAttr *attr = (XMLAttr *)node->attrs->data[i];
free(attr->key);
free(attr->value);
free(attr);
}
free(node->attrs->data);
free(node->attrs);
// Recursively free the children
for (size_t i = 0; i < node->children->len; i++)
xml_node_free((XMLNode *)node->children->data[i]);
free(node->children->data);
free(node->children);
// Free the tag
if (node->tag)
free(node->tag);
// Free the node itself
free(node);
}
// ------ PARSING FUNCTIONS ------ //
static void strip_new_lines(char *str) {
char *start = str;
while (isspace((unsigned char)*start))
start++;
char *end = str + strlen(str) - 1;
while (end > start && isspace((unsigned char)*end))
end--;
*(end + 1) = '\0';
if (start != str)
memmove(str, start, end - start + 2);
}
static inline XMLNode *xml_parse_string(const char *xml) {
LOG_DEBUG("Parse string");
XMLNode *root = xml_node_new(NULL);
XMLNode *curr_node = root;
size_t idx = 0;
while (xml[idx] != '\0') {
if (xml[idx] == '<') {
// Processing instruction
if (xml[idx + 1] == '?') {
LOG_DEBUG("Parse processing instruction");
int count = 0; // Count for inner '<' and '>' brackets
while (true) {
if (xml[idx] == '<')
count++;
else if (xml[idx] == '>') {
count--;
if (count <= 0) {
idx++;
break;
}
}
idx++;
}
idx++; // Move past '>' character
continue;
}
// DOCTYPE
if (xml[idx + 1] == '!') {
// Comment
if (xml[idx + 2] == '-' && xml[idx + 3] == '-') {
LOG_DEBUG("Parse comment");
while (xml[idx] != '>')
idx++;
idx++; // Move past '>' character
continue;
}
}
// End tag
if (xml[idx + 1] == '/') {
LOG_DEBUG("Parse end tag: %s", curr_node->tag);
while (xml[idx] != '>')
idx++;
idx++; // Move past '>' character
curr_node = curr_node->parent;
continue;
}
curr_node = xml_node_new(curr_node);
// Start tag
LOG_DEBUG("Start parsing tag");
XMLNode *new_current_node = curr_node;
idx++; // Move past '<' character
size_t tag_start = idx;
while (true) {
// Self-closing tag without attributes
if (xml[idx] == '/') {
LOG_DEBUG("Tag is self-closing without attributes");
while (xml[idx] != '>')
idx++;
new_current_node = curr_node->parent;
break;
}
// Space after tag name
if (isspace(xml[idx])) {
// Set tag name
size_t tag_len = idx - tag_start;
curr_node->tag = (char *)malloc(sizeof(char) * tag_len + 1);
strncpy(curr_node->tag, xml + tag_start, tag_len);
curr_node->tag[tag_len] = '\0';
LOG_DEBUG("Parsed start tag: %s", curr_node->tag);
// Skip white space
while (isspace(xml[idx]))
idx++;
// Self-closing tag without attributes
if (xml[idx] == '/') {
LOG_DEBUG("Tag is self-closing without attributes");
while (xml[idx] != '>')
idx++;
new_current_node = curr_node->parent;
break;
}
// Tag attributes
else {
while (xml[idx] != '>') {
// Skip white spaces
if (isspace(xml[idx])) {
idx++;
continue;
}
// Self-closing tag with attributes
if (xml[idx] == '/') {
LOG_DEBUG("Tag is self-closing");
new_current_node = curr_node->parent;
idx++;
continue;
}
// Get attribute key
size_t key_start = idx;
while (xml[idx] != '=')
idx++;
size_t key_len = idx - key_start;
char *attr_key = (char *)malloc(sizeof(char) * key_len + 1);
strncpy(attr_key, xml + key_start, key_len);
attr_key[key_len] = '\0';
idx += 2; // Skip '=' and '"'
// Get attribute value
size_t value_start = idx;
while (xml[idx] != '"')
idx++;
size_t value_len = idx - value_start;
idx++; // Skip '"'
char *attr_value = (char *)malloc(sizeof(char) * value_len + 1);
strncpy(attr_value, xml + value_start, value_len);
attr_value[value_len] = '\0';
// Add attribute to node
xml_node_add_attr(curr_node, attr_key, attr_value);
LOG_DEBUG("Parse attribute: %s='%s'", attr_key, attr_value);
}
break;
}
} else if (xml[idx] == '>') {
// Set tag name
size_t tag_len = idx - tag_start;
curr_node->tag = (char *)malloc(sizeof(char) * tag_len + 1);
strncpy(curr_node->tag, xml + tag_start, tag_len);
curr_node->tag[tag_len] = '\0';
LOG_DEBUG("Parsed start tag with no attributes: %s", curr_node->tag);
break;
}
idx++;
}
idx++; // Move past '>' character
curr_node = new_current_node;
continue;
}
// Inner text
if (!isspace(xml[idx])) {
LOG_DEBUG("Start parsing text");
size_t text_start = idx;
while (true) {
if (xml[idx] == '<' && xml[idx + 1] == '/')
break;
idx++;
}
size_t text_len = idx - text_start;
curr_node->text = (char *)malloc(sizeof(char) * text_len + 1);
strncpy(curr_node->text, xml + text_start, text_len);
curr_node->text[text_len] = '\0';
strip_new_lines(curr_node->text);
LOG_DEBUG("End parsing text: %s", curr_node->text);
} else {
idx++;
}
}
LOG_DEBUG("Done parsing string");
return root;
}
static inline XMLNode *xml_parse_file(const char *path) {
FILE *file = fopen(path, "rb");
if (!file)
return NULL;
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = (char *)malloc(file_size + 1);
if (!buffer) {
fclose(file);
return NULL;
}
size_t bytes_read = fread(buffer, 1, file_size, file);
if (bytes_read != file_size) {
free(buffer);
fclose(file);
return NULL;
}
buffer[file_size] = '\0';
fclose(file);
XMLNode *node = xml_parse_string(buffer);
free(buffer);
return node;
}
#ifdef __cplusplus
}
#endif
#endif // XML_H