-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_parser.h
277 lines (200 loc) · 7.17 KB
/
xml_parser.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
/*
MIT License
Copyright (c) 2018 Velli20
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.
*/
#ifndef xml_parser_h
#define xml_parser_h
// Includes.
#include <stddef.h>
#include <inttypes.h>
// Error codes.
#if !defined(ENOMEM)
#define ENOMEM 12
#endif
#if !defined(EINVAL)
#define EINVAL 22
#endif
//#define PARSER_INCLUDE_LOG
//#define PARSER_WITH_DYNAMIC_NAMES
#define PARSER_MAX_NAME_STRING_LENGTH 128
#define PARSER_MAX_VALUE_STRING_LENGTH 128
// Defines.
#define PARSER_RESULT_ERROR 0x01
#define PARSER_RESULT_OUT_OF_MEMORY 0x02
#define PARSER_UNKNOWN_INDEX -1
#define PARSER_ELEMENT_CONTENT_TYPE_NONE 0x00
#define PARSER_ELEMENT_CONTENT_TYPE_STRING 0x01
#define PARSER_ELEMENT_CONTENT_TYPE_ELEMENT 0x02
#define PARSER_ELEMENT_NAME_TYPE_INDEX 0x10
#define PARSER_ELEMENT_NAME_TYPE_STRING 0x20
#define PARSER_ELEMENT_NAME_TYPE_NONE 0x40
#define PARSER_ATTRIBUTE_VALUE_TYPE_STRING 0x01
#define PARSER_ATTRIBUTE_VALUE_TYPE_INTEGER 0x02
#define PARSER_ATTRIBUTE_VALUE_TYPE_FLOAT 0x04
#define PARSER_ATTRIBUTE_NAME_TYPE_INDEX 0x10
#define PARSER_ATTRIBUTE_NAME_TYPE_STRING 0x20
#define PARSER_ATTRIBUTE_NAME_TYPE_NONE 0x40
// Types.
typedef int32_t PARSER_ERROR;
typedef int32_t PARSER_INT;
typedef float PARSER_FLOAT;
typedef size_t PARSER_SIZE;
typedef char PARSER_CHAR;
typedef struct parser_xml_name
{
const char* name;
}
PARSER_XML_NAME;
typedef enum
{
ATTRIBUTE_TYPE_INTEGER = 0,
ATTRIBUTE_TYPE_FLOAT,
ATTRIBUTE_TYPE_STRING,
ATTRIBUTE_TYPE_UNKNOWN
}
PARSER_ATTRIBUTE_TYPE;
typedef struct parser_attribute
{
PARSER_INT attribute_type;
PARSER_INT pad;
union ATTRIBUTE_VALUE
{
char* string_ptr;
PARSER_INT int_value;
PARSER_FLOAT float_value;
}
attr_val;
// Attribute name either can be dynamically allocated string
// or an integer that represents index in the
// PARSER_XML_NAME -list that is passed optionally
// at the beginning of parsing.
union ATTRIBUTE_NAME
{
PARSER_CHAR* name_string;
PARSER_INT attribute_index;
}
attr_name;
struct parser_attribute* next_attribute;
}
PARSER_ATTRIBUTE;
typedef struct parser_string
{
PARSER_SIZE buffer_size;
PARSER_CHAR* buffer;
struct parser_string* next_string;
}
PARSER_STRING;
typedef struct parser_string_content
{
PARSER_STRING* first_string;
PARSER_STRING* last_string;
}
PARSER_STRING_CONTENT;
typedef struct parser_child_element
{
struct parser_element* first_element;
struct parser_element* last_element;
}
PARSER_CHILD_ELEMENT;
// parser_element
typedef struct parser_element
{
struct parser_element* next_element;
struct parser_element* parent_element;
struct parser_attribute* first_attribute;
struct parser_attribute* last_attribute;
// Element inner content.
struct parser_string_content child_string;
struct parser_child_element child_element;
// Element name.
union PARSER_ELEMENT_NAME
{
PARSER_INT name_index;
#if defined(PARSER_WITH_DYNAMIC_NAMES)
PARSER_CHAR* name_string;
#endif
}
elem_name;
PARSER_INT content_type;
}
PARSER_ELEMENT;
// parser_state
typedef struct parser_state
{
PARSER_ELEMENT* element;
PARSER_ELEMENT* parent_element;
PARSER_INT flags;
PARSER_INT name_buf_pos;
PARSER_INT value_buf_pos;
PARSER_INT pad_1;
PARSER_CHAR previous_char;
PARSER_CHAR current_char;
PARSER_CHAR next_char;
PARSER_CHAR pad_2;
PARSER_INT pad_3;
PARSER_CHAR temp_name_buffer[PARSER_MAX_NAME_STRING_LENGTH];
PARSER_CHAR temp_value_buffer[PARSER_MAX_VALUE_STRING_LENGTH];
}
PARSER_STATE;
// parser_xml
typedef struct parser_xml
{
PARSER_STATE* state;
const PARSER_XML_NAME* element_name_list;
const PARSER_XML_NAME* attribute_name_list;
struct parser_element* first_element;
struct parser_element* last_element;
PARSER_INT element_name_list_length;
PARSER_INT attribute_name_list_length;
}
PARSER_XML;
#define PARSER_GET_CHILD_STRING(PARENT_ELEMENT) ((((PARSER_ELEMENT*)PARENT_ELEMENT)->child_string.first_string))
// parser_malloc
void* parser_malloc(size_t size);
// parser_free
void parser_free(void* ptr);
// parser_begin
PARSER_XML* parser_begin(const PARSER_XML_NAME* element_name_list,
PARSER_INT element_name_list_length,
const PARSER_XML_NAME* attribute_name_list,
PARSER_INT attribute_name_list_length);
PARSER_ERROR parser_finalize(PARSER_XML* xml);
PARSER_ERROR parser_append(PARSER_XML* xml,
const PARSER_CHAR* xml_string,
PARSER_INT xml_string_length);
// parser_free_xml
PARSER_ERROR parser_free_xml(PARSER_XML* xml);
// parser_find_element
const PARSER_ELEMENT* parser_find_element(const PARSER_XML* xml,
const PARSER_ELEMENT* offset,
PARSER_INT max_depth,
const PARSER_CHAR* element_name);
const PARSER_ATTRIBUTE* parser_find_attribute(const PARSER_XML* xml,
const PARSER_ELEMENT* element,
const PARSER_ATTRIBUTE* offset,
const PARSER_CHAR* attribute_name);
const PARSER_ELEMENT* parser_get_next_element(const PARSER_ELEMENT* element);
const PARSER_ELEMENT* parser_get_first_child_element(const PARSER_ELEMENT* element);
PARSER_ATTRIBUTE_TYPE parser_get_attribute_type(const PARSER_ATTRIBUTE* attribute);
PARSER_ERROR parser_get_attribute_int_value(const PARSER_ATTRIBUTE* attribute,
PARSER_INT* int_value_ptr);
PARSER_ERROR parser_get_attribute_string_value(const PARSER_ATTRIBUTE* attribute,
const PARSER_CHAR** string_value_ptr);
const PARSER_ATTRIBUTE* parser_get_first_element_attribute(const PARSER_ELEMENT* element);
const PARSER_ATTRIBUTE* parser_get_next_element_attribute(const PARSER_ATTRIBUTE* attribute);
#endif