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 <fstream>
10 #include <stdexcept>
11 #include <string>
12 #include <vector>
13 
14 #include "rapidxml.hpp"
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  //! 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*
73  data()
74  {
75  return &m_data.front();
76  }
77 
78  //! Gets file data.
79  //! \return Pointer to data of file.
80  const Ch*
81  data() const
82  {
83  return &m_data.front();
84  }
85 
86  //! Gets file data size.
87  //! \return Size of file data, in characters.
88  std::size_t
89  size() const
90  {
91  return m_data.size();
92  }
93 
94  private:
95  std::vector<Ch> m_data; // File data
96  };
97 
98  //! Counts children of node. Time complexity is O(n).
99  //! \return Number of children of node
100  template <class Ch>
101  inline std::size_t
103  {
104  xml_node<Ch>* child = node->first_node();
105  std::size_t count = 0;
106 
107  while (child)
108  {
109  ++count;
110  child = child->next_sibling();
111  }
112 
113  return count;
114  }
115 
116  //! Counts attributes of node. Time complexity is O(n).
117  //! \return Number of attributes of node
118  template <class Ch>
119  inline std::size_t
121  {
122  xml_attribute<Ch>* attr = node->first_attribute();
123  std::size_t count = 0;
124 
125  while (attr)
126  {
127  ++count;
128  attr = attr->next_attribute();
129  }
130 
131  return count;
132  }
133 
134 } // namespace rapidxml
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:1189
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:986
rapidxml::count_children
std::size_t count_children(xml_node< Ch > *node)
Counts children of node.
Definition: rapidxml_utils.hpp:102
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:1224
rapidxml::file::data
Ch * data()
Gets file data.
Definition: rapidxml_utils.hpp:73
rapidxml::xml_attribute
Class representing attribute node of XML document.
Definition: rapidxml.hpp:142
rapidxml::file
Represents data loaded from a file.
Definition: rapidxml_utils.hpp:21
rapidxml
Definition: rapidxml.hpp:62
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:86
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:1084
rapidxml::xml_node
Class representing a node of XML document.
Definition: rapidxml.hpp:140
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:120
rapidxml::file::size
std::size_t size() const
Gets file data size.
Definition: rapidxml_utils.hpp:89
rapidxml::file::data
const Ch * data() const
Gets file data.
Definition: rapidxml_utils.hpp:81