123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #ifndef RAPIDXML_UTILS_HPP_INCLUDED
- #define RAPIDXML_UTILS_HPP_INCLUDED
- #include "rapidxml.hpp"
- #include <vector>
- #include <string>
- #include <fstream>
- #include <stdexcept>
- namespace rapidxml
- {
-
- template<class Ch = char>
- class file
- {
-
- public:
-
-
-
- file(const char *filename)
- {
- using namespace std;
-
- basic_ifstream<Ch> stream(filename, ios::binary);
- if (!stream)
- throw runtime_error(string("cannot open file ") + filename);
- stream.unsetf(ios::skipws);
-
-
- stream.seekg(0, ios::end);
- size_t size = stream.tellg();
- stream.seekg(0);
-
-
- m_data.resize(size + 1);
- stream.read(&m_data.front(), static_cast<streamsize>(size));
- m_data[size] = 0;
- }
-
-
- file(std::basic_istream<Ch> &stream)
- {
- using namespace std;
-
- stream.unsetf(ios::skipws);
- m_data.assign(istreambuf_iterator<Ch>(stream), istreambuf_iterator<Ch>());
- if (stream.fail() || stream.bad())
- throw runtime_error("error reading stream");
- m_data.push_back(0);
- }
-
-
-
- Ch *data()
- {
- return &m_data.front();
- }
-
-
- const Ch *data() const
- {
- return &m_data.front();
- }
-
-
- std::size_t size() const
- {
- return m_data.size();
- }
- private:
- std::vector<Ch> m_data;
- };
-
-
- template<class Ch>
- inline std::size_t count_children(xml_node<Ch> *node)
- {
- xml_node<Ch> *child = node->first_node();
- std::size_t count = 0;
- while (child)
- {
- ++count;
- child = child->next_sibling();
- }
- return count;
- }
-
-
- template<class Ch>
- inline std::size_t count_attributes(xml_node<Ch> *node)
- {
- xml_attribute<Ch> *attr = node->first_attribute();
- std::size_t count = 0;
- while (attr)
- {
- ++count;
- attr = attr->next_attribute();
- }
- return count;
- }
- }
- #endif
|