rapidxml_utils.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef RAPIDXML_UTILS_HPP_INCLUDED
  2. #define RAPIDXML_UTILS_HPP_INCLUDED
  3. // Copyright (C) 2006, 2009 Marcin Kalicinski
  4. // Version 1.13
  5. // Revision $DateTime: 2009/05/15 23:02:39 $
  6. //! \file rapidxml_utils.hpp This file contains high-level rapidxml utilities that can be useful
  7. //! in certain simple scenarios. They should probably not be used if maximizing performance is the main objective.
  8. #include "rapidxml.hpp"
  9. #include <stdexcept>
  10. namespace rapidxml
  11. {
  12. //! Counts children of node. Time complexity is O(n).
  13. //! \return Number of children of node
  14. template<class Ch>
  15. inline std::size_t count_children(const xml_node<Ch> *node,
  16. const Ch* name = 0,
  17. std::size_t name_size = 0)
  18. {
  19. if (name && name_size == 0)
  20. name_size = internal::measure(name);
  21. xml_node<Ch> *child = node->first_node(name, name_size);
  22. std::size_t count = 0;
  23. while (child)
  24. {
  25. ++count;
  26. child = child->next_sibling(name, name_size);
  27. }
  28. return count;
  29. }
  30. //! Counts attributes of node. Time complexity is O(n).
  31. //! \return Number of attributes of node
  32. template<class Ch>
  33. inline std::size_t count_attributes(const xml_node<Ch> *node,
  34. const Ch* name = 0,
  35. std::size_t name_size = 0)
  36. {
  37. if (name && name_size == 0)
  38. name_size = internal::measure(name);
  39. xml_attribute<Ch> *attr = node->first_attribute(name, name_size);
  40. std::size_t count = 0;
  41. while (attr)
  42. {
  43. ++count;
  44. attr = attr->next_attribute(name, name_size);
  45. }
  46. return count;
  47. }
  48. }
  49. #endif