From 2fdfe430a209a851fdd5a22377031099e6c30fe6 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Fri, 8 Dec 2006 14:18:07 +0000 Subject: PUGIXML_NO_STL mode, transfer_ownership ctor/parse function git-svn-id: http://pugixml.googlecode.com/svn/trunk@12 99668b35-9821-0410-8761-19e4c4f06640 --- src/pugixml.cpp | 37 ++++++++++++++++++++++++++++++++++--- src/pugixml.hpp | 30 ++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index c7ac5d1..382bf50 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -1621,6 +1621,7 @@ namespace pugi return xml_node(); // Not found. } +#ifndef PUGIXML_NO_STL std::string xml_node::path(char delimiter) const { std::string path; @@ -1641,6 +1642,7 @@ namespace pugi return path; } +#endif xml_node xml_node::first_element_by_path(const char* path, char delimiter) const { @@ -1844,16 +1846,23 @@ namespace pugi { } +#ifndef PUGIXML_NO_STL xml_parser::xml_parser(std::istream& stream,unsigned int optmsk): _xmldoc(0), _optmsk(optmsk) { parse(stream, _optmsk); } +#endif xml_parser::xml_parser(char* xmlstr,unsigned int optmsk): _xmldoc(0), _optmsk(optmsk) { parse(xmlstr, _optmsk); } + xml_parser::xml_parser(const transfer_ownership_tag& tag, char* xmlstr,unsigned int optmsk): _xmldoc(0), _optmsk(optmsk), _buffer(0) + { + parse(tag, xmlstr, _optmsk); + } + xml_parser::~xml_parser() { xml_memory_block* current = _memory.next; @@ -1888,6 +1897,7 @@ namespace pugi return prev; } +#ifndef PUGIXML_NO_STL void xml_parser::parse(std::istream& stream,unsigned int optmsk) { int length = 0, pos = stream.tellg(); @@ -1895,12 +1905,13 @@ namespace pugi length = stream.tellg(); stream.seekg(pos, std::ios_base::beg); - _buffer.resize(length + 1); - stream.read(&_buffer[0], length); + _buffer = new char[length + 1]; + stream.read(_buffer, length); _buffer[length] = 0; - parse(&_buffer[0],optmsk); // Parse the input string. + parse(_buffer, optmsk); // Parse the input string. } +#endif char* xml_parser::parse(char* xmlstr,unsigned int optmsk) { @@ -1917,6 +1928,25 @@ namespace pugi return parser.parse(xmlstr,_xmldoc,_optmsk); // Parse the input string. } + char* xml_parser::parse(const transfer_ownership_tag&, char* xmlstr,unsigned int optmsk) + { + if(!xmlstr) return NULL; + + delete[] _buffer; + _buffer = xmlstr; + + xml_allocator alloc(&_memory); + + _xmldoc = alloc.allocate(node_document); // Allocate a new root. + _xmldoc->parent = _xmldoc; // Point to self. + if(optmsk != parse_noset) _optmsk = optmsk; + + xml_parser_impl parser(alloc); + + return parser.parse(xmlstr,_xmldoc,_optmsk); // Parse the input string. + } + +#ifndef PUGIXML_NO_STL std::string utf8(const wchar_t* str) { std::string result; @@ -1946,4 +1976,5 @@ namespace pugi return result; } +#endif } diff --git a/src/pugixml.hpp b/src/pugixml.hpp index 7c65ebe..e651b84 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -14,9 +14,10 @@ #ifndef HEADER_PUGIXML_HPP #define HEADER_PUGIXML_HPP -#include -#include -#include +#ifndef PUGIXML_NO_STL +# include +# include +#endif /// The PugiXML Parser namespace. namespace pugi @@ -479,11 +480,13 @@ namespace pugi char data[memory_block_size]; }; + struct transfer_ownership_tag {}; + /// Provides a high-level interface to the XML parser. class xml_parser { private: - std::vector _buffer; ///< character buffer + char* _buffer; ///< character buffer xml_memory_block _memory; ///< Memory block @@ -504,11 +507,19 @@ namespace pugi /// \see parse xml_parser(char* xmlstr, unsigned int optmsk = parse_default); + /// Parse constructor that gains ownership. + /// \param xmlstr - readwrite string with xml data + /// \param optmsk - Options mask. + /// \see parse + xml_parser(const transfer_ownership_tag&, char* xmlstr, unsigned int optmsk = parse_default); + +#ifndef PUGIXML_NO_STL /// Parse constructor. /// \param stream - stream with xml data /// \param optmsk - Options mask. /// \see parse xml_parser(std::istream& stream, unsigned int optmsk = parse_default); +#endif /// Dtor ~xml_parser(); @@ -528,10 +539,12 @@ namespace pugi unsigned int options(unsigned int optmsk); public: +#ifndef PUGIXML_NO_STL /// Parse the given XML stream /// \param stream - stream with xml data /// \param optmsk - Options mask. void parse(std::istream& stream, unsigned int optmsk = parse_noset); +#endif /// Parse the given XML string in-situ. /// \param xmlstr - readwrite string with xml data @@ -539,15 +552,24 @@ namespace pugi /// \return last position or NULL /// \rem input string is zero-segmented char* parse(char* xmlstr, unsigned int optmsk = parse_noset); + + /// Parse the given XML string in-situ (gains ownership). + /// \param xmlstr - readwrite string with xml data + /// \param optmsk - Options mask. + /// \return last position or NULL + /// \rem input string is zero-segmented + char* parse(const transfer_ownership_tag&, char* xmlstr, unsigned int optmsk = parse_noset); }; /// Utility functions for xml +#ifndef PUGIXML_NO_STL /// Convert utf16 to utf8 std::string utf8(const wchar_t* str); /// Convert utf8 to utf16 std::wstring utf16(const char* str); +#endif } /// Inline implementation -- cgit v1.2.3