1 | #ifndef RAPIDXML_UTILS_HPP_INCLUDED |
---|
2 | #define RAPIDXML_UTILS_HPP_INCLUDED |
---|
3 | |
---|
4 | // Copyright (C) 2006, 2009 Marcin Kalicinski |
---|
5 | // Version 1.13 |
---|
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. |
---|
9 | |
---|
10 | #include "rapidxml.hpp" |
---|
11 | #include <vector> |
---|
12 | #include <string> |
---|
13 | #include <fstream> |
---|
14 | #include <stdexcept> |
---|
15 | |
---|
16 | namespace rapidxml |
---|
17 | { |
---|
18 | |
---|
19 | //! Represents data loaded from a file |
---|
20 | template<class Ch = char> |
---|
21 | class file |
---|
22 | { |
---|
23 | |
---|
24 | public: |
---|
25 | |
---|
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) |
---|
29 | { |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | // Open stream |
---|
33 | basic_ifstream<Ch> stream(filename, ios::binary); |
---|
34 | if (!stream) |
---|
35 | throw runtime_error(string("cannot open file ") + filename); |
---|
36 | stream.unsetf(ios::skipws); |
---|
37 | |
---|
38 | // Determine stream size |
---|
39 | stream.seekg(0, ios::end); |
---|
40 | size_t size = stream.tellg(); |
---|
41 | stream.seekg(0); |
---|
42 | |
---|
43 | // Load data and add terminating 0 |
---|
44 | m_data.resize(size + 1); |
---|
45 | stream.read(&m_data.front(), static_cast<streamsize>(size)); |
---|
46 | m_data[size] = 0; |
---|
47 | } |
---|
48 | |
---|
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) |
---|
52 | { |
---|
53 | using namespace std; |
---|
54 | |
---|
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"); |
---|
60 | m_data.push_back(0); |
---|
61 | } |
---|
62 | |
---|
63 | //! Gets file data. |
---|
64 | //! \return Pointer to data of file. |
---|
65 | Ch *data() |
---|
66 | { |
---|
67 | return &m_data.front(); |
---|
68 | } |
---|
69 | |
---|
70 | //! Gets file data. |
---|
71 | //! \return Pointer to data of file. |
---|
72 | const Ch *data() const |
---|
73 | { |
---|
74 | return &m_data.front(); |
---|
75 | } |
---|
76 | |
---|
77 | //! Gets file data size. |
---|
78 | //! \return Size of file data, in characters. |
---|
79 | std::size_t size() const |
---|
80 | { |
---|
81 | return m_data.size(); |
---|
82 | } |
---|
83 | |
---|
84 | private: |
---|
85 | |
---|
86 | std::vector<Ch> m_data; // File data |
---|
87 | |
---|
88 | }; |
---|
89 | |
---|
90 | //! Counts children of node. Time complexity is O(n). |
---|
91 | //! \return Number of children of node |
---|
92 | template<class Ch> |
---|
93 | inline std::size_t count_children(xml_node<Ch> *node) |
---|
94 | { |
---|
95 | xml_node<Ch> *child = node->first_node(); |
---|
96 | std::size_t count = 0; |
---|
97 | while (child) |
---|
98 | { |
---|
99 | ++count; |
---|
100 | child = child->next_sibling(); |
---|
101 | } |
---|
102 | return count; |
---|
103 | } |
---|
104 | |
---|
105 | //! Counts attributes of node. Time complexity is O(n). |
---|
106 | //! \return Number of attributes of node |
---|
107 | template<class Ch> |
---|
108 | inline std::size_t count_attributes(xml_node<Ch> *node) |
---|
109 | { |
---|
110 | xml_attribute<Ch> *attr = node->first_attribute(); |
---|
111 | std::size_t count = 0; |
---|
112 | while (attr) |
---|
113 | { |
---|
114 | ++count; |
---|
115 | attr = attr->next_attribute(); |
---|
116 | } |
---|
117 | return count; |
---|
118 | } |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | #endif |
---|