blob: d98173ecd04257c6d98769545d00753411a32c0f [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001#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);
1393
1394 // Parse children
1395 while (1)
1396 {
1397 // Skip whitespace before node
1398 skip<whitespace_pred, Flags>(text);
1399 if (*text == 0)
1400 break;
1401
1402 // Parse and append new child
1403 if (*text == Ch('<'))
1404 {
1405 ++text; // Skip '<'
1406 if (xml_node<Ch> *node = parse_node<Flags>(text))
1407 this->append_node(node);
1408 }
1409 else
1410 RAPIDXML_PARSE_ERROR("expected <", text);
1411 }
1412
1413 }
1414
1415 //! Clears the document by deleting all nodes and clearing the memory pool.
1416 //! All nodes owned by document pool are destroyed.
1417 void clear()
1418 {
1419 this->remove_all_nodes();
1420 this->remove_all_attributes();
1421 memory_pool<Ch>::clear();
1422 }
1423
1424 private:
1425
1426 ///////////////////////////////////////////////////////////////////////
1427 // Internal character utility functions
1428
1429 // Detect whitespace character
1430 struct whitespace_pred
1431 {
1432 static unsigned char test(Ch ch)
1433 {
1434 return internal::lookup_tables<0>::lookup_whitespace[static_cast<unsigned char>(ch)];
1435 }
1436 };
1437
1438 // Detect node name character
1439 struct node_name_pred
1440 {
1441 static unsigned char test(Ch ch)
1442 {
1443 return internal::lookup_tables<0>::lookup_node_name[static_cast<unsigned char>(ch)];
1444 }
1445 };
1446
1447 // Detect attribute name character
1448 struct attribute_name_pred
1449 {
1450 static unsigned char test(Ch ch)
1451 {
1452 return internal::lookup_tables<0>::lookup_attribute_name[static_cast<unsigned char>(ch)];
1453 }
1454 };
1455
1456 // Detect text character (PCDATA)
1457 struct text_pred
1458 {
1459 static unsigned char test(Ch ch)
1460 {
1461 return internal::lookup_tables<0>::lookup_text[static_cast<unsigned char>(ch)];
1462 }
1463 };
1464
1465 // Detect text character (PCDATA) that does not require processing
1466 struct text_pure_no_ws_pred
1467 {
1468 static unsigned char test(Ch ch)
1469 {
1470 return internal::lookup_tables<0>::lookup_text_pure_no_ws[static_cast<unsigned char>(ch)];
1471 }
1472 };
1473
1474 // Detect text character (PCDATA) that does not require processing
1475 struct text_pure_with_ws_pred
1476 {
1477 static unsigned char test(Ch ch)
1478 {
1479 return internal::lookup_tables<0>::lookup_text_pure_with_ws[static_cast<unsigned char>(ch)];
1480 }
1481 };
1482
1483 // Detect attribute value character
1484 template<Ch Quote>
1485 struct attribute_value_pred
1486 {
1487 static unsigned char test(Ch ch)
1488 {
1489 if (Quote == Ch('\''))
1490 return internal::lookup_tables<0>::lookup_attribute_data_1[static_cast<unsigned char>(ch)];
1491 if (Quote == Ch('\"'))
1492 return internal::lookup_tables<0>::lookup_attribute_data_2[static_cast<unsigned char>(ch)];
1493 return 0; // Should never be executed, to avoid warnings on Comeau
1494 }
1495 };
1496
1497 // Detect attribute value character
1498 template<Ch Quote>
1499 struct attribute_value_pure_pred
1500 {
1501 static unsigned char test(Ch ch)
1502 {
1503 if (Quote == Ch('\''))
1504 return internal::lookup_tables<0>::lookup_attribute_data_1_pure[static_cast<unsigned char>(ch)];
1505 if (Quote == Ch('\"'))
1506 return internal::lookup_tables<0>::lookup_attribute_data_2_pure[static_cast<unsigned char>(ch)];
1507 return 0; // Should never be executed, to avoid warnings on Comeau
1508 }
1509 };
1510
1511 // Insert coded character, using UTF8 or 8-bit ASCII
1512 template<int Flags>
1513 static void insert_coded_character(Ch *&text, unsigned long code)
1514 {
1515 if (Flags & parse_no_utf8)
1516 {
1517 // Insert 8-bit ASCII character
1518 // Todo: possibly verify that code is less than 256 and use replacement char otherwise?
1519 text[0] = static_cast<unsigned char>(code);
1520 text += 1;
1521 }
1522 else
1523 {
1524 // Insert UTF8 sequence
1525 if (code < 0x80) // 1 byte sequence
1526 {
1527 text[0] = static_cast<unsigned char>(code);
1528 text += 1;
1529 }
1530 else if (code < 0x800) // 2 byte sequence
1531 {
1532 text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1533 text[0] = static_cast<unsigned char>(code | 0xC0);
1534 text += 2;
1535 }
1536 else if (code < 0x10000) // 3 byte sequence
1537 {
1538 text[2] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1539 text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
1540 text[0] = static_cast<unsigned char>(code | 0xE0);
1541 text += 3;
1542 }
1543 else if (code < 0x110000) // 4 byte sequence
1544 {
1545 text[3] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
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 | 0xF0);
1549 text += 4;
1550 }
1551 else // Invalid, only codes up to 0x10FFFF are allowed in Unicode
1552 {
1553 RAPIDXML_PARSE_ERROR("invalid numeric character entity", text);
1554 }
1555 }
1556 }
1557
1558 // Skip characters until predicate evaluates to true
1559 template<class StopPred, int Flags>
1560 static void skip(Ch *&text)
1561 {
1562 Ch *tmp = text;
1563 while (StopPred::test(*tmp))
1564 ++tmp;
1565 text = tmp;
1566 }
1567
1568 // Skip characters until predicate evaluates to true while doing the following:
1569 // - replacing XML character entity references with proper characters (&apos; &amp; &quot; &lt; &gt; &#...;)
1570 // - condensing whitespace sequences to single space character
1571 template<class StopPred, class StopPredPure, int Flags>
1572 static Ch *skip_and_expand_character_refs(Ch *&text)
1573 {
1574 // If entity translation, whitespace condense and whitespace trimming is disabled, use plain skip
1575 if (Flags & parse_no_entity_translation &&
1576 !(Flags & parse_normalize_whitespace) &&
1577 !(Flags & parse_trim_whitespace))
1578 {
1579 skip<StopPred, Flags>(text);
1580 return text;
1581 }
1582
1583 // Use simple skip until first modification is detected
1584 skip<StopPredPure, Flags>(text);
1585
1586 // Use translation skip
1587 Ch *src = text;
1588 Ch *dest = src;
1589 while (StopPred::test(*src))
1590 {
1591 // If entity translation is enabled
1592 if (!(Flags & parse_no_entity_translation))
1593 {
1594 // Test if replacement is needed
1595 if (src[0] == Ch('&'))
1596 {
1597 switch (src[1])
1598 {
1599
1600 // &amp; &apos;
1601 case Ch('a'):
1602 if (src[2] == Ch('m') && src[3] == Ch('p') && src[4] == Ch(';'))
1603 {
1604 *dest = Ch('&');
1605 ++dest;
1606 src += 5;
1607 continue;
1608 }
1609 if (src[2] == Ch('p') && src[3] == Ch('o') && src[4] == Ch('s') && src[5] == Ch(';'))
1610 {
1611 *dest = Ch('\'');
1612 ++dest;
1613 src += 6;
1614 continue;
1615 }
1616 break;
1617
1618 // &quot;
1619 case Ch('q'):
1620 if (src[2] == Ch('u') && src[3] == Ch('o') && src[4] == Ch('t') && src[5] == Ch(';'))
1621 {
1622 *dest = Ch('"');
1623 ++dest;
1624 src += 6;
1625 continue;
1626 }
1627 break;
1628
1629 // &gt;
1630 case Ch('g'):
1631 if (src[2] == Ch('t') && src[3] == Ch(';'))
1632 {
1633 *dest = Ch('>');
1634 ++dest;
1635 src += 4;
1636 continue;
1637 }
1638 break;
1639
1640 // &lt;
1641 case Ch('l'):
1642 if (src[2] == Ch('t') && src[3] == Ch(';'))
1643 {
1644 *dest = Ch('<');
1645 ++dest;
1646 src += 4;
1647 continue;
1648 }
1649 break;
1650
1651 // &#...; - assumes ASCII
1652 case Ch('#'):
1653 if (src[2] == Ch('x'))
1654 {
1655 unsigned long code = 0;
1656 src += 3; // Skip &#x
1657 while (1)
1658 {
1659 unsigned char digit = internal::lookup_tables<0>::lookup_digits[static_cast<unsigned char>(*src)];
1660 if (digit == 0xFF)
1661 break;
1662 code = code * 16 + digit;
1663 ++src;
1664 }
1665 insert_coded_character<Flags>(dest, code); // Put character in output
1666 }
1667 else
1668 {
1669 unsigned long code = 0;
1670 src += 2; // Skip &#
1671 while (1)
1672 {
1673 unsigned char digit = internal::lookup_tables<0>::lookup_digits[static_cast<unsigned char>(*src)];
1674 if (digit == 0xFF)
1675 break;
1676 code = code * 10 + digit;
1677 ++src;
1678 }
1679 insert_coded_character<Flags>(dest, code); // Put character in output
1680 }
1681 if (*src == Ch(';'))
1682 ++src;
1683 else
1684 RAPIDXML_PARSE_ERROR("expected ;", src);
1685 continue;
1686
1687 // Something else
1688 default:
1689 // Ignore, just copy '&' verbatim
1690 break;
1691
1692 }
1693 }
1694 }
1695
1696 // If whitespace condensing is enabled
1697 if (Flags & parse_normalize_whitespace)
1698 {
1699 // Test if condensing is needed
1700 if (whitespace_pred::test(*src))
1701 {
1702 *dest = Ch(' '); ++dest; // Put single space in dest
1703 ++src; // Skip first whitespace char
1704 // Skip remaining whitespace chars
1705 while (whitespace_pred::test(*src))
1706 ++src;
1707 continue;
1708 }
1709 }
1710
1711 // No replacement, only copy character
1712 *dest++ = *src++;
1713
1714 }
1715
1716 // Return new end
1717 text = src;
1718 return dest;
1719
1720 }
1721
1722 ///////////////////////////////////////////////////////////////////////
1723 // Internal parsing functions
1724
1725 // Parse BOM, if any
1726 template<int Flags>
1727 void parse_bom(Ch *&text)
1728 {
1729 // UTF-8?
1730 if (static_cast<unsigned char>(text[0]) == 0xEF &&
1731 static_cast<unsigned char>(text[1]) == 0xBB &&
1732 static_cast<unsigned char>(text[2]) == 0xBF)
1733 {
1734 text += 3; // Skup utf-8 bom
1735 }
1736 }
1737
1738 // Parse XML declaration (<?xml...)
1739 template<int Flags>
1740 xml_node<Ch> *parse_xml_declaration(Ch *&text)
1741 {
1742 // If parsing of declaration is disabled
1743 if (!(Flags & parse_declaration_node))
1744 {
1745 // Skip until end of declaration
1746 while (text[0] != Ch('?') || text[1] != Ch('>'))
1747 {
1748 if (!text[0])
1749 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1750 ++text;
1751 }
1752 text += 2; // Skip '?>'
1753 return 0;
1754 }
1755
1756 // Create declaration
1757 xml_node<Ch> *declaration = this->allocate_node(node_declaration);
1758
1759 // Skip whitespace before attributes or ?>
1760 skip<whitespace_pred, Flags>(text);
1761
1762 // Parse declaration attributes
1763 parse_node_attributes<Flags>(text, declaration);
1764
1765 // Skip ?>
1766 if (text[0] != Ch('?') || text[1] != Ch('>'))
1767 RAPIDXML_PARSE_ERROR("expected ?>", text);
1768 text += 2;
1769
1770 return declaration;
1771 }
1772
1773 // Parse XML comment (<!--...)
1774 template<int Flags>
1775 xml_node<Ch> *parse_comment(Ch *&text)
1776 {
1777 // If parsing of comments is disabled
1778 if (!(Flags & parse_comment_nodes))
1779 {
1780 // Skip until end of comment
1781 while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>'))
1782 {
1783 if (!text[0])
1784 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1785 ++text;
1786 }
1787 text += 3; // Skip '-->'
1788 return 0; // Do not produce comment node
1789 }
1790
1791 // Remember value start
1792 Ch *value = text;
1793
1794 // Skip until end of comment
1795 while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>'))
1796 {
1797 if (!text[0])
1798 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1799 ++text;
1800 }
1801
1802 // Create comment node
1803 xml_node<Ch> *comment = this->allocate_node(node_comment);
1804 comment->value(value, text - value);
1805
1806 // Place zero terminator after comment value
1807 if (!(Flags & parse_no_string_terminators))
1808 *text = Ch('\0');
1809
1810 text += 3; // Skip '-->'
1811 return comment;
1812 }
1813
1814 // Parse DOCTYPE
1815 template<int Flags>
1816 xml_node<Ch> *parse_doctype(Ch *&text)
1817 {
1818 // Remember value start
1819 Ch *value = text;
1820
1821 // Skip to >
1822 while (*text != Ch('>'))
1823 {
1824 // Determine character type
1825 switch (*text)
1826 {
1827
1828 // If '[' encountered, scan for matching ending ']' using naive algorithm with depth
1829 // This works for all W3C test files except for 2 most wicked
1830 case Ch('['):
1831 {
1832 ++text; // Skip '['
1833 int depth = 1;
1834 while (depth > 0)
1835 {
1836 switch (*text)
1837 {
1838 case Ch('['): ++depth; break;
1839 case Ch(']'): --depth; break;
1840 case 0: RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1841 }
1842 ++text;
1843 }
1844 break;
1845 }
1846
1847 // Error on end of text
1848 case Ch('\0'):
1849 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1850
1851 // Other character, skip it
1852 default:
1853 ++text;
1854
1855 }
1856 }
1857
1858 // If DOCTYPE nodes enabled
1859 if (Flags & parse_doctype_node)
1860 {
1861 // Create a new doctype node
1862 xml_node<Ch> *doctype = this->allocate_node(node_doctype);
1863 doctype->value(value, text - value);
1864
1865 // Place zero terminator after value
1866 if (!(Flags & parse_no_string_terminators))
1867 *text = Ch('\0');
1868
1869 text += 1; // skip '>'
1870 return doctype;
1871 }
1872 else
1873 {
1874 text += 1; // skip '>'
1875 return 0;
1876 }
1877
1878 }
1879
1880 // Parse PI
1881 template<int Flags>
1882 xml_node<Ch> *parse_pi(Ch *&text)
1883 {
1884 // If creation of PI nodes is enabled
1885 if (Flags & parse_pi_nodes)
1886 {
1887 // Create pi node
1888 xml_node<Ch> *pi = this->allocate_node(node_pi);
1889
1890 // Extract PI target name
1891 Ch *name = text;
1892 skip<node_name_pred, Flags>(text);
1893 if (text == name)
1894 RAPIDXML_PARSE_ERROR("expected PI target", text);
1895 pi->name(name, text - name);
1896
1897 // Skip whitespace between pi target and pi
1898 skip<whitespace_pred, Flags>(text);
1899
1900 // Remember start of pi
1901 Ch *value = text;
1902
1903 // Skip to '?>'
1904 while (text[0] != Ch('?') || text[1] != Ch('>'))
1905 {
1906 if (*text == Ch('\0'))
1907 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1908 ++text;
1909 }
1910
1911 // Set pi value (verbatim, no entity expansion or whitespace normalization)
1912 pi->value(value, text - value);
1913
1914 // Place zero terminator after name and value
1915 if (!(Flags & parse_no_string_terminators))
1916 {
1917 pi->name()[pi->name_size()] = Ch('\0');
1918 pi->value()[pi->value_size()] = Ch('\0');
1919 }
1920
1921 text += 2; // Skip '?>'
1922 return pi;
1923 }
1924 else
1925 {
1926 // Skip to '?>'
1927 while (text[0] != Ch('?') || text[1] != Ch('>'))
1928 {
1929 if (*text == Ch('\0'))
1930 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
1931 ++text;
1932 }
1933 text += 2; // Skip '?>'
1934 return 0;
1935 }
1936 }
1937
1938 // Parse and append data
1939 // Return character that ends data.
1940 // This is necessary because this character might have been overwritten by a terminating 0
1941 template<int Flags>
1942 Ch parse_and_append_data(xml_node<Ch> *node, Ch *&text, Ch *contents_start)
1943 {
1944 // Backup to contents start if whitespace trimming is disabled
1945 if (!(Flags & parse_trim_whitespace))
1946 text = contents_start;
1947
1948 // Skip until end of data
1949 Ch *value = text, *end;
1950 if (Flags & parse_normalize_whitespace)
1951 end = skip_and_expand_character_refs<text_pred, text_pure_with_ws_pred, Flags>(text);
1952 else
1953 end = skip_and_expand_character_refs<text_pred, text_pure_no_ws_pred, Flags>(text);
1954
1955 // Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after >
1956 if (Flags & parse_trim_whitespace)
1957 {
1958 if (Flags & parse_normalize_whitespace)
1959 {
1960 // Whitespace is already condensed to single space characters by skipping function, so just trim 1 char off the end
1961 if (*(end - 1) == Ch(' '))
1962 --end;
1963 }
1964 else
1965 {
1966 // Backup until non-whitespace character is found
1967 while (whitespace_pred::test(*(end - 1)))
1968 --end;
1969 }
1970 }
1971
1972 // If characters are still left between end and value (this test is only necessary if normalization is enabled)
1973 // Create new data node
1974 if (!(Flags & parse_no_data_nodes))
1975 {
1976 xml_node<Ch> *data = this->allocate_node(node_data);
1977 data->value(value, end - value);
1978 node->append_node(data);
1979 }
1980
1981 // Add data to parent node if no data exists yet
1982 if (!(Flags & parse_no_element_values))
1983 if (*node->value() == Ch('\0'))
1984 node->value(value, end - value);
1985
1986 // Place zero terminator after value
1987 if (!(Flags & parse_no_string_terminators))
1988 {
1989 Ch ch = *text;
1990 *end = Ch('\0');
1991 return ch; // Return character that ends data; this is required because zero terminator overwritten it
1992 }
1993
1994 // Return character that ends data
1995 return *text;
1996 }
1997
1998 // Parse CDATA
1999 template<int Flags>
2000 xml_node<Ch> *parse_cdata(Ch *&text)
2001 {
2002 // If CDATA is disabled
2003 if (Flags & parse_no_data_nodes)
2004 {
2005 // Skip until end of cdata
2006 while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>'))
2007 {
2008 if (!text[0])
2009 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
2010 ++text;
2011 }
2012 text += 3; // Skip ]]>
2013 return 0; // Do not produce CDATA node
2014 }
2015
2016 // Skip until end of cdata
2017 Ch *value = text;
2018 while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>'))
2019 {
2020 if (!text[0])
2021 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
2022 ++text;
2023 }
2024
2025 // Create new cdata node
2026 xml_node<Ch> *cdata = this->allocate_node(node_cdata);
2027 cdata->value(value, text - value);
2028
2029 // Place zero terminator after value
2030 if (!(Flags & parse_no_string_terminators))
2031 *text = Ch('\0');
2032
2033 text += 3; // Skip ]]>
2034 return cdata;
2035 }
2036
2037 // Parse element node
2038 template<int Flags>
2039 xml_node<Ch> *parse_element(Ch *&text)
2040 {
2041 // Create element node
2042 xml_node<Ch> *element = this->allocate_node(node_element);
2043
2044 // Extract element name
2045 Ch *name = text;
2046 skip<node_name_pred, Flags>(text);
2047 if (text == name)
2048 RAPIDXML_PARSE_ERROR("expected element name", text);
2049 element->name(name, text - name);
2050
2051 // Skip whitespace between element name and attributes or >
2052 skip<whitespace_pred, Flags>(text);
2053
2054 // Parse attributes, if any
2055 parse_node_attributes<Flags>(text, element);
2056
2057 // Determine ending type
2058 if (*text == Ch('>'))
2059 {
2060 ++text;
2061 parse_node_contents<Flags>(text, element);
2062 }
2063 else if (*text == Ch('/'))
2064 {
2065 ++text;
2066 if (*text != Ch('>'))
2067 RAPIDXML_PARSE_ERROR("expected >", text);
2068 ++text;
2069 }
2070 else
2071 RAPIDXML_PARSE_ERROR("expected >", text);
2072
2073 // Place zero terminator after name
2074 if (!(Flags & parse_no_string_terminators))
2075 element->name()[element->name_size()] = Ch('\0');
2076
2077 // Return parsed element
2078 return element;
2079 }
2080
2081 // Determine node type, and parse it
2082 template<int Flags>
2083 xml_node<Ch> *parse_node(Ch *&text)
2084 {
2085 // Parse proper node type
2086 switch (text[0])
2087 {
2088
2089 // <...
2090 default:
2091 // Parse and append element node
2092 return parse_element<Flags>(text);
2093
2094 // <?...
2095 case Ch('?'):
2096 ++text; // Skip ?
2097 if ((text[0] == Ch('x') || text[0] == Ch('X')) &&
2098 (text[1] == Ch('m') || text[1] == Ch('M')) &&
2099 (text[2] == Ch('l') || text[2] == Ch('L')) &&
2100 whitespace_pred::test(text[3]))
2101 {
2102 // '<?xml ' - xml declaration
2103 text += 4; // Skip 'xml '
2104 return parse_xml_declaration<Flags>(text);
2105 }
2106 else
2107 {
2108 // Parse PI
2109 return parse_pi<Flags>(text);
2110 }
2111
2112 // <!...
2113 case Ch('!'):
2114
2115 // Parse proper subset of <! node
2116 switch (text[1])
2117 {
2118
2119 // <!-
2120 case Ch('-'):
2121 if (text[2] == Ch('-'))
2122 {
2123 // '<!--' - xml comment
2124 text += 3; // Skip '!--'
2125 return parse_comment<Flags>(text);
2126 }
2127 break;
2128
2129 // <![
2130 case Ch('['):
2131 if (text[2] == Ch('C') && text[3] == Ch('D') && text[4] == Ch('A') &&
2132 text[5] == Ch('T') && text[6] == Ch('A') && text[7] == Ch('['))
2133 {
2134 // '<![CDATA[' - cdata
2135 text += 8; // Skip '![CDATA['
2136 return parse_cdata<Flags>(text);
2137 }
2138 break;
2139
2140 // <!D
2141 case Ch('D'):
2142 if (text[2] == Ch('O') && text[3] == Ch('C') && text[4] == Ch('T') &&
2143 text[5] == Ch('Y') && text[6] == Ch('P') && text[7] == Ch('E') &&
2144 whitespace_pred::test(text[8]))
2145 {
2146 // '<!DOCTYPE ' - doctype
2147 text += 9; // skip '!DOCTYPE '
2148 return parse_doctype<Flags>(text);
2149 }
2150
2151 } // switch
2152
2153 // Attempt to skip other, unrecognized node types starting with <!
2154 ++text; // Skip !
2155 while (*text != Ch('>'))
2156 {
2157 if (*text == 0)
2158 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
2159 ++text;
2160 }
2161 ++text; // Skip '>'
2162 return 0; // No node recognized
2163
2164 }
2165 }
2166
2167 // Parse contents of the node - children, data etc.
2168 template<int Flags>
2169 void parse_node_contents(Ch *&text, xml_node<Ch> *node)
2170 {
2171 // For all children and text
2172 while (1)
2173 {
2174 // Skip whitespace between > and node contents
2175 Ch *contents_start = text; // Store start of node contents before whitespace is skipped
2176 skip<whitespace_pred, Flags>(text);
2177 Ch next_char = *text;
2178
2179 // After data nodes, instead of continuing the loop, control jumps here.
2180 // This is because zero termination inside parse_and_append_data() function
2181 // would wreak havoc with the above code.
2182 // Also, skipping whitespace after data nodes is unnecessary.
2183 after_data_node:
2184
2185 // Determine what comes next: node closing, child node, data node, or 0?
2186 switch (next_char)
2187 {
2188
2189 // Node closing or child node
2190 case Ch('<'):
2191 if (text[1] == Ch('/'))
2192 {
2193 // Node closing
2194 text += 2; // Skip '</'
2195 if (Flags & parse_validate_closing_tags)
2196 {
2197 // Skip and validate closing tag name
2198 Ch *closing_name = text;
2199 skip<node_name_pred, Flags>(text);
2200 if (!internal::compare(node->name(), node->name_size(), closing_name, text - closing_name, true))
2201 RAPIDXML_PARSE_ERROR("invalid closing tag name", text);
2202 }
2203 else
2204 {
2205 // No validation, just skip name
2206 skip<node_name_pred, Flags>(text);
2207 }
2208 // Skip remaining whitespace after node name
2209 skip<whitespace_pred, Flags>(text);
2210 if (*text != Ch('>'))
2211 RAPIDXML_PARSE_ERROR("expected >", text);
2212 ++text; // Skip '>'
2213 return; // Node closed, finished parsing contents
2214 }
2215 else
2216 {
2217 // Child node
2218 ++text; // Skip '<'
2219 if (xml_node<Ch> *child = parse_node<Flags>(text))
2220 node->append_node(child);
2221 }
2222 break;
2223
2224 // End of data - error
2225 case Ch('\0'):
2226 RAPIDXML_PARSE_ERROR("unexpected end of data", text);
2227
2228 // Data node
2229 default:
2230 next_char = parse_and_append_data<Flags>(node, text, contents_start);
2231 goto after_data_node; // Bypass regular processing after data nodes
2232
2233 }
2234 }
2235 }
2236
2237 // Parse XML attributes of the node
2238 template<int Flags>
2239 void parse_node_attributes(Ch *&text, xml_node<Ch> *node)
2240 {
2241 // For all attributes
2242 while (attribute_name_pred::test(*text))
2243 {
2244 // Extract attribute name
2245 Ch *name = text;
2246 ++text; // Skip first character of attribute name
2247 skip<attribute_name_pred, Flags>(text);
2248 if (text == name)
2249 RAPIDXML_PARSE_ERROR("expected attribute name", name);
2250
2251 // Create new attribute
2252 xml_attribute<Ch> *attribute = this->allocate_attribute();
2253 attribute->name(name, text - name);
2254 node->append_attribute(attribute);
2255
2256 // Skip whitespace after attribute name
2257 skip<whitespace_pred, Flags>(text);
2258
2259 // Skip =
2260 if (*text != Ch('='))
2261 RAPIDXML_PARSE_ERROR("expected =", text);
2262 ++text;
2263
2264 // Add terminating zero after name
2265 if (!(Flags & parse_no_string_terminators))
2266 attribute->name()[attribute->name_size()] = 0;
2267
2268 // Skip whitespace after =
2269 skip<whitespace_pred, Flags>(text);
2270
2271 // Skip quote and remember if it was ' or "
2272 Ch quote = *text;
2273 if (quote != Ch('\'') && quote != Ch('"'))
2274 RAPIDXML_PARSE_ERROR("expected ' or \"", text);
2275 ++text;
2276
2277 // Extract attribute value and expand char refs in it
2278 Ch *value = text, *end;
2279 const int AttFlags = Flags & ~parse_normalize_whitespace; // No whitespace normalization in attributes
2280 if (quote == Ch('\''))
2281 end = skip_and_expand_character_refs<attribute_value_pred<Ch('\'')>, attribute_value_pure_pred<Ch('\'')>, AttFlags>(text);
2282 else
2283 end = skip_and_expand_character_refs<attribute_value_pred<Ch('"')>, attribute_value_pure_pred<Ch('"')>, AttFlags>(text);
2284
2285 // Set attribute value
2286 attribute->value(value, end - value);
2287
2288 // Make sure that end quote is present
2289 if (*text != quote)
2290 RAPIDXML_PARSE_ERROR("expected ' or \"", text);
2291 ++text; // Skip quote
2292
2293 // Add terminating zero after value
2294 if (!(Flags & parse_no_string_terminators))
2295 attribute->value()[attribute->value_size()] = 0;
2296
2297 // Skip whitespace after attribute value
2298 skip<whitespace_pred, Flags>(text);
2299 }
2300 }
2301
2302 };
2303
2304 //! \cond internal
2305 namespace internal
2306 {
2307
2308 // Whitespace (space \n \r \t)
2309 template<int Dummy>
2310 const unsigned char lookup_tables<Dummy>::lookup_whitespace[256] =
2311 {
2312 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2313 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, // 0
2314 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
2315 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2
2316 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3
2317 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4
2318 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5
2319 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6
2320 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7
2321 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8
2322 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9
2323 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A
2324 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B
2325 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // C
2326 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D
2327 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // E
2328 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F
2329 };
2330
2331 // Node name (anything but space \n \r \t / > ? \0)
2332 template<int Dummy>
2333 const unsigned char lookup_tables<Dummy>::lookup_node_name[256] =
2334 {
2335 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2336 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
2337 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2338 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 2
2339 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, // 3
2340 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2341 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2342 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2343 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2344 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2345 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2346 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2347 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2348 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2349 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2350 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2351 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2352 };
2353
2354 // Text (i.e. PCDATA) (anything but < \0)
2355 template<int Dummy>
2356 const unsigned char lookup_tables<Dummy>::lookup_text[256] =
2357 {
2358 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2359 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2360 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2361 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2362 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
2363 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2364 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2365 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2366 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2367 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2368 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2369 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2370 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2371 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2372 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2373 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2374 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2375 };
2376
2377 // Text (i.e. PCDATA) that does not require processing when ws normalization is disabled
2378 // (anything but < \0 &)
2379 template<int Dummy>
2380 const unsigned char lookup_tables<Dummy>::lookup_text_pure_no_ws[256] =
2381 {
2382 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2383 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2384 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2385 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2386 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
2387 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2388 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2389 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2390 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2391 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2392 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2393 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2394 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2395 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2396 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2397 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2398 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2399 };
2400
2401 // Text (i.e. PCDATA) that does not require processing when ws normalizationis is enabled
2402 // (anything but < \0 & space \n \r \t)
2403 template<int Dummy>
2404 const unsigned char lookup_tables<Dummy>::lookup_text_pure_with_ws[256] =
2405 {
2406 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2407 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
2408 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2409 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2410 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
2411 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2412 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2413 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2414 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2415 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2416 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2417 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2418 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2419 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2420 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2421 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2422 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2423 };
2424
2425 // Attribute name (anything but space \n \r \t / < > = ? ! \0)
2426 template<int Dummy>
2427 const unsigned char lookup_tables<Dummy>::lookup_attribute_name[256] =
2428 {
2429 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2430 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
2431 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2432 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 2
2433 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, // 3
2434 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2435 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2436 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2437 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2438 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2439 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2440 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2441 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2442 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2443 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2444 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2445 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2446 };
2447
2448 // Attribute data with single quote (anything but ' \0)
2449 template<int Dummy>
2450 const unsigned char lookup_tables<Dummy>::lookup_attribute_data_1[256] =
2451 {
2452 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2453 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2454 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2455 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2456 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
2457 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2458 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2459 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2460 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2461 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2462 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2463 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2464 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2465 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2466 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2467 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2468 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2469 };
2470
2471 // Attribute data with single quote that does not require processing (anything but ' \0 &)
2472 template<int Dummy>
2473 const unsigned char lookup_tables<Dummy>::lookup_attribute_data_1_pure[256] =
2474 {
2475 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2476 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2477 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2478 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2479 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
2480 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2481 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2482 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2484 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2485 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2486 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2487 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2488 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2489 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2490 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2491 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2492 };
2493
2494 // Attribute data with double quote (anything but " \0)
2495 template<int Dummy>
2496 const unsigned char lookup_tables<Dummy>::lookup_attribute_data_2[256] =
2497 {
2498 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2499 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2500 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2501 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2502 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
2503 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2504 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2505 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2506 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2507 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2508 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2509 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2510 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2511 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2512 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2513 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2514 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2515 };
2516
2517 // Attribute data with double quote that does not require processing (anything but " \0 &)
2518 template<int Dummy>
2519 const unsigned char lookup_tables<Dummy>::lookup_attribute_data_2_pure[256] =
2520 {
2521 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2522 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
2523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
2524 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
2525 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
2526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
2527 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
2528 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
2529 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
2530 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
2531 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
2532 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
2533 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
2534 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
2535 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
2536 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
2537 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
2538 };
2539
2540 // Digits (dec and hex, 255 denotes end of numeric character reference)
2541 template<int Dummy>
2542 const unsigned char lookup_tables<Dummy>::lookup_digits[256] =
2543 {
2544 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
2545 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 0
2546 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 1
2547 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 2
2548 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,255,255,255,255,255,255, // 3
2549 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 4
2550 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 5
2551 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 6
2552 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 7
2553 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 8
2554 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 9
2555 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // A
2556 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // B
2557 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // C
2558 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // D
2559 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // E
2560 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 // F
2561 };
2562
2563 // Upper case conversion
2564 template<int Dummy>
2565 const unsigned char lookup_tables<Dummy>::lookup_upcase[256] =
2566 {
2567 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A B C D E F
2568 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0
2569 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 1
2570 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, // 2
2571 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, // 3
2572 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 4
2573 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, // 5
2574 96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 6
2575 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 123,124,125,126,127, // 7
2576 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, // 8
2577 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, // 9
2578 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, // A
2579 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, // B
2580 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, // C
2581 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, // D
2582 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, // E
2583 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 // F
2584 };
2585 }
2586 //! \endcond
2587
2588}
2589
2590// Undefine internal macros
2591#undef RAPIDXML_PARSE_ERROR
2592
2593// On MSVC, restore warnings state
2594#ifdef _MSC_VER
2595 #pragma warning(pop)
2596#endif
2597
2598#endif