Matt Mower | 64ffe73 | 2016-02-10 00:19:34 -0600 | [diff] [blame] | 1 | #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 | |
| 32 | namespace 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 | |
| 59 | namespace 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 | |
| 136 | namespace 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 (' & " < > &#...;) |
| 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 | // & ' |
| 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 | // " |
| 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 | // > |
| 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 | // < |
| 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 | return 0; |
| 1751 | } |
| 1752 | ++text; |
| 1753 | } |
| 1754 | text += 2; // Skip '?>' |
| 1755 | return 0; |
| 1756 | } |
| 1757 | |
| 1758 | // Create declaration |
| 1759 | xml_node<Ch> *declaration = this->allocate_node(node_declaration); |
| 1760 | |
| 1761 | // Skip whitespace before attributes or ?> |
| 1762 | skip<whitespace_pred, Flags>(text); |
| 1763 | |
| 1764 | // Parse declaration attributes |
| 1765 | parse_node_attributes<Flags>(text, declaration); |
| 1766 | |
| 1767 | // Skip ?> |
| 1768 | if (text[0] != Ch('?') || text[1] != Ch('>')) |
| 1769 | RAPIDXML_PARSE_ERROR("expected ?>", text); |
| 1770 | text += 2; |
| 1771 | |
| 1772 | return declaration; |
| 1773 | } |
| 1774 | |
| 1775 | // Parse XML comment (<!--...) |
| 1776 | template<int Flags> |
| 1777 | xml_node<Ch> *parse_comment(Ch *&text) |
| 1778 | { |
| 1779 | // If parsing of comments is disabled |
| 1780 | if (!(Flags & parse_comment_nodes)) |
| 1781 | { |
| 1782 | // Skip until end of comment |
| 1783 | while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>')) |
| 1784 | { |
| 1785 | if (!text[0]) { |
| 1786 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 1787 | return 0; |
| 1788 | } |
| 1789 | ++text; |
| 1790 | } |
| 1791 | text += 3; // Skip '-->' |
| 1792 | return 0; // Do not produce comment node |
| 1793 | } |
| 1794 | |
| 1795 | // Remember value start |
| 1796 | Ch *value = text; |
| 1797 | |
| 1798 | // Skip until end of comment |
| 1799 | while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>')) |
| 1800 | { |
| 1801 | if (!text[0]) { |
| 1802 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 1803 | return 0; |
| 1804 | } |
| 1805 | ++text; |
| 1806 | } |
| 1807 | |
| 1808 | // Create comment node |
| 1809 | xml_node<Ch> *comment = this->allocate_node(node_comment); |
| 1810 | comment->value(value, text - value); |
| 1811 | |
| 1812 | // Place zero terminator after comment value |
| 1813 | if (!(Flags & parse_no_string_terminators)) |
| 1814 | *text = Ch('\0'); |
| 1815 | |
| 1816 | text += 3; // Skip '-->' |
| 1817 | return comment; |
| 1818 | } |
| 1819 | |
| 1820 | // Parse DOCTYPE |
| 1821 | template<int Flags> |
| 1822 | xml_node<Ch> *parse_doctype(Ch *&text) |
| 1823 | { |
| 1824 | // Remember value start |
| 1825 | Ch *value = text; |
| 1826 | |
| 1827 | // Skip to > |
| 1828 | while (*text != Ch('>')) |
| 1829 | { |
| 1830 | // Determine character type |
| 1831 | switch (*text) |
| 1832 | { |
| 1833 | |
| 1834 | // If '[' encountered, scan for matching ending ']' using naive algorithm with depth |
| 1835 | // This works for all W3C test files except for 2 most wicked |
| 1836 | case Ch('['): |
| 1837 | { |
| 1838 | ++text; // Skip '[' |
| 1839 | int depth = 1; |
| 1840 | while (depth > 0) |
| 1841 | { |
| 1842 | switch (*text) |
| 1843 | { |
| 1844 | case Ch('['): ++depth; break; |
| 1845 | case Ch(']'): --depth; break; |
| 1846 | case 0: RAPIDXML_PARSE_ERROR("unexpected end of data", text); return 0; |
| 1847 | } |
| 1848 | ++text; |
| 1849 | } |
| 1850 | break; |
| 1851 | } |
| 1852 | |
| 1853 | // Error on end of text |
| 1854 | case Ch('\0'): |
| 1855 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 1856 | return 0; |
| 1857 | |
| 1858 | // Other character, skip it |
| 1859 | default: |
| 1860 | ++text; |
| 1861 | |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | // If DOCTYPE nodes enabled |
| 1866 | if (Flags & parse_doctype_node) |
| 1867 | { |
| 1868 | // Create a new doctype node |
| 1869 | xml_node<Ch> *doctype = this->allocate_node(node_doctype); |
| 1870 | doctype->value(value, text - value); |
| 1871 | |
| 1872 | // Place zero terminator after value |
| 1873 | if (!(Flags & parse_no_string_terminators)) |
| 1874 | *text = Ch('\0'); |
| 1875 | |
| 1876 | text += 1; // skip '>' |
| 1877 | return doctype; |
| 1878 | } |
| 1879 | else |
| 1880 | { |
| 1881 | text += 1; // skip '>' |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
| 1885 | } |
| 1886 | |
| 1887 | // Parse PI |
| 1888 | template<int Flags> |
| 1889 | xml_node<Ch> *parse_pi(Ch *&text) |
| 1890 | { |
| 1891 | // If creation of PI nodes is enabled |
| 1892 | if (Flags & parse_pi_nodes) |
| 1893 | { |
| 1894 | // Create pi node |
| 1895 | xml_node<Ch> *pi = this->allocate_node(node_pi); |
| 1896 | |
| 1897 | // Extract PI target name |
| 1898 | Ch *name = text; |
| 1899 | skip<node_name_pred, Flags>(text); |
| 1900 | if (text == name) |
| 1901 | RAPIDXML_PARSE_ERROR("expected PI target", text); |
| 1902 | pi->name(name, text - name); |
| 1903 | |
| 1904 | // Skip whitespace between pi target and pi |
| 1905 | skip<whitespace_pred, Flags>(text); |
| 1906 | |
| 1907 | // Remember start of pi |
| 1908 | Ch *value = text; |
| 1909 | |
| 1910 | // Skip to '?>' |
| 1911 | while (text[0] != Ch('?') || text[1] != Ch('>')) |
| 1912 | { |
| 1913 | if (*text == Ch('\0')) { |
| 1914 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 1915 | return 0; |
| 1916 | } |
| 1917 | ++text; |
| 1918 | } |
| 1919 | |
| 1920 | // Set pi value (verbatim, no entity expansion or whitespace normalization) |
| 1921 | pi->value(value, text - value); |
| 1922 | |
| 1923 | // Place zero terminator after name and value |
| 1924 | if (!(Flags & parse_no_string_terminators)) |
| 1925 | { |
| 1926 | pi->name()[pi->name_size()] = Ch('\0'); |
| 1927 | pi->value()[pi->value_size()] = Ch('\0'); |
| 1928 | } |
| 1929 | |
| 1930 | text += 2; // Skip '?>' |
| 1931 | return pi; |
| 1932 | } |
| 1933 | else |
| 1934 | { |
| 1935 | // Skip to '?>' |
| 1936 | while (text[0] != Ch('?') || text[1] != Ch('>')) |
| 1937 | { |
| 1938 | if (*text == Ch('\0')) { |
| 1939 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 1940 | return 0; |
| 1941 | } |
| 1942 | ++text; |
| 1943 | } |
| 1944 | text += 2; // Skip '?>' |
| 1945 | return 0; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | // Parse and append data |
| 1950 | // Return character that ends data. |
| 1951 | // This is necessary because this character might have been overwritten by a terminating 0 |
| 1952 | template<int Flags> |
| 1953 | Ch parse_and_append_data(xml_node<Ch> *node, Ch *&text, Ch *contents_start) |
| 1954 | { |
| 1955 | // Backup to contents start if whitespace trimming is disabled |
| 1956 | if (!(Flags & parse_trim_whitespace)) |
| 1957 | text = contents_start; |
| 1958 | |
| 1959 | // Skip until end of data |
| 1960 | Ch *value = text, *end; |
| 1961 | if (Flags & parse_normalize_whitespace) |
| 1962 | end = skip_and_expand_character_refs<text_pred, text_pure_with_ws_pred, Flags>(text); |
| 1963 | else |
| 1964 | end = skip_and_expand_character_refs<text_pred, text_pure_no_ws_pred, Flags>(text); |
| 1965 | |
| 1966 | // Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after > |
| 1967 | if (Flags & parse_trim_whitespace) |
| 1968 | { |
| 1969 | if (Flags & parse_normalize_whitespace) |
| 1970 | { |
| 1971 | // Whitespace is already condensed to single space characters by skipping function, so just trim 1 char off the end |
| 1972 | if (*(end - 1) == Ch(' ')) |
| 1973 | --end; |
| 1974 | } |
| 1975 | else |
| 1976 | { |
| 1977 | // Backup until non-whitespace character is found |
| 1978 | while (whitespace_pred::test(*(end - 1))) |
| 1979 | --end; |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | // If characters are still left between end and value (this test is only necessary if normalization is enabled) |
| 1984 | // Create new data node |
| 1985 | if (!(Flags & parse_no_data_nodes)) |
| 1986 | { |
| 1987 | xml_node<Ch> *data = this->allocate_node(node_data); |
| 1988 | data->value(value, end - value); |
| 1989 | node->append_node(data); |
| 1990 | } |
| 1991 | |
| 1992 | // Add data to parent node if no data exists yet |
| 1993 | if (!(Flags & parse_no_element_values)) |
| 1994 | if (*node->value() == Ch('\0')) |
| 1995 | node->value(value, end - value); |
| 1996 | |
| 1997 | // Place zero terminator after value |
| 1998 | if (!(Flags & parse_no_string_terminators)) |
| 1999 | { |
| 2000 | Ch ch = *text; |
| 2001 | *end = Ch('\0'); |
| 2002 | return ch; // Return character that ends data; this is required because zero terminator overwritten it |
| 2003 | } |
| 2004 | |
| 2005 | // Return character that ends data |
| 2006 | return *text; |
| 2007 | } |
| 2008 | |
| 2009 | // Parse CDATA |
| 2010 | template<int Flags> |
| 2011 | xml_node<Ch> *parse_cdata(Ch *&text) |
| 2012 | { |
| 2013 | // If CDATA is disabled |
| 2014 | if (Flags & parse_no_data_nodes) |
| 2015 | { |
| 2016 | // Skip until end of cdata |
| 2017 | while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>')) |
| 2018 | { |
| 2019 | if (!text[0]) { |
| 2020 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 2021 | return 0; |
| 2022 | } |
| 2023 | ++text; |
| 2024 | } |
| 2025 | text += 3; // Skip ]]> |
| 2026 | return 0; // Do not produce CDATA node |
| 2027 | } |
| 2028 | |
| 2029 | // Skip until end of cdata |
| 2030 | Ch *value = text; |
| 2031 | while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>')) |
| 2032 | { |
| 2033 | if (!text[0]) { |
| 2034 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 2035 | return 0; |
| 2036 | } |
| 2037 | ++text; |
| 2038 | } |
| 2039 | |
| 2040 | // Create new cdata node |
| 2041 | xml_node<Ch> *cdata = this->allocate_node(node_cdata); |
| 2042 | cdata->value(value, text - value); |
| 2043 | |
| 2044 | // Place zero terminator after value |
| 2045 | if (!(Flags & parse_no_string_terminators)) |
| 2046 | *text = Ch('\0'); |
| 2047 | |
| 2048 | text += 3; // Skip ]]> |
| 2049 | return cdata; |
| 2050 | } |
| 2051 | |
| 2052 | // Parse element node |
| 2053 | template<int Flags> |
| 2054 | xml_node<Ch> *parse_element(Ch *&text) |
| 2055 | { |
| 2056 | // Create element node |
| 2057 | xml_node<Ch> *element = this->allocate_node(node_element); |
| 2058 | |
| 2059 | // Extract element name |
| 2060 | Ch *name = text; |
| 2061 | skip<node_name_pred, Flags>(text); |
| 2062 | if (text == name) |
| 2063 | RAPIDXML_PARSE_ERROR("expected element name", text); |
| 2064 | element->name(name, text - name); |
| 2065 | |
| 2066 | // Skip whitespace between element name and attributes or > |
| 2067 | skip<whitespace_pred, Flags>(text); |
| 2068 | |
| 2069 | // Parse attributes, if any |
| 2070 | parse_node_attributes<Flags>(text, element); |
| 2071 | |
| 2072 | // Determine ending type |
| 2073 | if (*text == Ch('>')) |
| 2074 | { |
| 2075 | ++text; |
| 2076 | parse_node_contents<Flags>(text, element); |
| 2077 | } |
| 2078 | else if (*text == Ch('/')) |
| 2079 | { |
| 2080 | ++text; |
| 2081 | if (*text != Ch('>')) |
| 2082 | RAPIDXML_PARSE_ERROR("expected >", text); |
| 2083 | ++text; |
| 2084 | } |
| 2085 | else |
| 2086 | RAPIDXML_PARSE_ERROR("expected >", text); |
| 2087 | |
| 2088 | // Place zero terminator after name |
| 2089 | if (!(Flags & parse_no_string_terminators)) |
| 2090 | element->name()[element->name_size()] = Ch('\0'); |
| 2091 | |
| 2092 | // Return parsed element |
| 2093 | return element; |
| 2094 | } |
| 2095 | |
| 2096 | // Determine node type, and parse it |
| 2097 | template<int Flags> |
| 2098 | xml_node<Ch> *parse_node(Ch *&text) |
| 2099 | { |
| 2100 | // Parse proper node type |
| 2101 | switch (text[0]) |
| 2102 | { |
| 2103 | |
| 2104 | // <... |
| 2105 | default: |
| 2106 | // Parse and append element node |
| 2107 | return parse_element<Flags>(text); |
| 2108 | |
| 2109 | // <?... |
| 2110 | case Ch('?'): |
| 2111 | ++text; // Skip ? |
| 2112 | if ((text[0] == Ch('x') || text[0] == Ch('X')) && |
| 2113 | (text[1] == Ch('m') || text[1] == Ch('M')) && |
| 2114 | (text[2] == Ch('l') || text[2] == Ch('L')) && |
| 2115 | whitespace_pred::test(text[3])) |
| 2116 | { |
| 2117 | // '<?xml ' - xml declaration |
| 2118 | text += 4; // Skip 'xml ' |
| 2119 | return parse_xml_declaration<Flags>(text); |
| 2120 | } |
| 2121 | else |
| 2122 | { |
| 2123 | // Parse PI |
| 2124 | return parse_pi<Flags>(text); |
| 2125 | } |
| 2126 | |
| 2127 | // <!... |
| 2128 | case Ch('!'): |
| 2129 | |
| 2130 | // Parse proper subset of <! node |
| 2131 | switch (text[1]) |
| 2132 | { |
| 2133 | |
| 2134 | // <!- |
| 2135 | case Ch('-'): |
| 2136 | if (text[2] == Ch('-')) |
| 2137 | { |
| 2138 | // '<!--' - xml comment |
| 2139 | text += 3; // Skip '!--' |
| 2140 | return parse_comment<Flags>(text); |
| 2141 | } |
| 2142 | break; |
| 2143 | |
| 2144 | // <![ |
| 2145 | case Ch('['): |
| 2146 | if (text[2] == Ch('C') && text[3] == Ch('D') && text[4] == Ch('A') && |
| 2147 | text[5] == Ch('T') && text[6] == Ch('A') && text[7] == Ch('[')) |
| 2148 | { |
| 2149 | // '<![CDATA[' - cdata |
| 2150 | text += 8; // Skip '![CDATA[' |
| 2151 | return parse_cdata<Flags>(text); |
| 2152 | } |
| 2153 | break; |
| 2154 | |
| 2155 | // <!D |
| 2156 | case Ch('D'): |
| 2157 | if (text[2] == Ch('O') && text[3] == Ch('C') && text[4] == Ch('T') && |
| 2158 | text[5] == Ch('Y') && text[6] == Ch('P') && text[7] == Ch('E') && |
| 2159 | whitespace_pred::test(text[8])) |
| 2160 | { |
| 2161 | // '<!DOCTYPE ' - doctype |
| 2162 | text += 9; // skip '!DOCTYPE ' |
| 2163 | return parse_doctype<Flags>(text); |
| 2164 | } |
| 2165 | |
| 2166 | } // switch |
| 2167 | |
| 2168 | // Attempt to skip other, unrecognized node types starting with <! |
| 2169 | ++text; // Skip ! |
| 2170 | while (*text != Ch('>')) |
| 2171 | { |
| 2172 | if (*text == 0) { |
| 2173 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 2174 | return 0; |
| 2175 | } |
| 2176 | ++text; |
| 2177 | } |
| 2178 | ++text; // Skip '>' |
| 2179 | return 0; // No node recognized |
| 2180 | |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | // Parse contents of the node - children, data etc. |
| 2185 | template<int Flags> |
| 2186 | void parse_node_contents(Ch *&text, xml_node<Ch> *node) |
| 2187 | { |
| 2188 | // For all children and text |
| 2189 | while (1) |
| 2190 | { |
| 2191 | // Skip whitespace between > and node contents |
| 2192 | Ch *contents_start = text; // Store start of node contents before whitespace is skipped |
| 2193 | skip<whitespace_pred, Flags>(text); |
| 2194 | Ch next_char = *text; |
| 2195 | |
| 2196 | // After data nodes, instead of continuing the loop, control jumps here. |
| 2197 | // This is because zero termination inside parse_and_append_data() function |
| 2198 | // would wreak havoc with the above code. |
| 2199 | // Also, skipping whitespace after data nodes is unnecessary. |
| 2200 | after_data_node: |
| 2201 | |
| 2202 | // Determine what comes next: node closing, child node, data node, or 0? |
| 2203 | switch (next_char) |
| 2204 | { |
| 2205 | |
| 2206 | // Node closing or child node |
| 2207 | case Ch('<'): |
| 2208 | if (text[1] == Ch('/')) |
| 2209 | { |
| 2210 | // Node closing |
| 2211 | text += 2; // Skip '</' |
| 2212 | if (Flags & parse_validate_closing_tags) |
| 2213 | { |
| 2214 | // Skip and validate closing tag name |
| 2215 | Ch *closing_name = text; |
| 2216 | skip<node_name_pred, Flags>(text); |
| 2217 | if (!internal::compare(node->name(), node->name_size(), closing_name, text - closing_name, true)) |
| 2218 | RAPIDXML_PARSE_ERROR("invalid closing tag name", text); |
| 2219 | } |
| 2220 | else |
| 2221 | { |
| 2222 | // No validation, just skip name |
| 2223 | skip<node_name_pred, Flags>(text); |
| 2224 | } |
| 2225 | // Skip remaining whitespace after node name |
| 2226 | skip<whitespace_pred, Flags>(text); |
| 2227 | if (*text != Ch('>')) |
| 2228 | RAPIDXML_PARSE_ERROR("expected >", text); |
| 2229 | ++text; // Skip '>' |
| 2230 | return; // Node closed, finished parsing contents |
| 2231 | } |
| 2232 | else |
| 2233 | { |
| 2234 | // Child node |
| 2235 | ++text; // Skip '<' |
| 2236 | if (xml_node<Ch> *child = parse_node<Flags>(text)) |
| 2237 | node->append_node(child); |
| 2238 | } |
| 2239 | break; |
| 2240 | |
| 2241 | // End of data - error |
| 2242 | case Ch('\0'): |
| 2243 | RAPIDXML_PARSE_ERROR("unexpected end of data", text); |
| 2244 | return; |
| 2245 | |
| 2246 | // Data node |
| 2247 | default: |
| 2248 | next_char = parse_and_append_data<Flags>(node, text, contents_start); |
| 2249 | goto after_data_node; // Bypass regular processing after data nodes |
| 2250 | |
| 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | // Parse XML attributes of the node |
| 2256 | template<int Flags> |
| 2257 | void parse_node_attributes(Ch *&text, xml_node<Ch> *node) |
| 2258 | { |
| 2259 | // For all attributes |
| 2260 | while (attribute_name_pred::test(*text)) |
| 2261 | { |
| 2262 | // Extract attribute name |
| 2263 | Ch *name = text; |
| 2264 | ++text; // Skip first character of attribute name |
| 2265 | skip<attribute_name_pred, Flags>(text); |
| 2266 | if (text == name) |
| 2267 | RAPIDXML_PARSE_ERROR("expected attribute name", name); |
| 2268 | |
| 2269 | // Create new attribute |
| 2270 | xml_attribute<Ch> *attribute = this->allocate_attribute(); |
| 2271 | attribute->name(name, text - name); |
| 2272 | node->append_attribute(attribute); |
| 2273 | |
| 2274 | // Skip whitespace after attribute name |
| 2275 | skip<whitespace_pred, Flags>(text); |
| 2276 | |
| 2277 | // Skip = |
| 2278 | if (*text != Ch('=')) |
| 2279 | RAPIDXML_PARSE_ERROR("expected =", text); |
| 2280 | ++text; |
| 2281 | |
| 2282 | // Add terminating zero after name |
| 2283 | if (!(Flags & parse_no_string_terminators)) |
| 2284 | attribute->name()[attribute->name_size()] = 0; |
| 2285 | |
| 2286 | // Skip whitespace after = |
| 2287 | skip<whitespace_pred, Flags>(text); |
| 2288 | |
| 2289 | // Skip quote and remember if it was ' or " |
| 2290 | Ch quote = *text; |
| 2291 | if (quote != Ch('\'') && quote != Ch('"')) |
| 2292 | RAPIDXML_PARSE_ERROR("expected ' or \"", text); |
| 2293 | ++text; |
| 2294 | |
| 2295 | // Extract attribute value and expand char refs in it |
| 2296 | Ch *value = text, *end; |
| 2297 | const int AttFlags = Flags & ~parse_normalize_whitespace; // No whitespace normalization in attributes |
| 2298 | if (quote == Ch('\'')) |
| 2299 | end = skip_and_expand_character_refs<attribute_value_pred<Ch('\'')>, attribute_value_pure_pred<Ch('\'')>, AttFlags>(text); |
| 2300 | else |
| 2301 | end = skip_and_expand_character_refs<attribute_value_pred<Ch('"')>, attribute_value_pure_pred<Ch('"')>, AttFlags>(text); |
| 2302 | |
| 2303 | // Set attribute value |
| 2304 | attribute->value(value, end - value); |
| 2305 | |
| 2306 | // Make sure that end quote is present |
| 2307 | if (*text != quote) |
| 2308 | RAPIDXML_PARSE_ERROR("expected ' or \"", text); |
| 2309 | ++text; // Skip quote |
| 2310 | |
| 2311 | // Add terminating zero after value |
| 2312 | if (!(Flags & parse_no_string_terminators)) |
| 2313 | attribute->value()[attribute->value_size()] = 0; |
| 2314 | |
| 2315 | // Skip whitespace after attribute value |
| 2316 | skip<whitespace_pred, Flags>(text); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | }; |
| 2321 | |
| 2322 | //! \cond internal |
| 2323 | namespace internal |
| 2324 | { |
| 2325 | |
| 2326 | // Whitespace (space \n \r \t) |
| 2327 | template<int Dummy> |
| 2328 | const unsigned char lookup_tables<Dummy>::lookup_whitespace[256] = |
| 2329 | { |
| 2330 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2331 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, // 0 |
| 2332 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1 |
| 2333 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2 |
| 2334 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3 |
| 2335 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4 |
| 2336 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5 |
| 2337 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6 |
| 2338 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7 |
| 2339 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8 |
| 2340 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9 |
| 2341 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A |
| 2342 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B |
| 2343 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // C |
| 2344 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D |
| 2345 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // E |
| 2346 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F |
| 2347 | }; |
| 2348 | |
| 2349 | // Node name (anything but space \n \r \t / > ? \0) |
| 2350 | template<int Dummy> |
| 2351 | const unsigned char lookup_tables<Dummy>::lookup_node_name[256] = |
| 2352 | { |
| 2353 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2354 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0 |
| 2355 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2356 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 2 |
| 2357 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, // 3 |
| 2358 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2359 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2360 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2361 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2362 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2363 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2364 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2365 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2366 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2367 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2368 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2369 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2370 | }; |
| 2371 | |
| 2372 | // Text (i.e. PCDATA) (anything but < \0) |
| 2373 | template<int Dummy> |
| 2374 | const unsigned char lookup_tables<Dummy>::lookup_text[256] = |
| 2375 | { |
| 2376 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2377 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 |
| 2378 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2379 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2 |
| 2380 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3 |
| 2381 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2382 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2383 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2384 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2385 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2386 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2387 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2388 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2389 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2390 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2391 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2392 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2393 | }; |
| 2394 | |
| 2395 | // Text (i.e. PCDATA) that does not require processing when ws normalization is disabled |
| 2396 | // (anything but < \0 &) |
| 2397 | template<int Dummy> |
| 2398 | const unsigned char lookup_tables<Dummy>::lookup_text_pure_no_ws[256] = |
| 2399 | { |
| 2400 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2401 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 |
| 2402 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2403 | 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2 |
| 2404 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3 |
| 2405 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2406 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2407 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2408 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2409 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2410 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2411 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2412 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2413 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2414 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2415 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2416 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2417 | }; |
| 2418 | |
| 2419 | // Text (i.e. PCDATA) that does not require processing when ws normalizationis is enabled |
| 2420 | // (anything but < \0 & space \n \r \t) |
| 2421 | template<int Dummy> |
| 2422 | const unsigned char lookup_tables<Dummy>::lookup_text_pure_with_ws[256] = |
| 2423 | { |
| 2424 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2425 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0 |
| 2426 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2427 | 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2 |
| 2428 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3 |
| 2429 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2430 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2431 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2432 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2433 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2434 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2435 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2436 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2437 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2438 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2439 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2440 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2441 | }; |
| 2442 | |
| 2443 | // Attribute name (anything but space \n \r \t / < > = ? ! \0) |
| 2444 | template<int Dummy> |
| 2445 | const unsigned char lookup_tables<Dummy>::lookup_attribute_name[256] = |
| 2446 | { |
| 2447 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2448 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0 |
| 2449 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2450 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 2 |
| 2451 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, // 3 |
| 2452 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2453 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2454 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2455 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2456 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2457 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2458 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2459 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2460 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2461 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2462 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2463 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2464 | }; |
| 2465 | |
| 2466 | // Attribute data with single quote (anything but ' \0) |
| 2467 | template<int Dummy> |
| 2468 | const unsigned char lookup_tables<Dummy>::lookup_attribute_data_1[256] = |
| 2469 | { |
| 2470 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2471 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 |
| 2472 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2473 | 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 2 |
| 2474 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3 |
| 2475 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2476 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2477 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2478 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2479 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2480 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2481 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2482 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2483 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2484 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2485 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2486 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2487 | }; |
| 2488 | |
| 2489 | // Attribute data with single quote that does not require processing (anything but ' \0 &) |
| 2490 | template<int Dummy> |
| 2491 | const unsigned char lookup_tables<Dummy>::lookup_attribute_data_1_pure[256] = |
| 2492 | { |
| 2493 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2494 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 |
| 2495 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2496 | 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 2 |
| 2497 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3 |
| 2498 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2499 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2500 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2501 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2502 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2503 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2504 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2505 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2506 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2507 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2508 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2509 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2510 | }; |
| 2511 | |
| 2512 | // Attribute data with double quote (anything but " \0) |
| 2513 | template<int Dummy> |
| 2514 | const unsigned char lookup_tables<Dummy>::lookup_attribute_data_2[256] = |
| 2515 | { |
| 2516 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2517 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 |
| 2518 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2519 | 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2 |
| 2520 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3 |
| 2521 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2522 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2523 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2524 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2525 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2526 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2527 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2528 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2529 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2530 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2531 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2532 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2533 | }; |
| 2534 | |
| 2535 | // Attribute data with double quote that does not require processing (anything but " \0 &) |
| 2536 | template<int Dummy> |
| 2537 | const unsigned char lookup_tables<Dummy>::lookup_attribute_data_2_pure[256] = |
| 2538 | { |
| 2539 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2540 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 |
| 2541 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 |
| 2542 | 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2 |
| 2543 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3 |
| 2544 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 |
| 2545 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 |
| 2546 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 |
| 2547 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 |
| 2548 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 |
| 2549 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 |
| 2550 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A |
| 2551 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B |
| 2552 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C |
| 2553 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D |
| 2554 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E |
| 2555 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F |
| 2556 | }; |
| 2557 | |
| 2558 | // Digits (dec and hex, 255 denotes end of numeric character reference) |
| 2559 | template<int Dummy> |
| 2560 | const unsigned char lookup_tables<Dummy>::lookup_digits[256] = |
| 2561 | { |
| 2562 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 2563 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 0 |
| 2564 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 1 |
| 2565 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 2 |
| 2566 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,255,255,255,255,255,255, // 3 |
| 2567 | 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 4 |
| 2568 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 5 |
| 2569 | 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 6 |
| 2570 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 7 |
| 2571 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 8 |
| 2572 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 9 |
| 2573 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // A |
| 2574 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // B |
| 2575 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // C |
| 2576 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // D |
| 2577 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // E |
| 2578 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 // F |
| 2579 | }; |
| 2580 | |
| 2581 | // Upper case conversion |
| 2582 | template<int Dummy> |
| 2583 | const unsigned char lookup_tables<Dummy>::lookup_upcase[256] = |
| 2584 | { |
| 2585 | // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A B C D E F |
| 2586 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0 |
| 2587 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 1 |
| 2588 | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, // 2 |
| 2589 | 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, // 3 |
| 2590 | 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 4 |
| 2591 | 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, // 5 |
| 2592 | 96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 6 |
| 2593 | 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 123,124,125,126,127, // 7 |
| 2594 | 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, // 8 |
| 2595 | 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, // 9 |
| 2596 | 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, // A |
| 2597 | 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, // B |
| 2598 | 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, // C |
| 2599 | 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, // D |
| 2600 | 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, // E |
| 2601 | 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 // F |
| 2602 | }; |
| 2603 | } |
| 2604 | //! \endcond |
| 2605 | |
| 2606 | } |
| 2607 | |
| 2608 | // Undefine internal macros |
| 2609 | #undef RAPIDXML_PARSE_ERROR |
| 2610 | |
| 2611 | // On MSVC, restore warnings state |
| 2612 | #ifdef _MSC_VER |
| 2613 | #pragma warning(pop) |
| 2614 | #endif |
| 2615 | |
| 2616 | #endif |