1 #ifndef RAPIDXML_UTILS_HPP_INCLUDED
2 #define RAPIDXML_UTILS_HPP_INCLUDED
4 // Copyright (C) 2006, 2009 Marcin Kalicinski
6 // Revision $DateTime: 2009/05/13 01:46:17 $
7 //! \file rapidxml_utils.hpp This file contains high-level rapidxml utilities that can be useful
8 //! in certain simple scenarios. They should probably not be used if maximizing performance is the main objective.
10 #include "rapidxml.hpp"
19 //! Represents data loaded from a file
20 template<class Ch = char>
26 //! Loads file into the memory. Data will be automatically destroyed by the destructor.
27 //! \param filename Filename to load.
28 file(const char *filename)
33 basic_ifstream<Ch> stream(filename, ios::binary);
35 throw runtime_error(string("cannot open file ") + filename);
36 stream.unsetf(ios::skipws);
38 // Determine stream size
39 stream.seekg(0, ios::end);
40 size_t size = stream.tellg();
43 // Load data and add terminating 0
44 m_data.resize(size + 1);
45 stream.read(&m_data.front(), static_cast<streamsize>(size));
49 //! Loads file into the memory. Data will be automatically destroyed by the destructor
50 //! \param stream Stream to load from
51 file(std::basic_istream<Ch> &stream)
55 // Load data and add terminating 0
56 stream.unsetf(ios::skipws);
57 m_data.assign(istreambuf_iterator<Ch>(stream), istreambuf_iterator<Ch>());
58 if (stream.fail() || stream.bad())
59 throw runtime_error("error reading stream");
64 //! \return Pointer to data of file.
67 return &m_data.front();
71 //! \return Pointer to data of file.
72 const Ch *data() const
74 return &m_data.front();
77 //! Gets file data size.
78 //! \return Size of file data, in characters.
79 std::size_t size() const
86 std::vector<Ch> m_data; // File data
90 //! Counts children of node. Time complexity is O(n).
91 //! \return Number of children of node
93 inline std::size_t count_children(xml_node<Ch> *node)
95 xml_node<Ch> *child = node->first_node();
96 std::size_t count = 0;
100 child = child->next_sibling();
105 //! Counts attributes of node. Time complexity is O(n).
106 //! \return Number of attributes of node
108 inline std::size_t count_attributes(xml_node<Ch> *node)
110 xml_attribute<Ch> *attr = node->first_attribute();
111 std::size_t count = 0;
115 attr = attr->next_attribute();