Skip to content
Snippets Groups Projects
pugixml.hpp 44.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Radim Vavřík's avatar
    Radim Vavřík committed
    		// Add a new variable or get the existing one, if the types match
    		xpath_variable* add(const char_t* name, xpath_value_type type);
    
    		// Set value of an existing variable; no type conversion is performed, false is returned if there is no such variable or if types mismatch
    		bool set(const char_t* name, bool value);
    		bool set(const char_t* name, double value);
    		bool set(const char_t* name, const char_t* value);
    		bool set(const char_t* name, const xpath_node_set& value);
    
    		// Get existing variable by name
    		xpath_variable* get(const char_t* name);
    		const xpath_variable* get(const char_t* name) const;
    	};
    
    	// A compiled XPath query object
    	class PUGIXML_CLASS xpath_query
    	{
    	private:
    		void* _impl;
    		xpath_parse_result _result;
    
    		typedef void (*unspecified_bool_type)(xpath_query***);
    
    		// Non-copyable semantics
    		xpath_query(const xpath_query&);
    		xpath_query& operator=(const xpath_query&);
    
    	public:
    		// Construct a compiled object from XPath expression.
    		// If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on compilation errors.
    		explicit xpath_query(const char_t* query, xpath_variable_set* variables = 0);
    
    		// Destructor
    		~xpath_query();
    
    		// Get query expression return type
    		xpath_value_type return_type() const;
    		
    		// Evaluate expression as boolean value in the specified context; performs type conversion if necessary.
    		// If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors.
    		bool evaluate_boolean(const xpath_node& n) const;
    		
    		// Evaluate expression as double value in the specified context; performs type conversion if necessary.
    		// If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors.
    		double evaluate_number(const xpath_node& n) const;
    		
    	#ifndef PUGIXML_NO_STL
    		// Evaluate expression as string value in the specified context; performs type conversion if necessary.
    		// If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors.
    		string_t evaluate_string(const xpath_node& n) const;
    	#endif
    		
    		// Evaluate expression as string value in the specified context; performs type conversion if necessary.
    		// At most capacity characters are written to the destination buffer, full result size is returned (includes terminating zero).
    		// If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors.
    		// If PUGIXML_NO_EXCEPTIONS is defined, returns empty  set instead.
    		size_t evaluate_string(char_t* buffer, size_t capacity, const xpath_node& n) const;
    
    		// Evaluate expression as node set in the specified context.
    		// If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on type mismatch and std::bad_alloc on out of memory errors.
    		// If PUGIXML_NO_EXCEPTIONS is defined, returns empty node set instead.
    		xpath_node_set evaluate_node_set(const xpath_node& n) const;
    
    		// Get parsing result (used to get compilation errors in PUGIXML_NO_EXCEPTIONS mode)
    		const xpath_parse_result& result() const;
    
    		// Safe bool conversion operator
    		operator unspecified_bool_type() const;
    
    		// Borland C++ workaround
    		bool operator!() const;
    	};
    	
    	#ifndef PUGIXML_NO_EXCEPTIONS
    	// XPath exception class
    	class PUGIXML_CLASS xpath_exception: public std::exception
    	{
    	private:
    		xpath_parse_result _result;
    
    	public:
    		// Construct exception from parse result
    		explicit xpath_exception(const xpath_parse_result& result);
    
    		// Get error message
    		virtual const char* what() const throw();
    
    		// Get parse result
    		const xpath_parse_result& result() const;
    	};
    	#endif
    	
    	// XPath node class (either xml_node or xml_attribute)
    	class PUGIXML_CLASS xpath_node
    	{
    	private:
    		xml_node _node;
    		xml_attribute _attribute;
    	
    		typedef void (*unspecified_bool_type)(xpath_node***);
    
    	public:
    		// Default constructor; constructs empty XPath node
    		xpath_node();
    		
    		// Construct XPath node from XML node/attribute
    		xpath_node(const xml_node& node);
    		xpath_node(const xml_attribute& attribute, const xml_node& parent);
    
    		// Get node/attribute, if any
    		xml_node node() const;
    		xml_attribute attribute() const;
    		
    		// Get parent of contained node/attribute
    		xml_node parent() const;
    
    		// Safe bool conversion operator
    		operator unspecified_bool_type() const;
    		
    		// Borland C++ workaround
    		bool operator!() const;
    
    		// Comparison operators
    		bool operator==(const xpath_node& n) const;
    		bool operator!=(const xpath_node& n) const;
    	};
    
    #ifdef __BORLANDC__
    	// Borland C++ workaround
    	bool PUGIXML_FUNCTION operator&&(const xpath_node& lhs, bool rhs);
    	bool PUGIXML_FUNCTION operator||(const xpath_node& lhs, bool rhs);
    #endif
    
    	// A fixed-size collection of XPath nodes
    	class PUGIXML_CLASS xpath_node_set
    	{
    	public:
    		// Collection type
    		enum type_t
    		{
    			type_unsorted,			// Not ordered
    			type_sorted,			// Sorted by document order (ascending)
    			type_sorted_reverse		// Sorted by document order (descending)
    		};
    		
    		// Constant iterator type
    		typedef const xpath_node* const_iterator;
    	
    		// Default constructor. Constructs empty set.
    		xpath_node_set();
    
    		// Constructs a set from iterator range; data is not checked for duplicates and is not sorted according to provided type, so be careful
    		xpath_node_set(const_iterator begin, const_iterator end, type_t type = type_unsorted);
    
    		// Destructor
    		~xpath_node_set();
    		
    		// Copy constructor/assignment operator
    		xpath_node_set(const xpath_node_set& ns);
    		xpath_node_set& operator=(const xpath_node_set& ns);
    
    		// Get collection type
    		type_t type() const;
    		
    		// Get collection size
    		size_t size() const;
    
    		// Indexing operator
    		const xpath_node& operator[](size_t index) const;
    		
    		// Collection iterators
    		const_iterator begin() const;
    		const_iterator end() const;
    
    		// Sort the collection in ascending/descending order by document order
    		void sort(bool reverse = false);
    		
    		// Get first node in the collection by document order
    		xpath_node first() const;
    		
    		// Check if collection is empty
    		bool empty() const;
    	
    	private:
    		type_t _type;
    		
    		xpath_node _storage;
    		
    		xpath_node* _begin;
    		xpath_node* _end;
    
    		void _assign(const_iterator begin, const_iterator end);
    	};
    #endif
    
    #ifndef PUGIXML_NO_STL
    	// Convert wide string to UTF8
    	std::basic_string<char, std::char_traits<char>, std::allocator<char> > PUGIXML_FUNCTION as_utf8(const wchar_t* str);
    	std::basic_string<char, std::char_traits<char>, std::allocator<char> > PUGIXML_FUNCTION as_utf8(const std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >& str);
    	
    	// Convert UTF8 to wide string
    	std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > PUGIXML_FUNCTION as_wide(const char* str);
    	std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > PUGIXML_FUNCTION as_wide(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >& str);
    #endif
    
    	// Memory allocation function interface; returns pointer to allocated memory or NULL on failure
    	typedef void* (*allocation_function)(size_t size);
    	
    	// Memory deallocation function interface
    	typedef void (*deallocation_function)(void* ptr);
    
    	// Override default memory management functions. All subsequent allocations/deallocations will be performed via supplied functions.
    	void PUGIXML_FUNCTION set_memory_management_functions(allocation_function allocate, deallocation_function deallocate);
    	
    	// Get current memory management functions
    	allocation_function PUGIXML_FUNCTION get_memory_allocation_function();
    	deallocation_function PUGIXML_FUNCTION get_memory_deallocation_function();
    }
    
    #if !defined(PUGIXML_NO_STL) && (defined(_MSC_VER) || defined(__ICC))
    namespace std
    {
    	// Workarounds for (non-standard) iterator category detection for older versions (MSVC7/IC8 and earlier)
    	std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_node_iterator&);
    	std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_attribute_iterator&);
    	std::forward_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_named_node_iterator&);
    }
    #endif
    
    #if !defined(PUGIXML_NO_STL) && defined(__SUNPRO_CC)
    namespace std
    {
    	// Workarounds for (non-standard) iterator category detection
    	std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_node_iterator&);
    	std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_attribute_iterator&);
    	std::forward_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_named_node_iterator&);
    }
    #endif
    
    #endif
    
    /**
     * Copyright (c) 2006-2012 Arseny Kapoulkine
     *
     * Permission is hereby granted, free of charge, to any person
     * obtaining a copy of this software and associated documentation
     * files (the "Software"), to deal in the Software without
     * restriction, including without limitation the rights to use,
     * copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the
     * Software is furnished to do so, subject to the following
     * conditions:
     *
     * The above copyright notice and this permission notice shall be
     * included in all copies or substantial portions of the Software.
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
     * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
     * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     * OTHER DEALINGS IN THE SOFTWARE.
     */