/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

Large files are truncated click here to view the full file

  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…