rapidxml_utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // Copyright (C) 2006, 2009 Marcin Kalicinski
4 // Version 1.13
5 // Revision $DateTime: 2009/05/13 01:46:17 $
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 
9 #include "rapidxml.hpp"
10 #include <vector>
11 #include <string>
12 #include <fstream>
13 #include <stdexcept>
14 
15 namespace rapidxml
16 {
17 
18  //! Represents data loaded from a file
19  template<class Ch = char>
20  class file
21  {
22 
23  public:
24 
25  //! Loads file into the memory. Data will be automatically destroyed by the destructor.
26  //! \param filename Filename to load.
27  file(const char* filename)
28  {
29  using namespace std;
30 
31  // Open stream
32  basic_ifstream<Ch> stream(filename, ios::binary);
33 
34  if (!stream)
35  {
36  throw runtime_error(string("cannot open file ") + filename);
37  }
38 
39  stream.unsetf(ios::skipws);
40 
41  // Determine stream size
42  stream.seekg(0, ios::end);
43  size_t size = stream.tellg();
44  stream.seekg(0);
45 
46  // Load data and add terminating 0
47  m_data.resize(size + 1);
48  stream.read(&m_data.front(), static_cast<streamsize>(size));
49  m_data[size] = 0;
50  }
51 
52  //! Loads file into the memory. Data will be automatically destroyed by the destructor
53  //! \param stream Stream to load from
54  file(std::basic_istream<Ch>& stream)
55  {
56  using namespace std;
57 
58  // Load data and add terminating 0
59  stream.unsetf(ios::skipws);
60  m_data.assign(istreambuf_iterator<Ch>(stream), istreambuf_iterator<Ch>());
61 
62  if (stream.fail() || stream.bad())
63  {
64  throw runtime_error("error reading stream");
65  }
66 
67  m_data.push_back(0);
68  }
69 
70  //! Gets file data.
71  //! \return Pointer to data of file.
72  Ch* data()
73  {
74  return &m_data.front();
75  }
76 
77  //! Gets file data.
78  //! \return Pointer to data of file.
79  const Ch* data() const
80  {
81  return &m_data.front();
82  }
83 
84  //! Gets file data size.
85  //! \return Size of file data, in characters.
86  std::size_t size() const
87  {
88  return m_data.size();
89  }
90 
91  private:
92 
93  std::vector<Ch> m_data; // File data
94 
95  };
96 
97  //! Counts children of node. Time complexity is O(n).
98  //! \return Number of children of node
99  template<class Ch>
100  inline std::size_t count_children(xml_node<Ch>* node)
101  {
102  xml_node<Ch>* child = node->first_node();
103  std::size_t count = 0;
104 
105  while (child)
106  {
107  ++count;
108  child = child->next_sibling();
109  }
110 
111  return count;
112  }
113 
114  //! Counts attributes of node. Time complexity is O(n).
115  //! \return Number of attributes of node
116  template<class Ch>
117  inline std::size_t count_attributes(xml_node<Ch>* node)
118  {
119  xml_attribute<Ch>* attr = node->first_attribute();
120  std::size_t count = 0;
121 
122  while (attr)
123  {
124  ++count;
125  attr = attr->next_attribute();
126  }
127 
128  return count;
129  }
130 
131 }
132 
rapidxml::xml_node::next_sibling
xml_node< Ch > * next_sibling(const Ch *name=nullptr, std::size_t name_size=0, bool case_sensitive=true) const
Gets next sibling node, optionally matching node name.
Definition: rapidxml.hpp:1121
rapidxml::xml_attribute::next_attribute
xml_attribute< Ch > * next_attribute(const Ch *name=nullptr, std::size_t name_size=0, bool case_sensitive=true) const
Gets next attribute, optionally matching attribute name.
Definition: rapidxml.hpp:935
rapidxml::count_children
std::size_t count_children(xml_node< Ch > *node)
Counts children of node.
Definition: rapidxml_utils.hpp:100
rapidxml::xml_node::first_attribute
xml_attribute< Ch > * first_attribute(const Ch *name=nullptr, std::size_t name_size=0, bool case_sensitive=true) const
Gets first attribute of node, optionally matching attribute name.
Definition: rapidxml.hpp:1151
rapidxml::file::data
Ch * data()
Gets file data.
Definition: rapidxml_utils.hpp:72
rapidxml::xml_attribute
Class representing attribute node of XML document.
Definition: rapidxml.hpp:139
rapidxml::file
Represents data loaded from a file.
Definition: rapidxml_utils.hpp:20
rapidxml
Definition: rapidxml.hpp:58
rapidxml::file::file
file(std::basic_istream< Ch > &stream)
Loads file into the memory.
Definition: rapidxml_utils.hpp:54
filename
std::string filename
Definition: VisualizationRobot.cpp:84
rapidxml::xml_node::first_node
xml_node< Ch > * first_node(const Ch *name=nullptr, std::size_t name_size=0, bool case_sensitive=true) const
Gets first child node, optionally matching node name.
Definition: rapidxml.hpp:1027
rapidxml::xml_node
Class representing a node of XML document.
Definition: rapidxml.hpp:138
std
Definition: Application.h:66
rapidxml::file::file
file(const char *filename)
Loads file into the memory.
Definition: rapidxml_utils.hpp:27
rapidxml.hpp
rapidxml::count_attributes
std::size_t count_attributes(xml_node< Ch > *node)
Counts attributes of node.
Definition: rapidxml_utils.hpp:117
rapidxml::file::size
std::size_t size() const
Gets file data size.
Definition: rapidxml_utils.hpp:86
rapidxml::file::data
const Ch * data() const
Gets file data.
Definition: rapidxml_utils.hpp:79