rapidxml_utils.hpp
Go to the documentation of this file.
00001 #ifndef RAPIDXML_UTILS_HPP_INCLUDED
00002 #define RAPIDXML_UTILS_HPP_INCLUDED
00003 
00004 // Copyright (C) 2006, 2009 Marcin Kalicinski
00005 // Version 1.13
00006 // Revision $DateTime: 2009/05/13 01:46:17 $
00009 
00010 #include "rapidxml.hpp"
00011 #include <vector>
00012 #include <string>
00013 #include <fstream>
00014 #include <stdexcept>
00015 
00016 namespace rapidxml
00017 {
00018 
00020     template<class Ch = char>
00021     class file
00022     {
00023         
00024     public:
00025         
00028         file(const char *filename)
00029         {
00030             using namespace std;
00031 
00032             // Open stream
00033             basic_ifstream<Ch> stream(filename, ios::binary);
00034             if (!stream)
00035                 throw runtime_error(string("cannot open file ") + filename);
00036             stream.unsetf(ios::skipws);
00037             
00038             // Determine stream size
00039             stream.seekg(0, ios::end);
00040             size_t size = stream.tellg();
00041             stream.seekg(0);   
00042             
00043             // Load data and add terminating 0
00044             m_data.resize(size + 1);
00045             stream.read(&m_data.front(), static_cast<streamsize>(size));
00046             m_data[size] = 0;
00047         }
00048 
00051         file(std::basic_istream<Ch> &stream)
00052         {
00053             using namespace std;
00054 
00055             // Load data and add terminating 0
00056             stream.unsetf(ios::skipws);
00057             m_data.assign(istreambuf_iterator<Ch>(stream), istreambuf_iterator<Ch>());
00058             if (stream.fail() || stream.bad())
00059                 throw runtime_error("error reading stream");
00060             m_data.push_back(0);
00061         }
00062         
00065         Ch *data()
00066         {
00067             return &m_data.front();
00068         }
00069 
00072         const Ch *data() const
00073         {
00074             return &m_data.front();
00075         }
00076 
00079         std::size_t size() const
00080         {
00081             return m_data.size();
00082         }
00083 
00084     private:
00085 
00086         std::vector<Ch> m_data;   // File data
00087 
00088     };
00089 
00092     template<class Ch>
00093     inline std::size_t count_children(xml_node<Ch> *node)
00094     {
00095         xml_node<Ch> *child = node->first_node();
00096         std::size_t count = 0;
00097         while (child)
00098         {
00099             ++count;
00100             child = child->next_sibling();
00101         }
00102         return count;
00103     }
00104 
00107     template<class Ch>
00108     inline std::size_t count_attributes(xml_node<Ch> *node)
00109     {
00110         xml_attribute<Ch> *attr = node->first_attribute();
00111         std::size_t count = 0;
00112         while (attr)
00113         {
00114             ++count;
00115             attr = attr->next_attribute();
00116         }
00117         return count;
00118     }
00119 
00120 }
00121 
00122 #endif


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:35