Chris@16: // ---------------------------------------------------------------------------- Chris@16: // Copyright (C) 2002-2006 Marcin Kalicinski Chris@16: // Copyright (C) 2009 Sebastian Redl Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see www.boost.org Chris@16: // ---------------------------------------------------------------------------- Chris@16: #ifndef BOOST_PROPERTY_TREE_XML_PARSER_HPP_INCLUDED Chris@16: #define BOOST_PROPERTY_TREE_XML_PARSER_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace property_tree { namespace xml_parser Chris@16: { Chris@16: Chris@16: /** Chris@16: * Reads XML from an input stream and translates it to property tree. Chris@16: * @note Clears existing contents of property tree. In case of error the Chris@16: * property tree unmodified. Chris@16: * @note XML attributes are placed under keys named @c \. Chris@16: * @throw xml_parser_error In case of error deserializing the property tree. Chris@16: * @param stream Stream from which to read in the property tree. Chris@16: * @param[out] pt The property tree to populate. Chris@16: * @param flags Flags controlling the behaviour of the parser. Chris@16: * The following flags are supported: Chris@16: * @li @c no_concat_text -- Prevents concatenation of text nodes into Chris@16: * datastring of property tree. Puts them in Chris@16: * separate @c \ strings instead. Chris@16: * @li @c no_comments -- Skip XML comments. Chris@16: * @li @c trim_whitespace -- Trim leading and trailing whitespace from text, Chris@16: * and collapse sequences of whitespace. Chris@16: */ Chris@16: template Chris@16: void read_xml(std::basic_istream< Chris@16: typename Ptree::key_type::value_type Chris@16: > &stream, Chris@16: Ptree &pt, Chris@16: int flags = 0) Chris@16: { Chris@16: read_xml_internal(stream, pt, flags, std::string()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Reads XML from a file using the given locale and translates it to Chris@16: * property tree. Chris@16: * @note Clears existing contents of property tree. In case of error the Chris@16: * property tree unmodified. Chris@16: * @note XML attributes are placed under keys named @c \. Chris@16: * @throw xml_parser_error In case of error deserializing the property tree. Chris@16: * @param filename The file from which to read in the property tree. Chris@16: * @param[out] pt The property tree to populate. Chris@16: * @param flags Flags controlling the bahviour of the parser. Chris@16: * The following flags are supported: Chris@16: * @li @c no_concat_text -- Prevents concatenation of text nodes into Chris@16: * datastring of property tree. Puts them in Chris@16: * separate @c \ strings instead. Chris@16: * @li @c no_comments -- Skip XML comments. Chris@16: * @param loc The locale to use when reading in the file contents. Chris@16: */ Chris@16: template Chris@16: void read_xml(const std::string &filename, Chris@16: Ptree &pt, Chris@16: int flags = 0, Chris@16: const std::locale &loc = std::locale()) Chris@16: { Chris@16: BOOST_ASSERT(validate_flags(flags)); Chris@16: std::basic_ifstream Chris@16: stream(filename.c_str()); Chris@16: if (!stream) Chris@16: BOOST_PROPERTY_TREE_THROW(xml_parser_error( Chris@16: "cannot open file", filename, 0)); Chris@16: stream.imbue(loc); Chris@16: read_xml_internal(stream, pt, flags, filename); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Translates the property tree to XML and writes it the given output Chris@16: * stream. Chris@16: * @throw xml_parser_error In case of error translating the property tree to Chris@16: * XML or writing to the output stream. Chris@16: * @param stream The stream to which to write the XML representation of the Chris@16: * property tree. Chris@16: * @param pt The property tree to tranlsate to XML and output. Chris@16: * @param settings The settings to use when writing out the property tree as Chris@16: * XML. Chris@16: */ Chris@16: template Chris@16: void write_xml(std::basic_ostream< Chris@16: typename Ptree::key_type::value_type Chris@16: > &stream, Chris@16: const Ptree &pt, Chris@16: const xml_writer_settings< Chris@101: typename Ptree::key_type Chris@16: > & settings = xml_writer_settings< Chris@101: typename Ptree::key_type>() ) Chris@16: { Chris@16: write_xml_internal(stream, pt, std::string(), settings); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Translates the property tree to XML and writes it the given file. Chris@16: * @throw xml_parser_error In case of error translating the property tree to Chris@16: * XML or writing to the output stream. Chris@16: * @param filename The file to which to write the XML representation of the Chris@16: * property tree. Chris@16: * @param pt The property tree to tranlsate to XML and output. Chris@16: * @param loc The locale to use when writing the output to file. Chris@16: * @param settings The settings to use when writing out the property tree as Chris@16: * XML. Chris@16: */ Chris@16: template Chris@16: void write_xml(const std::string &filename, Chris@16: const Ptree &pt, Chris@16: const std::locale &loc = std::locale(), Chris@16: const xml_writer_settings< Chris@101: typename Ptree::key_type Chris@101: > & settings = xml_writer_settings()) Chris@16: { Chris@16: std::basic_ofstream Chris@16: stream(filename.c_str()); Chris@16: if (!stream) Chris@16: BOOST_PROPERTY_TREE_THROW(xml_parser_error( Chris@16: "cannot open file", filename, 0)); Chris@16: stream.imbue(loc); Chris@16: write_xml_internal(stream, pt, filename, settings); Chris@16: } Chris@16: Chris@16: } } } Chris@16: Chris@16: namespace boost { namespace property_tree Chris@16: { Chris@16: using xml_parser::read_xml; Chris@16: using xml_parser::write_xml; Chris@16: using xml_parser::xml_parser_error; Chris@16: Chris@16: using xml_parser::xml_writer_settings; Chris@16: using xml_parser::xml_writer_make_settings; Chris@16: } } Chris@16: Chris@16: #endif