cphot 0.1
A C++ tool for computing photometry from spectra.
rapidxml_utils.hpp
Go to the documentation of this file.
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
rapidxml::file::size
std::size_t size() const
Gets file data size.
Definition: rapidxml_utils.hpp:79
rapidxml::count_children
std::size_t count_children(xml_node< Ch > *node)
Counts children of node.
Definition: rapidxml_utils.hpp:93
rapidxml::file::file
file(std::basic_istream< Ch > &stream)
Loads file into the memory.
Definition: rapidxml_utils.hpp:51
rapidxml::xml_attribute
Class representing attribute node of XML document.
Definition: rapidxml.hpp:141
rapidxml::file
Represents data loaded from a file.
Definition: rapidxml_utils.hpp:21
rapidxml::file::data
Ch * data()
Gets file data.
Definition: rapidxml_utils.hpp:65
rapidxml
Definition: rapidxml.hpp:59
rapidxml::file::data
const Ch * data() const
Gets file data.
Definition: rapidxml_utils.hpp:72
rapidxml::xml_node::first_node
xml_node< Ch > * first_node(const Ch *name=0, const Ch *xmlns=0, std::size_t name_size=0, std::size_t xmlns_size=0, bool case_sensitive=true) const
Gets first child node, optionally matching node name.
Definition: rapidxml.hpp:1115
rapidxml::file::file
file(const char *filename)
Loads file into the memory.
Definition: rapidxml_utils.hpp:28
rapidxml::xml_node
Class representing a node of XML document.
Definition: rapidxml.hpp:140
rapidxml::xml_attribute::next_attribute
xml_attribute< Ch > * next_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Gets next attribute, optionally matching attribute name.
Definition: rapidxml.hpp:921
rapidxml.hpp
rapidxml::count_attributes
std::size_t count_attributes(xml_node< Ch > *node)
Counts attributes of node.
Definition: rapidxml_utils.hpp:108
rapidxml::xml_node::first_attribute
xml_attribute< Ch > * first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Gets first attribute of node, optionally matching attribute name.
Definition: rapidxml.hpp:1208
rapidxml::xml_node::next_sibling
xml_node< Ch > * next_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Gets next sibling node, optionally matching node name.
Definition: rapidxml.hpp:1187