PageRenderTime 1401ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Dependencies/Boost/boost/property_tree/detail/rapidxml.hpp

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