blob: f47807d3e1d4587f06463b11e076663973ba3b75 [file] [log] [blame]
Matt Mower64ffe732016-02-10 00:19:34 -06001#ifndef RAPIDXML_HPP_INCLUDED
2#define RAPIDXML_HPP_INCLUDED
3
4#define RAPIDXML_NO_EXCEPTIONS
5
6// Copyright (C) 2006, 2009 Marcin Kalicinski
7// Version 1.13
8// Revision $DateTime: 2009/05/13 01:46:17 $
9//! \file rapidxml.hpp This file contains rapidxml parser and DOM implementation
10
11// If standard library is disabled, user must provide implementations of required functions and typedefs
12#if !defined(RAPIDXML_NO_STDLIB)
13 #include <cstdlib> // For std::size_t
14 #include <cassert> // For assert
15 #include <new> // For placement new
16#endif
17
18// On MSVC, disable "conditional expression is constant" warning (level 4).
19// This warning is almost impossible to avoid with certain types of templated code
20#ifdef _MSC_VER
21 #pragma warning(push)
22 #pragma warning(disable:4127) // Conditional expression is constant
23#endif
24
25///////////////////////////////////////////////////////////////////////////
26// RAPIDXML_PARSE_ERROR
27
28#if defined(RAPIDXML_NO_EXCEPTIONS)
29
30#define RAPIDXML_PARSE_ERROR(what, where) { parse_error_handler(what, where); assert(0); }
31
32namespace rapidxml
33{
34 //! When exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS,
35 //! this function is called to notify user about the error.
36 //! It must be defined by the user.
37 //! <br><br>
38 //! This function cannot return. If it does, the results are undefined.
39 //! <br><br>
40 //! A very simple definition might look like that:
41 //! <pre>
42 //! void %rapidxml::%parse_error_handler(const char *what, void *where)
43 //! {
44 //! std::cout << "Parse error: " << what << "\n";
45 //! std::abort();
46 //! }
47 //! </pre>
48 //! \param what Human readable description of the error.
49 //! \param where Pointer to character data where error was detected.
50 void parse_error_handler(const char *what, void *where);
51}
52
53#else
54
55#include <exception> // For std::exception
56
57#define RAPIDXML_PARSE_ERROR(what, where) throw parse_error(what, where)
58
59namespace rapidxml
60{
61
62 //! Parse error exception.
63 //! This exception is thrown by the parser when an error occurs.
64 //! Use what() function to get human-readable error message.
65 //! Use where() function to get a pointer to position within source text where error was detected.
66 //! <br><br>
67 //! If throwing exceptions by the parser is undesirable,
68 //! it can be disabled by defining RAPIDXML_NO_EXCEPTIONS macro before rapidxml.hpp is included.
69 //! This will cause the parser to call rapidxml::parse_error_handler() function instead of throwing an exception.
70 //! This function must be defined by the user.
71 //! <br><br>
72 //! This class derives from <code>std::exception</code> class.
73 class parse_error: public std::exception
74 {
75
76 public:
77
78 //! Constructs parse error
79 parse_error(const char *what, void *where)
80 : m_what(what)
81 , m_where(where)
82 {
83 }
84
85 //! Gets human readable description of error.
86 //! \return Pointer to null terminated description of the error.
87 virtual const char *what() const throw()
88 {
89 return m_what;
90 }
91
92 //! Gets pointer to character data where error happened.
93 //! Ch should be the same as char type of xml_document that produced the error.
94 //! \return Pointer to location within the parsed string where error occured.
95 template<class Ch>
96 Ch *where() const
97 {
98 return reinterpret_cast<Ch *>(m_where);
99 }
100
101 private:
102
103 const char *m_what;
104 void *m_where;
105
106 };
107}
108
109#endif
110
111///////////////////////////////////////////////////////////////////////////
112// Pool sizes
113
114#ifndef RAPIDXML_STATIC_POOL_SIZE
115 // Size of static memory block of memory_pool.
116 // Define RAPIDXML_STATIC_POOL_SIZE before including rapidxml.hpp if you want to override the default value.
117 // No dynamic memory allocations are performed by memory_pool until static memory is exhausted.
118 #define RAPIDXML_STATIC_POOL_SIZE (64 * 1024)
119#endif
120
121#ifndef RAPIDXML_DYNAMIC_POOL_SIZE
122 // Size of dynamic memory block of memory_pool.
123 // Define RAPIDXML_DYNAMIC_POOL_SIZE before including rapidxml.hpp if you want to override the default value.
124 // After the static block is exhausted, dynamic blocks with approximately this size are allocated by memory_pool.
125 #define RAPIDXML_DYNAMIC_POOL_SIZE (64 * 1024)
126#endif
127
128#ifndef RAPIDXML_ALIGNMENT
129 // Memory allocation alignment.
130 // Define RAPIDXML_ALIGNMENT before including rapidxml.hpp if you want to override the default value, which is the size of pointer.
131 // All memory allocations for nodes, attributes and strings will be aligned to this value.
132 // This must be a power of 2 and at least 1, otherwise memory_pool will not work.
133 #define RAPIDXML_ALIGNMENT sizeof(void *)
134#endif
135
136namespace rapidxml
137{
138 // Forward declarations
139 template<class Ch> class xml_node;
140 template<class Ch> class xml_attribute;
141 template<class Ch> class xml_document;
142
143 //! Enumeration listing all node types produced by the parser.
144 //! Use xml_node::type() function to query node type.
145 enum node_type
146 {
147 node_document, //!< A document node. Name and value are empty.
148 node_element, //!< An element node. Name contains element name. Value contains text of first data node.
149 node_data, //!< A data node. Name is empty. Value contains data text.
150 node_cdata, //!< A CDATA node. Name is empty. Value contains data text.
151 node_comment, //!< A comment node. Name is empty. Value contains comment text.
152 node_declaration, //!< A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalone) are in node attributes.
153 node_doctype, //!< A DOCTYPE node. Name is empty. Value contains DOCTYPE text.
154 node_pi //!< A PI node. Name contains target. Value contains instructions.
155 };
156
157 ///////////////////////////////////////////////////////////////////////
158 // Parsing flags
159
160 //! Parse flag instructing the parser to not create data nodes.
161 //! Text of first data node will still be placed in value of parent element, unless rapidxml::parse_no_element_values flag is also specified.
162 //! Can be combined with other flags by use of | operator.
163 //! <br><br>
164 //! See xml_document::parse() function.
165 const int parse_no_data_nodes = 0x1;
166
167 //! Parse flag instructing the parser to not use text of first data node as a value of parent element.
168 //! Can be combined with other flags by use of | operator.
169 //! Note that child data nodes of element node take precendence over its value when printing.
170 //! That is, if element has one or more child data nodes <em>and</em> a value, the value will be ignored.
171 //! Use rapidxml::parse_no_data_nodes flag to prevent creation of data nodes if you want to manipulate data using values of elements.
172 //! <br><br>
173 //! See xml_document::parse() function.
174 const int parse_no_element_values = 0x2;
175
176 //! Parse flag instructing the parser to not place zero terminators after strings in the source text.
177 //! By default zero terminators are placed, modifying source text.
178 //! Can be combined with other flags by use of | operator.
179 //! <br><br>
180 //! See xml_document::parse() function.
181 const int parse_no_string_terminators = 0x4;
182
183 //! Parse flag instructing the parser to not translate entities in the source text.
184 //! By default entities are translated, modifying source text.
185 //! Can be combined with other flags by use of | operator.
186 //! <br><br>
187 //! See xml_document::parse() function.
188 const int parse_no_entity_translation = 0x8;
189
190 //! Parse flag instructing the parser to disable UTF-8 handling and assume plain 8 bit characters.
191 //! By default, UTF-8 handling is enabled.
192 //! Can be combined with other flags by use of | operator.
193 //! <br><br>
194 //! See xml_document::parse() function.
195 const int parse_no_utf8 = 0x10;
196
197 //! Parse flag instructing the parser to create XML declaration node.
198 //! By default, declaration node is not created.
199 //! Can be combined with other flags by use of | operator.
200 //! <br><br>
201 //! See xml_document::parse() function.
202 const int parse_declaration_node = 0x20;
203
204 //! Parse flag instructing the parser to create comments nodes.
205 //! By default, comment nodes are not created.
206 //! Can be combined with other flags by use of | operator.
207 //! <br><br>
208 //! See xml_document::parse() function.
209 const int parse_comment_nodes = 0x40;
210
211 //! Parse flag instructing the parser to create DOCTYPE node.
212 //! By default, doctype node is not created.
213 //! Although W3C specification allows at most one DOCTYPE node, RapidXml will silently accept documents with more than one.
214 //! Can be combined with other flags by use of | operator.
215 //! <br><br>
216 //! See xml_document::parse() function.
217 const int parse_doctype_node = 0x80;
218
219 //! Parse flag instructing the parser to create PI nodes.
220 //! By default, PI nodes are not created.
221 //! Can be combined with other flags by use of | operator.
222 //! <br><br>
223 //! See xml_document::parse() function.
224 const int parse_pi_nodes = 0x100;
225
226 //! Parse flag instructing the parser to validate closing tag names.
227 //! If not set, name inside closing tag is irrelevant to the parser.
228 //! By default, closing tags are not validated.
229 //! Can be combined with other flags by use of | operator.
230 //! <br><br>
231 //! See xml_document::parse() function.
232 const int parse_validate_closing_tags = 0x200;
233
234 //! Parse flag instructing the parser to trim all leading and trailing whitespace of data nodes.
235 //! By default, whitespace is not trimmed.
236 //! This flag does not cause the parser to modify source text.
237 //! Can be combined with other flags by use of | operator.
238 //! <br><br>
239 //! See xml_document::parse() function.
240 const int parse_trim_whitespace = 0x400;
241
242 //! Parse flag instructing the parser to condense all whitespace runs of data nodes to a single space character.
243 //! Trimming of leading and trailing whitespace of data is controlled by rapidxml::parse_trim_whitespace flag.
244 //! By default, whitespace is not normalized.
245 //! If this flag is specified, source text will be modified.
246 //! Can be combined with other flags by use of | operator.
247 //! <br><br>
248 //! See xml_document::parse() function.
249 const int parse_normalize_whitespace = 0x800;
250
251 // Compound flags
252
253 //! Parse flags which represent default behaviour of the parser.
254 //! This is always equal to 0, so that all other flags can be simply ored together.
255 //! Normally there is no need to inconveniently disable flags by anding with their negated (~) values.
256 //! This also means that meaning of each flag is a <i>negation</i> of the default setting.
257 //! For example, if flag name is rapidxml::parse_no_utf8, it means that utf-8 is <i>enabled</i> by default,
258 //! and using the flag will disable it.
259 //! <br><br>
260 //! See xml_document::parse() function.
261 const int parse_default = 0;
262
263 //! A combination of parse flags that forbids any modifications of the source text.
264 //! This also results in faster parsing. However, note that the following will occur:
265 //! <ul>
266 //! <li>names and values of nodes will not be zero terminated, you have to use xml_base::name_size() and xml_base::value_size() functions to determine where name and value ends</li>
267 //! <li>entities will not be translated</li>
268 //! <li>whitespace will not be normalized</li>
269 //! </ul>
270 //! See xml_document::parse() function.
271 const int parse_non_destructive = parse_no_string_terminators | parse_no_entity_translation;
272
273 //! A combination of parse flags resulting in fastest possible parsing, without sacrificing important data.
274 //! <br><br>
275 //! See xml_document::parse() function.
276 const int parse_fastest = parse_non_destructive | parse_no_data_nodes;
277
278 //! A combination of parse flags resulting in largest amount of data being extracted.
279 //! This usually results in slowest parsing.
280 //! <br><br>
281 //! See xml_document::parse() function.
282 const int parse_full = parse_declaration_node | parse_comment_nodes | parse_doctype_node | parse_pi_nodes | parse_validate_closing_tags;
283
284 ///////////////////////////////////////////////////////////////////////
285 // Internals
286
287 //! \cond internal
288 namespace internal
289 {
290
291 // Struct that contains lookup tables for the parser
292 // It must be a template to allow correct linking (because it has static data members, which are defined in a header file).
293 template<int Dummy>
294 struct lookup_tables
295 {
296 static const unsigned char lookup_whitespace[256]; // Whitespace table
297 static const unsigned char lookup_node_name[256]; // Node name table
298 static const unsigned char lookup_text[256]; // Text table
299 static const unsigned char lookup_text_pure_no_ws[256]; // Text table
300 static const unsigned char lookup_text_pure_with_ws[256]; // Text table
301 static const unsigned char lookup_attribute_name[256]; // Attribute name table
302 static const unsigned char lookup_attribute_data_1[256]; // Attribute data table with single quote
303 static const unsigned char lookup_attribute_data_1_pure[256]; // Attribute data table with single quote
304 static const unsigned char lookup_attribute_data_2[256]; // Attribute data table with double quotes
305 static const unsigned char lookup_attribute_data_2_pure[256]; // Attribute data table with double quotes
306 static const unsigned char lookup_digits[256]; // Digits
307 static const unsigned char lookup_upcase[256]; // To uppercase conversion table for ASCII characters
308 };
309
310 // Find length of the string
311 template<class Ch>
312 inline std::size_t measure(const Ch *p)
313 {
314 const Ch *tmp = p;
315 while (*tmp)
316 ++tmp;
317 return tmp - p;
318 }
319
320 // Compare strings for equality
321 template<class Ch>
322 inline bool compare(const Ch *p1, std::size_t size1, const Ch *p2, std::size_t size2, bool case_sensitive)
323 {
324 if (size1 != size2)
325 return false;
326 if (case_sensitive)
327 {
328 for (const Ch *end = p1 + size1; p1 < end; ++p1, ++p2)
329 if (*p1 != *p2)
330 return false;
331 }
332 else
333 {
334 for (const Ch *end = p1 + size1; p1 < end; ++p1, ++p2)
335 if (lookup_tables<0>::lookup_upcase[static_cast<unsigned char>(*p1)] != lookup_tables<0>::lookup_upcase[static_cast<unsigned char>(*p2)])
336 return false;
337 }
338 return true;
339 }
340 }
341 //! \endcond
342
343 ///////////////////////////////////////////////////////////////////////
344 // Memory pool
345
346 //! This class is used by the parser to create new nodes and attributes, without overheads of dynamic memory allocation.
347 //! In most cases, you will not need to use this class directly.
348 //! However, if you need to create nodes manually or modify names/values of nodes,
349 //! you are encouraged to use memory_pool of relevant xml_document to allocate the memory.
350 //! Not only is this faster than allocating them by using <code>new</code> operator,
351 //! but also their lifetime will be tied to the lifetime of document,
352 //! possibly simplyfing memory management.
353 //! <br><br>
354 //! Call allocate_node() or allocate_attribute() functions to obtain new nodes or attributes from the pool.
355 //! You can also call allocate_string() function to allocate strings.
356 //! Such strings can then be used as names or values of nodes without worrying about their lifetime.
357 //! Note that there is no <code>free()</code> function -- all allocations are freed at once when clear() function is called,
358 //! or when the pool is destroyed.
359 //! <br><br>
360 //! It is also possible to create a standalone memory_pool, and use it
361 //! to allocate nodes, whose lifetime will not be tied to any document.
362 //! <br><br>
363 //! Pool maintains <code>RAPIDXML_STATIC_POOL_SIZE</code> bytes of statically allocated memory.
364 //! Until static memory is exhausted, no dynamic memory allocations are done.
365 //! When static memory is exhausted, pool allocates additional blocks of memory of size <code>RAPIDXML_DYNAMIC_POOL_SIZE</code> each,
366 //! by using global <code>new[]</code> and <code>delete[]</code> operators.
367 //! This behaviour can be changed by setting custom allocation routines.
368 //! Use set_allocator() function to set them.
369 //! <br><br>
370 //! Allocations for nodes, attributes and strings are aligned at <code>RAPIDXML_ALIGNMENT</code> bytes.
371 //! This value defaults to the size of pointer on target architecture.
372 //! <br><br>
373 //! To obtain absolutely top performance from the parser,
374 //! it is important that all nodes are allocated from a single, contiguous block of memory.
375 //! Otherwise, cache misses when jumping between two (or more) disjoint blocks of memory can slow down parsing quite considerably.
376 //! If required, you can tweak <code>RAPIDXML_STATIC_POOL_SIZE</code>, <code>RAPIDXML_DYNAMIC_POOL_SIZE</code> and <code>RAPIDXML_ALIGNMENT</code>
377 //! to obtain best wasted memory to performance compromise.
378 //! To do it, define their values before rapidxml.hpp file is included.
379 //! \param Ch Character type of created nodes.
380 template<class Ch = char>
381 class memory_pool
382 {
383
384 public:
385
386 //! \cond internal
387 typedef void *(alloc_func)(std::size_t); // Type of user-defined function used to allocate memory
388 typedef void (free_func)(void *); // Type of user-defined function used to free memory
389 //! \endcond
390
391 //! Constructs empty pool with default allocator functions.
392 memory_pool()
393 : m_alloc_func(0)
394 , m_free_func(0)
395 {
396 init();
397 }
398
399 //! Destroys pool and frees all the memory.
400 //! This causes memory occupied by nodes allocated by the pool to be freed.
401 //! Nodes allocated from the pool are no longer valid.
402 ~memory_pool()
403 {
404 clear();
405 }
406
407 //! Allocates a new node from the pool, and optionally assigns name and value to it.
408 //! If the allocation request cannot be accomodated, this function will throw <code>std::bad_alloc</code>.
409 //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function
410 //! will call rapidxml::parse_error_handler() function.
411 //! \param type Type of node to create.
412 //! \param name Name to assign to the node, or 0 to assign no name.
413 //! \param value Value to assign to the node, or 0 to assign no value.
414 //! \param name_size Size of name to assign, or 0 to automatically calculate size from name string.
415 //! \param value_size Size of value to assign, or 0 to automatically calculate size from value string.
416 //! \return Pointer to allocated node. This pointer will never be NULL.
417 xml_node<Ch> *allocate_node(node_type type,
418 const Ch *name = 0, const Ch *value = 0,
419 std::size_t name_size = 0, std::size_t value_size = 0)
420 {
421 void *memory = allocate_aligned(sizeof(xml_node<Ch>));
422 xml_node<Ch> *node = new(memory) xml_node<Ch>(type);
423 if (name)
424 {
425 if (name_size > 0)
426 node->name(name, name_size);
427 else
428 node->name(name);
429 }
430 if (value)
431 {
432 if (value_size > 0)
433 node->value(value, value_size);
434 else
435 node->value(value);
436 }
437 return node;
438 }
439
440 //! Allocates a new attribute from the pool, and optionally assigns name and value to it.
441 //! If the allocation request cannot be accomodated, this function will throw <code>std::bad_alloc</code>.
442 //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function
443 //! will call rapidxml::parse_error_handler() function.
444 //! \param name Name to assign to the attribute, or 0 to assign no name.
445 //! \param value Value to assign to the attribute, or 0 to assign no value.
446 //! \param name_size Size of name to assign, or 0 to automatically calculate size from name string.
447 //! \param value_size Size of value to assign, or 0 to automatically calculate size from value string.
448 //! \return Pointer to allocated attribute. This pointer will never be NULL.
449 xml_attribute<Ch> *allocate_attribute(const Ch *name = 0, const Ch *value = 0,
450 std::size_t name_size = 0, std::size_t value_size = 0)
451 {
452 void *memory = allocate_aligned(sizeof(xml_attribute<Ch>));
453 xml_attribute<Ch> *attribute = new(memory) xml_attribute<Ch>;
454 if (name)
455 {
456 if (name_size > 0)
457 attribute->name(name, name_size);
458 else
459 attribute->name(name);
460 }
461 if (value)
462 {
463 if (value_size > 0)
464 attribute->value(value, value_size);
465 else
466 attribute->value(value);
467 }
468 return attribute;
469 }
470
471 //! Allocates a char array of given size from the pool, and optionally copies a given string to it.
472 //! If the allocation request cannot be accomodated, this function will throw <code>std::bad_alloc</code>.
473 //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function
474 //! will call rapidxml::parse_error_handler() function.
475 //! \param source String to initialize the allocated memory with, or 0 to not initialize it.
476 //! \param size Number of characters to allocate, or zero to calculate it automatically from source string length; if size is 0, source string must be specified and null terminated.
477 //! \return Pointer to allocated char array. This pointer will never be NULL.
478 Ch *allocate_string(const Ch *source = 0, std::size_t size = 0)
479 {
480 assert(source || size); // Either source or size (or both) must be specified
481 if (size == 0)
482 size = internal::measure(source) + 1;
483 Ch *result = static_cast<Ch *>(allocate_aligned(size * sizeof(Ch)));
484 if (source)
485 for (std::size_t i = 0; i < size; ++i)
486 result[i] = source[i];
487 return result;
488 }
489
490 //! Clones an xml_node and its hierarchy of child nodes and attributes.
491 //! Nodes and attributes are allocated from this memory pool.
492 //! Names and values are not cloned, they are shared between the clone and the source.
493 //! Result node can be optionally specified as a second parameter,
494 //! in which case its contents will be replaced with cloned source node.
495 //! This is useful when you want to clone entire document.
496 //! \param source Node to clone.
497 //! \param result Node to put results in, or 0 to automatically allocate result node
498 //! \return Pointer to cloned node. This pointer will never be NULL.
499 xml_node<Ch> *clone_node(const xml_node<Ch> *source, xml_node<Ch> *result = 0)
500 {
501 // Prepare result node
502 if (result)
503 {
504 result->remove_all_attributes();
505 result->remove_all_nodes();
506 result->type(source->type());
507 }
508 else
509 result = allocate_node(source->type());
510
511 // Clone name and value
512 result->name(source->name(), source->name_size());
513 result->value(source->value(), source->value_size());
514
515 // Clone child nodes and attributes
516 for (xml_node<Ch> *child = source->first_node(); child; child = child->next_sibling())
517 result->append_node(clone_node(child));
518 for (xml_attribute<Ch> *attr = source->first_attribute(); attr; attr = attr->next_attribute())
519 result->append_attribute(allocate_attribute(attr->name(), attr->value(), attr->name_size(), attr->value_size()));
520
521 return result;
522 }
523
524 //! Clears the pool.
525 //! This causes memory occupied by nodes allocated by the pool to be freed.
526 //! Any nodes or strings allocated from the pool will no longer be valid.
527 void clear()
528 {
529 while (m_begin != m_static_memory)
530 {
531 char *previous_begin = reinterpret_cast<header *>(align(m_begin))->previous_begin;
532 if (m_free_func)
533 m_free_func(m_begin);
534 else
535 delete[] m_begin;
536 m_begin = previous_begin;
537 }
538 init();
539 }
540
541 //! Sets or resets the user-defined memory allocation functions for the pool.
542 //! This can only be called when no memory is allocated from the pool yet, otherwise results are undefined.
543 //! Allocation function must not return invalid pointer on failure. It should either throw,
544 //! stop the program, or use <code>longjmp()</code> function to pass control to other place of program.
545 //! If it returns invalid pointer, results are undefined.
546 //! <br><br>
547 //! User defined allocation functions must have the following forms:
548 //! <br><code>
549 //! <br>void *allocate(std::size_t size);
550 //! <br>void free(void *pointer);
551 //! </code><br>
552 //! \param af Allocation function, or 0 to restore default function
553 //! \param ff Free function, or 0 to restore default function
554 void set_allocator(alloc_func *af, free_func *ff)
555 {
556 assert(m_begin == m_static_memory && m_ptr == align(m_begin)); // Verify that no memory is allocated yet
557 m_alloc_func = af;
558 m_free_func = ff;
559 }
560
561 private:
562
563 struct header
564 {
565 char *previous_begin;
566 };
567
568 void init()
569 {
570 m_begin = m_static_memory;
571 m_ptr = align(m_begin);
572 m_end = m_static_memory + sizeof(m_static_memory);
573 }
574
575 char *align(char *ptr)
576 {
577 std::size_t alignment = ((RAPIDXML_ALIGNMENT - (std::size_t(ptr) & (RAPIDXML_ALIGNMENT - 1))) & (RAPIDXML_ALIGNMENT - 1));
578 return ptr + alignment;
579 }
580
581 char *allocate_raw(std::size_t size)
582 {
583 // Allocate
584 void *memory;
585 if (m_alloc_func) // Allocate memory using either user-specified allocation function or global operator new[]
586 {
587 memory = m_alloc_func(size);
588 assert(memory); // Allocator is not allowed to return 0, on failure it must either throw, stop the program or use longjmp
589 }
590 else
591 {
592 memory = new char[size];
593#ifdef RAPIDXML_NO_EXCEPTIONS
594 if (!memory) // If exceptions are disabled, verify memory allocation, because new will not be able to throw bad_alloc
595 RAPIDXML_PARSE_ERROR("out of memory", 0);
596#endif
597 }
598 return static_cast<char *>(memory);
599 }
600
601 void *allocate_aligned(std::size_t size)
602 {
603 // Calculate aligned pointer
604 char *result = align(m_ptr);
605
606 // If not enough memory left in current pool, allocate a new pool
607 if (result + size > m_end)
608 {
609 // Calculate required pool size (may be bigger than RAPIDXML_DYNAMIC_POOL_SIZE)
610 std::size_t pool_size = RAPIDXML_DYNAMIC_POOL_SIZE;
611 if (pool_size < size)
612 pool_size = size;
613
614 // Allocate
615 std::size_t alloc_size = sizeof(header) + (2 * RAPIDXML_ALIGNMENT - 2) + pool_size; // 2 alignments required in worst case: one for header, one for actual allocation
616 char *raw_memory = allocate_raw(alloc_size);
617
618 // Setup new pool in allocated memory
619 char *pool = align(raw_memory);
620 header *new_header = reinterpret_cast<header *>(pool);
621 new_header->previous_begin = m_begin;
622 m_begin = raw_memory;
623 m_ptr = pool + sizeof(header);
624 m_end = raw_memory + alloc_size;
625
626 // Calculate aligned pointer again using new pool
627 result = align(m_ptr);
628 }
629
630 // Update pool and return aligned pointer
631 m_ptr = result + size;
632 return result;
633 }
634
635 char *m_begin; // Start of raw memory making up current pool
636 char *m_ptr; // First free byte in current pool
637 char *m_end; // One past last available byte in current pool
638 char m_static_memory[RAPIDXML_STATIC_POOL_SIZE]; // Static raw memory
639 alloc_func *m_alloc_func; // Allocator function, or 0 if default is to be used
640 free_func *m_free_func; // Free function, or 0 if default is to be used
641 };
642
643 ///////////////////////////////////////////////////////////////////////////
644 // XML base
645
646 //! Base class for xml_node and xml_attribute implementing common functions:
647 //! name(), name_size(), value(), value_size() and parent().
648 //! \param Ch Character type to use
649 template<class Ch = char>
650 class xml_base
651 {
652
653 public:
654
655 ///////////////////////////////////////////////////////////////////////////
656 // Construction & destruction
657
658 // Construct a base with empty name, value and parent
659 xml_base()
660 : m_name(0)
661 , m_value(0)
662 , m_parent(0)
663 {
664 }
665
666 ///////////////////////////////////////////////////////////////////////////
667 // Node data access
668
669 //! Gets name of the node.
670 //! Interpretation of name depends on type of node.
671 //! Note that name will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse.
672 //! <br><br>
673 //! Use name_size() function to determine length of the name.
674 //! \return Name of node, or empty string if node has no name.
675 Ch *name() const
676 {
677 return m_name ? m_name : nullstr();
678 }
679
680 //! Gets size of node name, not including terminator character.
681 //! This function works correctly irrespective of whether name is or is not zero terminated.
682 //! \return Size of node name, in characters.
683 std::size_t name_size() const
684 {
685 return m_name ? m_name_size : 0;
686 }
687
688 //! Gets value of node.
689 //! Interpretation of value depends on type of node.
690 //! Note that value will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse.
691 //! <br><br>
692 //! Use value_size() function to determine length of the value.
693 //! \return Value of node, or empty string if node has no value.
694 Ch *value() const
695 {
696 return m_value ? m_value : nullstr();
697 }
698
699 //! Gets size of node value, not including terminator character.
700 //! This function works correctly irrespective of whether value is or is not zero terminated.
701 //! \return Size of node value, in characters.
702 std::size_t value_size() const
703 {
704 return m_value ? m_value_size : 0;
705 }
706
707 ///////////////////////////////////////////////////////////////////////////
708 // Node modification
709
710 //! Sets name of node to a non zero-terminated string.
711 //! See \ref ownership_of_strings.
712 //! <br><br>
713 //! Note that node does not own its name or value, it only stores a pointer to it.
714 //! It will not delete or otherwise free the pointer on destruction.
715 //! It is reponsibility of the user to properly manage lifetime of the string.
716 //! The easiest way to achieve it is to use memory_pool of the document to allocate the string -
717 //! on destruction of the document the string will be automatically freed.
718 //! <br><br>
719 //! Size of name must be specified separately, because name does not have to be zero terminated.
720 //! Use name(const Ch *) function to have the length automatically calculated (string must be zero terminated).
721 //! \param name Name of node to set. Does not have to be zero terminated.
722 //! \param size Size of name, in characters. This does not include zero terminator, if one is present.
723 void name(const Ch *name, std::size_t size)
724 {
725 m_name = const_cast<Ch *>(name);
726 m_name_size = size;
727 }
728
729 //! Sets name of node to a zero-terminated string.
730 //! See also \ref ownership_of_strings and xml_node::name(const Ch *, std::size_t).
731 //! \param name Name of node to set. Must be zero terminated.
732 void name(const Ch *name)
733 {
734 this->name(name, internal::measure(name));
735 }
736
737 //! Sets value of node to a non zero-terminated string.
738 //! See \ref ownership_of_strings.
739 //! <br><br>
740 //! Note that node does not own its name or value, it only stores a pointer to it.
741 //! It will not delete or otherwise free the pointer on destruction.
742 //! It is reponsibility of the user to properly manage lifetime of the string.
743 //! The easiest way to achieve it is to use memory_pool of the document to allocate the string -
744 //! on destruction of the document the string will be automatically freed.
745 //! <br><br>
746 //! Size of value must be specified separately, because it does not have to be zero terminated.
747 //! Use value(const Ch *) function to have the length automatically calculated (string must be zero terminated).
748 //! <br><br>
749 //! If an element has a child node of type node_data, it will take precedence over element value when printing.
750 //! If you want to manipulate data of elements using values, use parser flag rapidxml::parse_no_data_nodes to prevent creation of data nodes by the parser.
751 //! \param value value of node to set. Does not have to be zero terminated.
752 //! \param size Size of value, in characters. This does not include zero terminator, if one is present.
753 void value(const Ch *value, std::size_t size)
754 {
755 m_value = const_cast<Ch *>(value);
756 m_value_size = size;
757 }
758
759 //! Sets value of node to a zero-terminated string.
760 //! See also \ref ownership_of_strings and xml_node::value(const Ch *, std::size_t).
761 //! \param value Vame of node to set. Must be zero terminated.
762 void value(const Ch *value)
763 {
764 this->value(value, internal::measure(value));
765 }
766
767 ///////////////////////////////////////////////////////////////////////////
768 // Related nodes access
769
770 //! Gets node parent.
771 //! \return Pointer to parent node, or 0 if there is no parent.
772 xml_node<Ch> *parent() const
773 {
774 return m_parent;
775 }
776
777 protected:
778
779 // Return empty string
780 static Ch *nullstr()
781 {
782 static Ch zero = Ch('\0');
783 return &zero;
784 }
785
786 Ch *m_name; // Name of node, or 0 if no name
787 Ch *m_value; // Value of node, or 0 if no value
788 std::size_t m_name_size; // Length of node name, or undefined of no name
789 std::size_t m_value_size; // Length of node value, or undefined if no value
790 xml_node<Ch> *m_parent; // Pointer to parent node, or 0 if none
791
792 };
793
794 //! Class representing attribute node of XML document.
795 //! Each attribute has name and value strings, which are available through name() and value() functions (inherited from xml_base).
796 //! Note that after parse, both name and value of attribute will point to interior of source text used for parsing.
797 //! Thus, this text must persist in memory for the lifetime of attribute.
798 //! \param Ch Character type to use.
799 template<class Ch = char>
800 class xml_attribute: public xml_base<Ch>
801 {
802
803 friend class xml_node<Ch>;
804
805 public:
806
807 ///////////////////////////////////////////////////////////////////////////
808 // Construction & destruction
809
810 //! Constructs an empty attribute with the specified type.
811 //! Consider using memory_pool of appropriate xml_document if allocating attributes manually.
812 xml_attribute()
813 {
814 }
815
816 ///////////////////////////////////////////////////////////////////////////
817 // Related nodes access
818
819 //! Gets document of which attribute is a child.
820 //! \return Pointer to document that contains this attribute, or 0 if there is no parent document.
821 xml_document<Ch> *document() const
822 {
823 if (xml_node<Ch> *node = this->parent())
824 {
825 while (node->parent())
826 node = node->parent();
827 return node->type() == node_document ? static_cast<xml_document<Ch> *>(node) : 0;
828 }
829 else
830 return 0;
831 }
832
833 //! Gets previous attribute, optionally matching attribute name.
834 //! \param name Name of attribute to find, or 0 to return previous attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
835 //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
836 //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
837 //! \return Pointer to found attribute, or 0 if not found.
838 xml_attribute<Ch> *previous_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
839 {
840 if (name)
841 {
842 if (name_size == 0)
843 name_size = internal::measure(name);
844 for (xml_attribute<Ch> *attribute = m_prev_attribute; attribute; attribute = attribute->m_prev_attribute)
845 if (internal::compare(attribute->name(), attribute->name_size(), name, name_size, case_sensitive))
846 return attribute;
847 return 0;
848 }
849 else
850 return this->m_parent ? m_prev_attribute : 0;
851 }
852
853 //! Gets next attribute, optionally matching attribute name.
854 //! \param name Name of attribute to find, or 0 to return next attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
855 //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
856 //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
857 //! \return Pointer to found attribute, or 0 if not found.
858 xml_attribute<Ch> *next_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
859 {
860 if (name)
861 {
862 if (name_size == 0)
863 name_size = internal::measure(name);
864 for (xml_attribute<Ch> *attribute = m_next_attribute; attribute; attribute = attribute->m_next_attribute)
865 if (internal::compare(attribute->name(), attribute->name_size(), name, name_size, case_sensitive))
866 return attribute;
867 return 0;
868 }
869 else
870 return this->m_parent ? m_next_attribute : 0;
871 }
872
873 private:
874
875 xml_attribute<Ch> *m_prev_attribute; // Pointer to previous sibling of attribute, or 0 if none; only valid if parent is non-zero
876 xml_attribute<Ch> *m_next_attribute; // Pointer to next sibling of attribute, or 0 if none; only valid if parent is non-zero
877
878 };
879
880 ///////////////////////////////////////////////////////////////////////////
881 // XML node
882
883 //! Class representing a node of XML document.
884 //! Each node may have associated name and value strings, which are available through name() and value() functions.
885 //! Interpretation of name and value depends on type of the node.
886 //! Type of node can be determined by using type() function.
887 //! <br><br>
888 //! Note that after parse, both name and value of node, if any, will point interior of source text used for parsing.
889 //! Thus, this text must persist in the memory for the lifetime of node.
890 //! \param Ch Character type to use.
891 template<class Ch = char>
892 class xml_node: public xml_base<Ch>
893 {
894
895 public:
896
897 ///////////////////////////////////////////////////////////////////////////
898 // Construction & destruction
899
900 //! Constructs an empty node with the specified type.
901 //! Consider using memory_pool of appropriate document to allocate nodes manually.
902 //! \param type Type of node to construct.
903 xml_node(node_type type)
904 : m_type(type)
905 , m_first_node(0)
906 , m_first_attribute(0)
907 {
908 }
909
910 ///////////////////////////////////////////////////////////////////////////
911 // Node data access
912
913 //! Gets type of node.
914 //! \return Type of node.
915 node_type type() const
916 {
917 return m_type;
918 }
919
920 ///////////////////////////////////////////////////////////////////////////
921 // Related nodes access
922
923 //! Gets document of which node is a child.
924 //! \return Pointer to document that contains this node, or 0 if there is no parent document.
925 xml_document<Ch> *document() const
926 {
927 xml_node<Ch> *node = const_cast<xml_node<Ch> *>(this);
928 while (node->parent())
929 node = node->parent();
930 return node->type() == node_document ? static_cast<xml_document<Ch> *>(node) : 0;
931 }
932
933 //! Gets first child node, optionally matching node name.
934 //! \param name Name of child to find, or 0 to return first child regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
935 //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
936 //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
937 //! \return Pointer to found child, or 0 if not found.
938 xml_node<Ch> *first_node(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
939 {
940 if (name)
941 {
942 if (name_size == 0)
943 name_size = internal::measure(name);
944 for (xml_node<Ch> *child = m_first_node; child; child = child->next_sibling())
945 if (internal::compare(child->name(), child->name_size(), name, name_size, case_sensitive))
946 return child;
947 return 0;
948 }
949 else
950 return m_first_node;
951 }
952
953 //! Gets last child node, optionally matching node name.
954 //! Behaviour is undefined if node has no children.
955 //! Use first_node() to test if node has children.
956 //! \param name Name of child to find, or 0 to return last child regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
957 //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
958 //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
959 //! \return Pointer to found child, or 0 if not found.
960 xml_node<Ch> *last_node(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
961 {
962 assert(m_first_node); // Cannot query for last child if node has no children
963 if (name)
964 {
965 if (name_size == 0)
966 name_size = internal::measure(name);
967 for (xml_node<Ch> *child = m_last_node; child; child = child->previous_sibling())
968 if (internal::compare(child->name(), child->name_size(), name, name_size, case_sensitive))
969 return child;
970 return 0;
971 }
972 else
973 return m_last_node;
974 }
975
976 //! Gets previous sibling node, optionally matching node name.
977 //! Behaviour is undefined if node has no parent.
978 //! Use parent() to test if node has a parent.
979 //! \param name Name of sibling to find, or 0 to return previous sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
980 //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
981 //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
982 //! \return Pointer to found sibling, or 0 if not found.
983 xml_node<Ch> *previous_sibling(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
984 {
985 assert(this->m_parent); // Cannot query for siblings if node has no parent
986 if (name)
987 {
988 if (name_size == 0)
989 name_size = internal::measure(name);
990 for (xml_node<Ch> *sibling = m_prev_sibling; sibling; sibling = sibling->m_prev_sibling)
991 if (internal::compare(sibling->name(), sibling->name_size(), name, name_size, case_sensitive))
992 return sibling;
993 return 0;
994 }
995 else
996 return m_prev_sibling;
997 }
998
999 //! Gets next sibling node, optionally matching node name.
1000 //! Behaviour is undefined if node has no parent.
1001 //! Use parent() to test if node has a parent.
1002 //! \param name Name of sibling to find, or 0 to return next sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
1003 //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
1004 //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
1005 //! \return Pointer to found sibling, or 0 if not found.
1006 xml_node<Ch> *next_sibling(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
1007 {
1008 assert(this->m_parent); // Cannot query for siblings if node has no parent
1009 if (name)
1010 {
1011 if (name_size == 0)
1012 name_size = internal::measure(name);
1013 for (xml_node<Ch> *sibling = m_next_sibling; sibling; sibling = sibling->m_next_sibling)
1014 if (internal::compare(sibling->name(), sibling->name_size(), name, name_size, case_sensitive))
1015 return sibling;
1016 return 0;
1017 }
1018 else
1019 return m_next_sibling;
1020 }
1021
1022 //! Gets first attribute of node, optionally matching attribute name.
1023 //! \param name Name of attribute to find, or 0 to return first attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
1024 //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
1025 //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
1026 //! \return Pointer to found attribute, or 0 if not found.
1027 xml_attribute<Ch> *first_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
1028 {
1029 if (name)
1030 {
1031 if (name_size == 0)
1032 name_size = internal::measure(name);
1033 for (xml_attribute<Ch> *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute)
1034 if (internal::compare(attribute->name(), attribute->name_size(), name, name_size, case_sensitive))
1035 return attribute;
1036 return 0;
1037 }
1038 else
1039 return m_first_attribute;
1040 }
1041
1042 //! Gets last attribute of node, optionally matching attribute name.
1043 //! \param name Name of attribute to find, or 0 to return last attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
1044 //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
1045 //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
1046 //! \return Pointer to found attribute, or 0 if not found.
1047 xml_attribute<Ch> *last_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
1048 {
1049 if (name)
1050 {
1051 if (name_size == 0)
1052 name_size = internal::measure(name);
1053 for (xml_attribute<Ch> *attribute = m_last_attribute; attribute; attribute = attribute->m_prev_attribute)
1054 if (internal::compare(attribute->name(), attribute->name_size(), name, name_size, case_sensitive))
1055 return attribute;
1056 return 0;
1057 }
1058 else
1059 return m_first_attribute ? m_last_attribute : 0;
1060 }
1061
1062 ///////////////////////////////////////////////////////////////////////////
1063 // Node modification
1064
1065 //! Sets type of node.
1066 //! \param type Type of node to set.
1067 void type(node_type type)
1068 {
1069 m_type = type;
1070 }
1071
1072 ///////////////////////////////////////////////////////////////////////////
1073 // Node manipulation
1074
1075 //! Prepends a new child node.
1076 //! The prepended child becomes the first child, and all existing children are moved one position back.
1077 //! \param child Node to prepend.
1078 void prepend_node(xml_node<Ch> *child)
1079 {
1080 assert(child && !child->parent() && child->type() != node_document);
1081 if (first_node())
1082 {
1083 child->m_next_sibling = m_first_node;
1084 m_first_node->m_prev_sibling = child;
1085 }
1086 else
1087 {
1088 child->m_next_sibling = 0;
1089 m_last_node = child;
1090 }
1091 m_first_node = child;
1092 child->m_parent = this;
1093 child->m_prev_sibling = 0;
1094 }
1095
1096 //! Appends a new child node.
1097 //! The appended child becomes the last child.
1098 //! \param child Node to append.
1099 void append_node(xml_node<Ch> *child)
1100 {
1101 assert(child && !child->parent() && child->type() != node_document);
1102 if (first_node())
1103 {
1104 child->m_prev_sibling = m_last_node;
1105 m_last_node->m_next_sibling = child;
1106 }
1107 else
1108 {
1109 child->m_prev_sibling = 0;
1110 m_first_node = child;
1111 }
1112 m_last_node = child;
1113 child->m_parent = this;
1114 child->m_next_sibling = 0;
1115 }
1116
1117 //! Inserts a new child node at specified place inside the node.
1118 //! All children after and including the specified node are moved one position back.
1119 //! \param where Place where to insert the child, or 0 to insert at the back.
1120 //! \param child Node to insert.
1121 void insert_node(xml_node<Ch> *where, xml_node<Ch> *child)
1122 {
1123 assert(!where || where->parent() == this);
1124 assert(child && !child->parent() && child->type() != node_document);
1125 if (where == m_first_node)
1126 prepend_node(child);
1127 else if (where == 0)
1128 append_node(child);
1129 else
1130 {
1131 child->m_prev_sibling = where->m_prev_sibling;
1132 child->m_next_sibling = where;
1133 where->m_prev_sibling->m_next_sibling = child;
1134 where->m_prev_sibling = child;
1135 child->m_parent = this;
1136 }
1137 }
1138
1139 //! Removes first child node.
1140 //! If node has no children, behaviour is undefined.
1141 //! Use first_node() to test if node has children.
1142 void remove_first_node()
1143 {
1144 assert(first_node());
1145 xml_node<Ch> *child = m_first_node;
1146 m_first_node = child->m_next_sibling;
1147 if (child->m_next_sibling)
1148 child->m_next_sibling->m_prev_sibling = 0;
1149 else
1150 m_last_node = 0;
1151 child->m_parent = 0;
1152 }
1153
1154 //! Removes last child of the node.
1155 //! If node has no children, behaviour is undefined.
1156 //! Use first_node() to test if node has children.
1157 void remove_last_node()
1158 {
1159 assert(first_node());
1160 xml_node<Ch> *child = m_last_node;
1161 if (child->m_prev_sibling)
1162 {
1163 m_last_node = child->m_prev_sibling;
1164 child->m_prev_sibling->m_next_sibling = 0;
1165 }
1166 else
1167 m_first_node = 0;
1168 child->m_parent = 0;
1169 }
1170
1171 //! Removes specified child from the node
1172 // \param where Pointer to child to be removed.
1173 void remove_node(xml_node<Ch> *where)
1174 {
1175 assert(where && where->parent() == this);
1176 assert(first_node());
1177 if (where == m_first_node)
1178 remove_first_node();
1179 else if (where == m_last_node)
1180 remove_last_node();
1181 else
1182 {
1183 where->m_prev_sibling->m_next_sibling = where->m_next_sibling;
1184 where->m_next_sibling->m_prev_sibling = where->m_prev_sibling;
1185 where->m_parent = 0;
1186 }
1187 }
1188
1189 //! Removes all child nodes (but not attributes).
1190 void remove_all_nodes()
1191 {
1192 for (xml_node<Ch> *node = first_node(); node; node = node->m_next_sibling)
1193 node->m_parent = 0;
1194 m_first_node = 0;
1195 }
1196
1197 //! Prepends a new attribute to the node.
1198 //! \param attribute Attribute to prepend.
1199 void prepend_attribute(xml_attribute<Ch> *attribute)
1200 {
1201 assert(attribute && !attribute->parent());
1202 if (first_attribute())
1203 {
1204 attribute->m_next_attribute = m_first_attribute;
1205 m_first_attribute->m_prev_attribute = attribute;
1206 }
1207 else
1208 {
1209 attribute->m_next_attribute = 0;
1210 m_last_attribute = attribute;
1211 }
1212 m_first_attribute = attribute;
1213 attribute->m_parent = this;
1214 attribute->m_prev_attribute = 0;
1215 }
1216
1217 //! Appends a new attribute to the node.
1218 //! \param attribute Attribute to append.
1219 void append_attribute(xml_attribute<Ch> *attribute)
1220 {
1221 assert(attribute && !attribute->parent());
1222 if (first_attribute())
1223 {
1224 attribute->m_prev_attribute = m_last_attribute;
1225 m_last_attribute->m_next_attribute = attribute;
1226 }
1227 else
1228 {
1229 attribute->m_prev_attribute = 0;
1230 m_first_attribute = attribute;
1231 }
1232 m_last_attribute = attribute;
1233 attribute->m_parent = this;
1234 attribute->m_next_attribute = 0;
1235 }
1236
1237 //! Inserts a new attribute at specified place inside the node.
1238 //! All attributes after and including the specified attribute are moved one position back.
1239 //! \param where Place where to insert the attribute, or 0 to insert at the back.
1240 //! \param attribute Attribute to insert.
1241 void insert_attribute(xml_attribute<Ch> *where, xml_attribute<Ch> *attribute)
1242 {
1243 assert(!where || where->parent() == this);
1244 assert(attribute && !attribute->parent());
1245 if (where == m_first_attribute)
1246 prepend_attribute(attribute);
1247 else if (where == 0)
1248 append_attribute(attribute);
1249 else
1250 {
1251 attribute->m_prev_attribute = where->m_prev_attribute;
1252 attribute->m_next_attribute = where;
1253 where->m_prev_attribute->m_next_attribute = attribute;
1254 where->m_prev_attribute = attribute;
1255 attribute->m_parent = this;
1256 }
1257 }
1258
1259 //! Removes first attribute of the node.
1260 //! If node has no attributes, behaviour is undefined.
1261 //! Use first_attribute() to test if node has attributes.
1262 void remove_first_attribute()
1263 {
1264 assert(first_attribute());
1265 xml_attribute<Ch> *attribute = m_first_attribute;
1266 if (attribute->m_next_attribute)
1267 {
1268 attribute->m_next_attribute->m_prev_attribute = 0;
1269 }
1270 else
1271 m_last_attribute = 0;
1272 attribute->m_parent = 0;
1273 m_first_attribute = attribute->m_next_attribute;
1274 }
1275
1276 //! Removes last attribute of the node.
1277 //! If node has no attributes, behaviour is undefined.
1278 //! Use first_attribute() to test if node has attributes.
1279 void remove_last_attribute()
1280 {
1281 assert(first_attribute());
1282 xml_attribute<Ch> *attribute = m_last_attribute;
1283 if (attribute->m_prev_attribute)
1284 {
1285 attribute->m_prev_attribute->m_next_attribute = 0;
1286 m_last_attribute = attribute->m_prev_attribute;
1287 }
1288 else
1289 m_first_attribute = 0;
1290 attribute->m_parent = 0;
1291 }
1292
1293 //! Removes specified attribute from node.
1294 //! \param where Pointer to attribute to be removed.
1295 void remove_attribute(xml_attribute<Ch> *where)
1296 {
1297 assert(first_attribute() && where->parent() == this);
1298 if (where == m_first_attribute)
1299 remove_first_attribute();
1300 else if (where == m_last_attribute)
1301 remove_last_attribute();
1302 else
1303 {
1304 where->m_prev_attribute->m_next_attribute = where->m_next_attribute;
1305 where->m_next_attribute->m_prev_attribute = where->m_prev_attribute;
1306 where->m_parent = 0;
1307 }
1308 }
1309
1310 //! Removes all attributes of node.
1311 void remove_all_attributes()
1312 {
1313 for (xml_attribute<Ch> *attribute = first_attribute(); attribute; attribute = attribute->m_next_attribute)
1314 attribute->m_parent = 0;
1315 m_first_attribute = 0;
1316 }
1317
1318 private:
1319
1320 ///////////////////////////////////////////////////////////////////////////
1321 // Restrictions
1322
1323 // No copying
1324 xml_node(const xml_node &);
1325 void operator =(const xml_node &);
1326
1327 ///////////////////////////////////////////////////////////////////////////
1328 // Data members
1329
1330 // Note that some of the pointers below have UNDEFINED values if certain other pointers are 0.
1331 // This is required for maximum performance, as it allows the parser to omit initialization of
1332 // unneded/redundant values.
1333 //
1334 // The rules are as follows:
1335 // 1. first_node and first_attribute contain valid pointers, or 0 if node has no children/attributes respectively
1336 // 2. last_node and last_attribute are valid only if node has at least one child/attribute respectively, otherwise they contain garbage
1337 // 3. prev_sibling and next_sibling are valid only if node has a parent, otherwise they contain garbage
1338
1339 node_type m_type; // Type of node; always valid
1340 xml_node<Ch> *m_first_node; // Pointer to first child node, or 0 if none; always valid
1341 xml_node<Ch> *m_last_node; // Pointer to last child node, or 0 if none; this value is only valid if m_first_node is non-zero
1342 xml_attribute<Ch> *m_first_attribute; // Pointer to first attribute of node, or 0 if none; always valid
1343 xml_attribute<Ch> *m_last_attribute; // Pointer to last attribute of node, or 0 if none; this value is only valid if m_first_attribute is non-zero
1344 xml_node<Ch> *m_prev_sibling; // Pointer to previous sibling of node, or 0 if none; this value is only valid if m_parent is non-zero
1345 xml_node<Ch> *m_next_sibling; // Pointer to next sibling of node, or 0 if none; this value is only valid if m_parent is non-zero
1346
1347 };
1348
1349 ///////////////////////////////////////////////////////////////////////////
1350 // XML document
1351
1352 //! This class represents root of the DOM hierarchy.
1353 //! It is also an xml_node and a memory_pool through public inheritance.
1354 //! Use parse() function to build a DOM tree from a zero-terminated XML text string.
1355 //! parse() function allocates memory for nodes and attributes by using functions of xml_document,
1356 //! which are inherited from memory_pool.
1357 //! To access root node of the document, use the document itself, as if it was an xml_node.
1358 //! \param Ch Character type to use.
1359 template<class Ch = char>
1360 class xml_document: public xml_node<Ch>, public memory_pool<Ch>
1361 {
1362
1363 public:
1364
1365 //! Constructs empty XML document
1366 xml_document()
1367 : xml_node<Ch>(node_document)
1368 {
1369 }
1370
1371 //! Parses zero-terminated XML string according to given flags.
1372 //! Passed string will be modified by the parser, unless rapidxml::parse_non_destructive flag is used.
1373 //! The string must persist for the lifetime of the document.
1374 //! In case of error, rapidxml::parse_error exception will be thrown.
1375 //! <br><br>
1376 //! If you want to parse contents of a file, you must first load the file into the memory, and pass pointer to its beginning.
1377 //! Make sure that data is zero-terminated.
1378 //! <br><br>
1379 //! Document can be parsed into multiple times.
1380 //! Each new call to parse removes previous nodes and attributes (if any), but does not clear memory pool.
1381 //! \param text XML data to parse; pointer is non-const to denote fact that this data may be modified by the parser.
1382 template<int Flags>
1383 void parse(Ch *text)
1384 {
1385 assert(text);
1386
1387 // Remove current contents
1388 this->remove_all_nodes();
1389 this->remove_all_attributes();
1390
1391 // Parse BOM, if any
1392 parse_bom<Flags>(text);
me-cafebabe322eb022022-07-21 14:25:53 +08001393
1394 // Abort if it's ABX format
1395 if (text[0] == Ch('A') &&
1396 text[1] == Ch('B') &&
1397 text[2] == Ch('X')) {
1398 RAPIDXML_PARSE_ERROR("ABX Format Unsupported by RapidXML", text);
1399 return;
1400 }
1401
Matt Mower64ffe732016-02-10 00:19:34 -06001402 // Parse children
1403 while (1)
1404 {
1405 // Skip whitespace before node
1406 skip<whitespace_pred, Flags>(text);
1407 if (*text == 0)
1408 break;
1409
1410 // Parse and append new child
1411 if (*text == Ch('<'))
1412 {
1413 ++text; // Skip '<'
1414 if (xml_node<Ch> *node = parse_node<Flags>(text))
1415 this->append_node(node);
1416 }
1417 else
1418 RAPIDXML_PARSE_ERROR("expected <", text);
1419 }
1420
1421 }
1422
1423 //! Clears the document by deleting all nodes and clearing the memory pool.
1424 //! All nodes owned by document pool are destroyed.
1425 void clear()
1426 {
1427 this->remove_all_nodes();
1428 this->remove_all_attributes();
1429 memory_pool<Ch>::clear();
1430 }
1431
1432 private:
1433
1434 ///////////////////////////////////////////////////////////////////////
1435 // Internal character utility functions
1436
1437 // Detect whitespace character
1438 struct whitespace_pred
1439 {
1440 static unsigned char test(Ch ch)
1441 {
1442 return internal::lookup_tables<0>::lookup_whitespace[static_cast<unsigned char>(ch)];
1443 }
1444 };
1445
1446 // Detect node name character
1447 struct node_name_pred
1448 {
1449 static unsigned char test(Ch ch)
1450 {
1451 return internal::lookup_tables<0>::lookup_node_name[static_cast<unsigned char>(ch)];
1452 }
1453 };
1454
1455 // Detect attribute name character
1456 struct attribute_name_pred
1457 {
1458 static unsigned char test(Ch ch)
1459 {
1460 return internal::lookup_tables<0>::lookup_attribute_name[static_cast<unsigned char>(ch)];
1461 }
1462 };
1463
1464 // Detect text character (PCDATA)
1465 struct text_pred
1466 {
1467 static unsigned char test(Ch ch)
1468 {
1469 return internal::lookup_tables<0>::lookup_text[static_cast<unsigned char>(ch)];
1470 }
1471 };
1472
1473 // Detect text character (PCDATA) that does not require processing
1474 struct text_pure_no_ws_pred
1475 {
1476 static unsigned char test(Ch ch)
1477 {
1478 return internal::lookup_tables<0>::lookup_text_pure_no_ws[static_cast<unsigned char>(ch)];
1479 }
1480 };
1481
1482 // Detect text character (PCDATA) that does not require processing
1483 struct text_pure_with_ws_pred
1484 {
1485 static unsigned char test(Ch ch)
1486 {
1487 return internal::lookup_tables<0>::lookup_text_pure_with_ws[static_cast<unsigned char>(ch)];
1488 }
1489 };
1490
1491 // Detect attribute value character
1492 template<Ch Quote>
1493 struct attribute_value_pred
1494 {
1495 static unsigned char test(Ch ch)
1496 {
1497 if (Quote == Ch('\''))
1498 return internal::lookup_tables<0>::lookup_attribute_data_1[static_cast<unsigned char>(ch)];
1499 if (Quote == Ch('\"'))
1500 return internal::lookup_tables<0>::lookup_attribute_data_2[static_cast<unsigned char>(ch)];
1501 return 0; // Should never be executed, to avoid warnings on Comeau
1502 }
1503 };
1504
1505 // Detect attribute value character
1506 template<Ch Quote>
1507 struct attribute_value_pure_pred
1508 {
1509 static unsigned char test(Ch ch)
1510 {
1511 if (Quote == Ch('\''))
1512 return internal::lookup_tables<0>::lookup_attribute_data_1_pure[static_cast<unsigned char>(ch)];
1513 if (Quote == Ch('\"'))
1514 return internal::lookup_tables<0>::lookup_attribute_data_2_pure[static_cast<unsigned char>(ch)];
1515 return 0; // Should never be executed, to avoid warnings on Comeau
1516 }
1517 };
1518
1519 // Insert coded character, using UTF8 or 8-bit ASCII
1520 template<int Flags>
1521 static void insert_coded_character(Ch *&text, unsigned long code)
1522 {
1523 if (Flags & parse_no_utf8)
1524 {
1525 // Insert 8-bit ASCII character
1526 // Todo: possibly verify that code is less than 256 and use replacement char otherwise?
1527 text[0] = static_cast<unsigned char>(code);
1528 text += 1;
1529 }
1530 else
1531 {
1532 // Insert UTF8 sequence
1533 if (code < 0x80) // 1 byte sequence
1534 {
1535 text[0] = static_cast<unsigned char>(code);
1536 text += 1;
1537 }
1538 else if (code < 0x800) // 2 byte sequence
1539 {
1540 text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1541 text[0] = static_cast<unsigned char>(code | 0xC0);
1542 text += 2;
1543 }
1544 else if (code < 0x10000) // 3 byte sequence
1545 {
1546 text[2] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1547 text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1548 text[0] = static_cast<unsigned char>(code | 0xE0);
1549 text += 3;
1550 }
1551 else if (code < 0x110000) // 4 byte sequence
1552 {
1553 text[3] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1554 text[2] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1555 text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1556 text[0] = static_cast<unsigned char>(code | 0xF0);
1557 text += 4;
1558 }
1559 else // Invalid, only codes up to 0x10FFFF are allowed in Unicode
1560 {
1561 RAPIDXML_PARSE_ERROR("invalid numeric character entity", text);
1562 }
1563 }
1564 }
1565
1566 // Skip characters until predicate evaluates to true
1567 template<class StopPred, int Flags>
1568 static void skip(Ch *&text)
1569 {
1570 Ch *tmp = text;
1571 while (StopPred::test(*tmp))
1572 ++tmp;
1573 text = tmp;
1574 }
1575
1576 // Skip characters until predicate evaluates to true while doing the following:
1577 // - replacing XML character entity references with proper characters (&apos; &amp; &quot; &lt; &gt; &#...;)
1578 // - condensing whitespace sequences to single space character
1579 template<class StopPred, class StopPredPure, int Flags>
1580 static Ch *skip_and_expand_character_refs(Ch *&text)
1581 {
1582 // If entity translation, whitespace condense and whitespace trimming is disabled, use plain skip
1583 if (Flags & parse_no_entity_translation &&
1584 !(Flags & parse_normalize_whitespace) &&
1585 !(Flags & parse_trim_whitespace))
1586 {
1587 skip<StopPred, Flags>(text);
1588 return text;
1589 }
1590
1591 // Use simple skip until first modification is detected
1592 skip<StopPredPure, Flags>(text);
1593
1594 // Use translation skip
1595 Ch *src = text;
1596 Ch *dest = src;
1597 while (StopPred::test(*src))
1598 {
1599 // If entity translation is enabled
1600 if (!(Flags & parse_no_entity_translation))
1601 {
1602 // Test if replacement is needed
1603 if (src[0] == Ch('&'))
1604 {
1605 switch (src[1])
1606 {
1607
1608 // &amp; &apos;
1609 case Ch('a'):
1610 if (src[2] == Ch('m') && src[3] == Ch('p') && src[4] == Ch(';'))
1611 {
1612 *dest = Ch('&');
1613 ++dest;
1614 src += 5;
1615 continue;
1616 }
1617 if (src[2] == Ch('p') && src[3] == Ch('o') && src[4] == Ch('s') && src[5] == Ch(';'))
1618 {
1619 *dest = Ch('\'');
1620 ++dest;
1621 src += 6;
1622 continue;
1623 }
1624 break;
1625
1626 // &quot;
1627 case Ch('q'):
1628 if (src[2] == Ch('u') && src[3] == Ch('o') && src[4] == Ch('t') && src[5] == Ch(';'))
1629 {
1630 *dest = Ch('"');
1631 ++dest;
1632 src += 6;
1633 continue;
1634 }
1635 break;
1636
1637 // &gt;
1638 case Ch('g'):
1639 if (src[2] == Ch('t') && src[3] == Ch(';'))
1640 {
1641 *dest = Ch('>');
1642 ++dest;
1643 src += 4;
1644 continue;
1645 }
1646 break;
1647
1648 // &lt;
1649 case Ch('l'):
1650 if (src[2] == Ch('t') && src[3] == Ch(';'))
1651 {
1652 *dest = Ch('<');
1653 ++dest;
1654 src += 4;
1655 continue;
1656 }
1657 break;
1658
1659 // &#...; - assumes ASCII
1660 case Ch('#'):
1661 if (src[2] == Ch('x'))
1662 {
1663 unsigned long code = 0;
1664 src += 3; // Skip &#x
1665 while (1)
1666 {
1667 unsigned char digit = internal::lookup_tables<0>::lookup_digits[static_cast<unsigned char>(*src)];
1668 if (digit == 0xFF)
1669 break;
1670 code = code * 16 + digit;
1671 ++src;
1672 }
1673 insert_coded_character<Flags>(dest, code); // Put character in output
1674 }
1675 else
1676 {
1677 unsigned long code = 0;
1678 src += 2; // Skip &#
1679 while (1)
1680 {
1681 unsigned char digit = internal::lookup_tables<0>::lookup_digits[static_cast<unsigned char>(*src)];
1682 if (digit == 0xFF)
1683 break;
1684 code = code * 10 + digit;
1685 ++src;
1686 }
1687 insert_coded_character<Flags>(dest, code); // Put character in output
1688 }
1689 if (*src == Ch(';'))
1690 ++src;
1691 else
1692 RAPIDXML_PARSE_ERROR("expected ;", src);
1693 continue;
1694
1695 // Something else
1696 default:
1697 // Ignore, just copy '&' verbatim
1698 break;
1699
1700 }
1701 }
1702 }
1703
1704 // If whitespace condensing is enabled
1705 if (Flags & parse_normalize_whitespace)
1706 {
1707 // Test if condensing is needed
1708 if (whitespace_pred::test(*src))
1709 {
1710 *dest = Ch(' '); ++dest; // Put single space in dest
1711 ++src; // Skip first whitespace char
1712 // Skip remaining whitespace chars
1713 while (whitespace_pred::test(*src))
1714 ++src;
1715 continue;
1716 }
1717 }
1718
1719 // No replacement, only copy character
1720 *dest++ = *src++;
1721
1722 }
1723
1724 // Return new end
1725 text = src;
1726 return dest;
1727
1728 }
1729
1730 ///////////////////////////////////////////////////////////////////////
1731 // Internal parsing functions
1732
1733 // Parse BOM, if any
1734 template<int Flags>
1735 void parse_bom(Ch *&text)
1736 {
1737 // UTF-8?
1738 if (static_cast<unsigned char>(text[0]) == 0xEF &&
1739 static_cast<unsigned char>(text[1]) == 0xBB &&
1740 static_cast<unsigned char>(text[2]) == 0xBF)
1741 {
1742 text += 3; // Skup utf-8 bom
1743 }
1744 }
1745
1746 // Parse XML declaration (<?xml...)
1747 template<int Flags>
1748 xml_node<Ch> *parse_xml_declaration(Ch *&text)
1749 {
1750 // If parsing of declaration is disabled
1751 if (!(Flags & parse_declaration_node))
1752 {
1753 // Skip until end of declaration
1754 while (text[0] != Ch('?') || text[1] != Ch('>'))
1755 {
1756 if (!text[0]) {
1757 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1758 return 0;
1759 }
1760 ++text;
1761 }
1762 text += 2; // Skip '?>'
1763 return 0;
1764 }
1765
1766 // Create declaration
1767 xml_node<Ch> *declaration = this->allocate_node(node_declaration);
1768
1769 // Skip whitespace before attributes or ?>
1770 skip<whitespace_pred, Flags>(text);
1771
1772 // Parse declaration attributes
1773 parse_node_attributes<Flags>(text, declaration);
1774
1775 // Skip ?>
1776 if (text[0] != Ch('?') || text[1] != Ch('>'))
1777 RAPIDXML_PARSE_ERROR("expected ?>", text);
1778 text += 2;
1779
1780 return declaration;
1781 }
1782
1783 // Parse XML comment (<!--...)
1784 template<int Flags>
1785 xml_node<Ch> *parse_comment(Ch *&text)
1786 {
1787 // If parsing of comments is disabled
1788 if (!(Flags & parse_comment_nodes))
1789 {
1790 // Skip until end of comment
1791 while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>'))
1792 {
1793 if (!text[0]) {
1794 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1795 return 0;
1796 }
1797 ++text;
1798 }
1799 text += 3; // Skip '-->'
1800 return 0; // Do not produce comment node
1801 }
1802
1803 // Remember value start
1804 Ch *value = text;
1805
1806 // Skip until end of comment
1807 while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>'))
1808 {
1809 if (!text[0]) {
1810 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1811 return 0;
1812 }
1813 ++text;
1814 }
1815
1816 // Create comment node
1817 xml_node<Ch> *comment = this->allocate_node(node_comment);
1818 comment->value(value, text - value);
1819
1820 // Place zero terminator after comment value
1821 if (!(Flags & parse_no_string_terminators))
1822 *text = Ch('\0');
1823
1824 text += 3; // Skip '-->'
1825 return comment;
1826 }
1827
1828 // Parse DOCTYPE
1829 template<int Flags>
1830 xml_node<Ch> *parse_doctype(Ch *&text)
1831 {
1832 // Remember value start
1833 Ch *value = text;
1834
1835 // Skip to >
1836 while (*text != Ch('>'))
1837 {
1838 // Determine character type
1839 switch (*text)
1840 {
1841
1842 // If '[' encountered, scan for matching ending ']' using naive algorithm with depth
1843 // This works for all W3C test files except for 2 most wicked
1844 case Ch('['):
1845 {
1846 ++text; // Skip '['
1847 int depth = 1;
1848 while (depth > 0)
1849 {
1850 switch (*text)
1851 {
1852 case Ch('['): ++depth; break;
1853 case Ch(']'): --depth; break;
1854 case 0: RAPIDXML_PARSE_ERROR("unexpected end of data", text); return 0;
1855 }
1856 ++text;
1857 }
1858 break;
1859 }
1860
1861 // Error on end of text
1862 case Ch('\0'):
1863 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1864 return 0;
1865
1866 // Other character, skip it
1867 default:
1868 ++text;
1869
1870 }
1871 }
1872
1873 // If DOCTYPE nodes enabled
1874 if (Flags & parse_doctype_node)
1875 {
1876 // Create a new doctype node
1877 xml_node<Ch> *doctype = this->allocate_node(node_doctype);
1878 doctype->value(value, text - value);
1879
1880 // Place zero terminator after value
1881 if (!(Flags & parse_no_string_terminators))
1882 *text = Ch('\0');
1883
1884 text += 1; // skip '>'
1885 return doctype;
1886 }
1887 else
1888 {
1889 text += 1; // skip '>'
1890 return 0;
1891 }
1892
1893 }
1894
1895 // Parse PI
1896 template<int Flags>
1897 xml_node<Ch> *parse_pi(Ch *&text)
1898 {
1899 // If creation of PI nodes is enabled
1900 if (Flags & parse_pi_nodes)
1901 {
1902 // Create pi node
1903 xml_node<Ch> *pi = this->allocate_node(node_pi);
1904
1905 // Extract PI target name
1906 Ch *name = text;
1907 skip<node_name_pred, Flags>(text);
1908 if (text == name)
1909 RAPIDXML_PARSE_ERROR("expected PI target", text);
1910 pi->name(name, text - name);
1911
1912 // Skip whitespace between pi target and pi
1913 skip<whitespace_pred, Flags>(text);
1914
1915 // Remember start of pi
1916 Ch *value = text;
1917
1918 // Skip to '?>'
1919 while (text[0] != Ch('?') || text[1] != Ch('>'))
1920 {
1921 if (*text == Ch('\0')) {
1922 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1923 return 0;
1924 }
1925 ++text;
1926 }
1927
1928 // Set pi value (verbatim, no entity expansion or whitespace normalization)
1929 pi->value(value, text - value);
1930
1931 // Place zero terminator after name and value
1932 if (!(Flags & parse_no_string_terminators))
1933 {
1934 pi->name()[pi->name_size()] = Ch('\0');
1935 pi->value()[pi->value_size()] = Ch('\0');
1936 }
1937
1938 text += 2; // Skip '?>'
1939 return pi;
1940 }
1941 else
1942 {
1943 // Skip to '?>'
1944 while (text[0] != Ch('?') || text[1] != Ch('>'))
1945 {
1946 if (*text == Ch('\0')) {
1947 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1948 return 0;
1949 }
1950 ++text;
1951 }
1952 text += 2; // Skip '?>'
1953 return 0;
1954 }
1955 }
1956
1957 // Parse and append data
1958 // Return character that ends data.
1959 // This is necessary because this character might have been overwritten by a terminating 0
1960 template<int Flags>
1961 Ch parse_and_append_data(xml_node<Ch> *node, Ch *&text, Ch *contents_start)
1962 {
1963 // Backup to contents start if whitespace trimming is disabled
1964 if (!(Flags & parse_trim_whitespace))
1965 text = contents_start;
1966
1967 // Skip until end of data
1968 Ch *value = text, *end;
1969 if (Flags & parse_normalize_whitespace)
1970 end = skip_and_expand_character_refs<text_pred, text_pure_with_ws_pred, Flags>(text);
1971 else
1972 end = skip_and_expand_character_refs<text_pred, text_pure_no_ws_pred, Flags>(text);
1973
1974 // Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after >
1975 if (Flags & parse_trim_whitespace)
1976 {
1977 if (Flags & parse_normalize_whitespace)
1978 {
1979 // Whitespace is already condensed to single space characters by skipping function, so just trim 1 char off the end
1980 if (*(end - 1) == Ch(' '))
1981 --end;
1982 }
1983 else
1984 {
1985 // Backup until non-whitespace character is found
1986 while (whitespace_pred::test(*(end - 1)))
1987 --end;
1988 }
1989 }
1990
1991 // If characters are still left between end and value (this test is only necessary if normalization is enabled)
1992 // Create new data node
1993 if (!(Flags & parse_no_data_nodes))
1994 {
1995 xml_node<Ch> *data = this->allocate_node(node_data);
1996 data->value(value, end - value);
1997 node->append_node(data);
1998 }
1999
2000 // Add data to parent node if no data exists yet
2001 if (!(Flags & parse_no_element_values))
2002 if (*node->value() == Ch('\0'))
2003 node->value(value, end - value);
2004
2005 // Place zero terminator after value
2006 if (!(Flags & parse_no_string_terminators))
2007 {
2008 Ch ch = *text;
2009 *end = Ch('\0');
2010 return ch; // Return character that ends data; this is required because zero terminator overwritten it
2011 }
2012
2013 // Return character that ends data
2014 return *text;
2015 }
2016
2017 // Parse CDATA
2018 template<int Flags>
2019 xml_node<Ch> *parse_cdata(Ch *&text)
2020 {
2021 // If CDATA is disabled
2022 if (Flags & parse_no_data_nodes)
2023 {
2024 // Skip until end of cdata
2025 while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>'))
2026 {
2027 if (!text[0]) {
2028 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
2029 return 0;
2030 }
2031 ++text;
2032 }
2033 text += 3; // Skip ]]>
2034 return 0; // Do not produce CDATA node
2035 }
2036
2037 // Skip until end of cdata
2038 Ch *value = text;
2039 while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>'))
2040 {
2041 if (!text[0]) {
2042 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
2043 return 0;
2044 }
2045 ++text;
2046 }
2047
2048 // Create new cdata node
2049 xml_node<Ch> *cdata = this->allocate_node(node_cdata);
2050 cdata->value(value, text - value);
2051
2052 // Place zero terminator after value
2053 if (!(Flags & parse_no_string_terminators))
2054 *text = Ch('\0');
2055
2056 text += 3; // Skip ]]>
2057 return cdata;
2058 }
2059
2060 // Parse element node
2061 template<int Flags>
2062 xml_node<Ch> *parse_element(Ch *&text)
2063 {
2064 // Create element node
2065 xml_node<Ch> *element = this->allocate_node(node_element);
2066
2067 // Extract element name
2068 Ch *name = text;
2069 skip<node_name_pred, Flags>(text);
2070 if (text == name)
2071 RAPIDXML_PARSE_ERROR("expected element name", text);
2072 element->name(name, text - name);
2073
2074 // Skip whitespace between element name and attributes or >
2075 skip<whitespace_pred, Flags>(text);
2076
2077 // Parse attributes, if any
2078 parse_node_attributes<Flags>(text, element);
2079
2080 // Determine ending type
2081 if (*text == Ch('>'))
2082 {
2083 ++text;
2084 parse_node_contents<Flags>(text, element);
2085 }
2086 else if (*text == Ch('/'))
2087 {
2088 ++text;
2089 if (*text != Ch('>'))
2090 RAPIDXML_PARSE_ERROR("expected >", text);
2091 ++text;
2092 }
2093 else
2094 RAPIDXML_PARSE_ERROR("expected >", text);
2095
2096 // Place zero terminator after name
2097 if (!(Flags & parse_no_string_terminators))
2098 element->name()[element->name_size()] = Ch('\0');
2099
2100 // Return parsed element
2101 return element;
2102 }
2103
2104 // Determine node type, and parse it
2105 template<int Flags>
2106 xml_node<Ch> *parse_node(Ch *&text)
2107 {
2108 // Parse proper node type
2109 switch (text[0])
2110 {
2111
2112 // <...
2113 default:
2114 // Parse and append element node
2115 return parse_element<Flags>(text);
2116
2117 // <?...
2118 case Ch('?'):
2119 ++text; // Skip ?
2120 if ((text[0] == Ch('x') || text[0] == Ch('X')) &&
2121 (text[1] == Ch('m') || text[1] == Ch('M')) &&
2122 (text[2] == Ch('l') || text[2] == Ch('L')) &&
2123 whitespace_pred::test(text[3]))
2124 {
2125 // '<?xml ' - xml declaration
2126 text += 4; // Skip 'xml '
2127 return parse_xml_declaration<Flags>(text);
2128 }
2129 else
2130 {
2131 // Parse PI
2132 return parse_pi<Flags>(text);
2133 }
2134
2135 // <!...
2136 case Ch('!'):
2137
2138 // Parse proper subset of <! node
2139 switch (text[1])
2140 {
2141
2142 // <!-
2143 case Ch('-'):
2144 if (text[2] == Ch('-'))
2145 {
2146 // '<!--' - xml comment
2147 text += 3; // Skip '!--'
2148 return parse_comment<Flags>(text);
2149 }
2150 break;
2151
2152 // <![
2153 case Ch('['):
2154 if (text[2] == Ch('C') && text[3] == Ch('D') && text[4] == Ch('A') &&
2155 text[5] == Ch('T') && text[6] == Ch('A') && text[7] == Ch('['))
2156 {
2157 // '<![CDATA[' - cdata
2158 text += 8; // Skip '![CDATA['
2159 return parse_cdata<Flags>(text);
2160 }
2161 break;
2162
2163 // <!D
2164 case Ch('D'):
2165 if (text[2] == Ch('O') && text[3] == Ch('C') && text[4] == Ch('T') &&
2166 text[5] == Ch('Y') && text[6] == Ch('P') && text[7] == Ch('E') &&
2167 whitespace_pred::test(text[8]))
2168 {
2169 // '<!DOCTYPE ' - doctype
2170 text += 9; // skip '!DOCTYPE '
2171 return parse_doctype<Flags>(text);
2172 }
2173
2174 } // switch
2175
2176 // Attempt to skip other, unrecognized node types starting with <!
2177 ++text; // Skip !
2178 while (*text != Ch('>'))
2179 {
2180 if (*text == 0) {
2181 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
2182 return 0;
2183 }
2184 ++text;
2185 }
2186 ++text; // Skip '>'
2187 return 0; // No node recognized
2188
2189 }
2190 }
2191
2192 // Parse contents of the node - children, data etc.
2193 template<int Flags>
2194 void parse_node_contents(Ch *&text, xml_node<Ch> *node)
2195 {
2196 // For all children and text
2197 while (1)
2198 {
2199 // Skip whitespace between > and node contents
2200 Ch *contents_start = text; // Store start of node contents before whitespace is skipped
2201 skip<whitespace_pred, Flags>(text);
2202 Ch next_char = *text;
2203
2204 // After data nodes, instead of continuing the loop, control jumps here.
2205 // This is because zero termination inside parse_and_append_data() function
2206 // would wreak havoc with the above code.
2207 // Also, skipping whitespace after data nodes is unnecessary.
2208 after_data_node:
2209
2210 // Determine what comes next: node closing, child node, data node, or 0?
2211 switch (next_char)
2212 {
2213
2214 // Node closing or child node
2215 case Ch('<'):
2216 if (text[1] == Ch('/'))
2217 {
2218 // Node closing
2219 text += 2; // Skip '</'
2220 if (Flags & parse_validate_closing_tags)
2221 {
2222 // Skip and validate closing tag name
2223 Ch *closing_name = text;
2224 skip<node_name_pred, Flags>(text);
2225 if (!internal::compare(node->name(), node->name_size(), closing_name, text - closing_name, true))
2226 RAPIDXML_PARSE_ERROR("invalid closing tag name", text);
2227 }
2228 else
2229 {
2230 // No validation, just skip name
2231 skip<node_name_pred, Flags>(text);
2232 }
2233 // Skip remaining whitespace after node name
2234 skip<whitespace_pred, Flags>(text);
2235 if (*text != Ch('>'))
2236 RAPIDXML_PARSE_ERROR("expected >", text);
2237 ++text; // Skip '>'
2238 return; // Node closed, finished parsing contents
2239 }
2240 else
2241 {
2242 // Child node
2243 ++text; // Skip '<'
2244 if (xml_node<Ch> *child = parse_node<Flags>(text))
2245 node->append_node(child);
2246 }
2247 break;
2248
2249 // End of data - error
2250 case Ch('\0'):
2251 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
2252 return;
2253
2254 // Data node
2255 default:
2256 next_char = parse_and_append_data<Flags>(node, text, contents_start);
2257 goto after_data_node; // Bypass regular processing after data nodes
2258
2259 }
2260 }
2261 }
2262
2263 // Parse XML attributes of the node
2264 template<int Flags>
2265 void parse_node_attributes(Ch *&text, xml_node<Ch> *node)
2266 {
2267 // For all attributes
2268 while (attribute_name_pred::test(*text))
2269 {
2270 // Extract attribute name
2271 Ch *name = text;
2272 ++text; // Skip first character of attribute name
2273 skip<attribute_name_pred, Flags>(text);
2274 if (text == name)
2275 RAPIDXML_PARSE_ERROR("expected attribute name", name);
2276
2277 // Create new attribute
2278 xml_attribute<Ch> *attribute = this->allocate_attribute();
2279 attribute->name(name, text - name);
2280 node->append_attribute(attribute);
2281
2282 // Skip whitespace after attribute name
2283 skip<whitespace_pred, Flags>(text);
2284
2285 // Skip =
2286 if (*text != Ch('='))
2287 RAPIDXML_PARSE_ERROR("expected =", text);
2288 ++text;
2289
2290 // Add terminating zero after name
2291 if (!(Flags & parse_no_string_terminators))
2292 attribute->name()[attribute->name_size()] = 0;
2293
2294 // Skip whitespace after =
2295 skip<whitespace_pred, Flags>(text);
2296
2297 // Skip quote and remember if it was ' or "
2298 Ch quote = *text;
2299 if (quote != Ch('\'') && quote != Ch('"'))
2300 RAPIDXML_PARSE_ERROR("expected ' or \"", text);
2301 ++text;
2302
2303 // Extract attribute value and expand char refs in it
2304 Ch *value = text, *end;
2305 const int AttFlags = Flags & ~parse_normalize_whitespace; // No whitespace normalization in attributes
2306 if (quote == Ch('\''))
2307 end = skip_and_expand_character_refs<attribute_value_pred<Ch('\'')>, attribute_value_pure_pred<Ch('\'')>, AttFlags>(text);
2308 else
2309 end = skip_and_expand_character_refs<attribute_value_pred<Ch('"')>, attribute_value_pure_pred<Ch('"')>, AttFlags>(text);
2310
2311 // Set attribute value
2312 attribute->value(value, end - value);
2313
2314 // Make sure that end quote is present
2315 if (*text != quote)
2316 RAPIDXML_PARSE_ERROR("expected ' or \"", text);
2317 ++text; // Skip quote
2318
2319 // Add terminating zero after value
2320 if (!(Flags & parse_no_string_terminators))
2321 attribute->value()[attribute->value_size()] = 0;
2322
2323 // Skip whitespace after attribute value
2324 skip<whitespace_pred, Flags>(text);
2325 }
2326 }
2327
2328 };
2329
2330 //! \cond internal
2331 namespace internal
2332 {
2333
2334 // Whitespace (space \n \r \t)
2335 template<int Dummy>
2336 const unsigned char lookup_tables<Dummy>::lookup_whitespace[256] =
2337 {
2338 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2339 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, // 0
2340 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
2341 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2
2342 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3
2343 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4
2344 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5
2345 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6
2346 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7
2347 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8
2348 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9
2349 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A
2350 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B
2351 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // C
2352 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D
2353 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // E
2354 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F
2355 };
2356
2357 // Node name (anything but space \n \r \t / > ? \0)
2358 template<int Dummy>
2359 const unsigned char lookup_tables<Dummy>::lookup_node_name[256] =
2360 {
2361 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2362 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
2363 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2364 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 2
2365 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, // 3
2366 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2367 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2368 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2369 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2370 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2371 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2372 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2373 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2374 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2375 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2376 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2377 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2378 };
2379
2380 // Text (i.e. PCDATA) (anything but < \0)
2381 template<int Dummy>
2382 const unsigned char lookup_tables<Dummy>::lookup_text[256] =
2383 {
2384 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2385 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2386 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2387 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2388 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
2389 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2390 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2391 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2392 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2393 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2394 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2395 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2396 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2397 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2398 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2399 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2400 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2401 };
2402
2403 // Text (i.e. PCDATA) that does not require processing when ws normalization is disabled
2404 // (anything but < \0 &)
2405 template<int Dummy>
2406 const unsigned char lookup_tables<Dummy>::lookup_text_pure_no_ws[256] =
2407 {
2408 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2409 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2410 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2411 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2412 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
2413 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2414 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2415 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2416 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2417 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2418 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2419 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2420 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2421 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2422 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2423 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2424 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2425 };
2426
2427 // Text (i.e. PCDATA) that does not require processing when ws normalizationis is enabled
2428 // (anything but < \0 & space \n \r \t)
2429 template<int Dummy>
2430 const unsigned char lookup_tables<Dummy>::lookup_text_pure_with_ws[256] =
2431 {
2432 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2433 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
2434 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2435 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2436 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
2437 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2438 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2439 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2440 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2441 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2442 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2443 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2444 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2445 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2446 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2447 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2448 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2449 };
2450
2451 // Attribute name (anything but space \n \r \t / < > = ? ! \0)
2452 template<int Dummy>
2453 const unsigned char lookup_tables<Dummy>::lookup_attribute_name[256] =
2454 {
2455 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2456 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
2457 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2458 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 2
2459 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, // 3
2460 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2461 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2462 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2463 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2464 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2465 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2466 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2467 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2468 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2469 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2470 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2471 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2472 };
2473
2474 // Attribute data with single quote (anything but ' \0)
2475 template<int Dummy>
2476 const unsigned char lookup_tables<Dummy>::lookup_attribute_data_1[256] =
2477 {
2478 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2479 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2480 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2481 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2482 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
2483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2484 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2485 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2486 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2487 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2488 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2489 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2490 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2491 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2492 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2493 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2494 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2495 };
2496
2497 // Attribute data with single quote that does not require processing (anything but ' \0 &)
2498 template<int Dummy>
2499 const unsigned char lookup_tables<Dummy>::lookup_attribute_data_1_pure[256] =
2500 {
2501 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2502 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2503 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2504 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2505 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
2506 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2507 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2508 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2509 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2510 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2511 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2512 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2513 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2514 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2515 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2516 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2517 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2518 };
2519
2520 // Attribute data with double quote (anything but " \0)
2521 template<int Dummy>
2522 const unsigned char lookup_tables<Dummy>::lookup_attribute_data_2[256] =
2523 {
2524 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2525 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2527 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2528 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
2529 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2530 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2531 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2532 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2533 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2534 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2535 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2536 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2537 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2538 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2539 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2540 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2541 };
2542
2543 // Attribute data with double quote that does not require processing (anything but " \0 &)
2544 template<int Dummy>
2545 const unsigned char lookup_tables<Dummy>::lookup_attribute_data_2_pure[256] =
2546 {
2547 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2548 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2549 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2550 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2551 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
2552 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2553 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2554 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2555 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2556 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2557 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2558 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2559 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2560 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2561 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2562 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2563 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2564 };
2565
2566 // Digits (dec and hex, 255 denotes end of numeric character reference)
2567 template<int Dummy>
2568 const unsigned char lookup_tables<Dummy>::lookup_digits[256] =
2569 {
2570 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2571 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 0
2572 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 1
2573 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 2
2574 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,255,255,255,255,255,255, // 3
2575 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 4
2576 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 5
2577 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 6
2578 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 7
2579 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 8
2580 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 9
2581 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // A
2582 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // B
2583 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // C
2584 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // D
2585 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // E
2586 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 // F
2587 };
2588
2589 // Upper case conversion
2590 template<int Dummy>
2591 const unsigned char lookup_tables<Dummy>::lookup_upcase[256] =
2592 {
2593 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A B C D E F
2594 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0
2595 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 1
2596 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, // 2
2597 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, // 3
2598 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 4
2599 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, // 5
2600 96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 6
2601 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 123,124,125,126,127, // 7
2602 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, // 8
2603 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, // 9
2604 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, // A
2605 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, // B
2606 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, // C
2607 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, // D
2608 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, // E
2609 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 // F
2610 };
2611 }
2612 //! \endcond
2613
2614}
2615
2616// Undefine internal macros
2617#undef RAPIDXML_PARSE_ERROR
2618
2619// On MSVC, restore warnings state
2620#ifdef _MSC_VER
2621 #pragma warning(pop)
2622#endif
2623
2624#endif