Chris@16: // ---------------------------------------------------------------------------- Chris@16: // Copyright (C) 2002-2006 Marcin Kalicinski 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_INFO_PARSER_HPP_INCLUDED Chris@16: #define BOOST_PROPERTY_TREE_INFO_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: namespace boost { namespace property_tree { namespace info_parser Chris@16: { Chris@16: Chris@16: /** Chris@16: * Read INFO from a the given stream and translate it to a property tree. Chris@16: * @note Replaces the existing contents. Strong exception guarantee. Chris@16: * @throw info_parser_error If the stream cannot be read, doesn't contain Chris@16: * valid INFO, or a conversion fails. Chris@16: */ Chris@16: template Chris@16: void read_info(std::basic_istream &stream, Ptree &pt) Chris@16: { Chris@16: Ptree local; Chris@16: read_info_internal(stream, local, std::string(), 0); Chris@16: pt.swap(local); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Read INFO from a the given stream and translate it to a property tree. Chris@16: * @note Replaces the existing contents. Strong exception guarantee. Chris@16: * @param default_ptree If parsing fails, pt is set to a copy of this tree. Chris@16: */ Chris@16: template Chris@16: void read_info(std::basic_istream &stream, Ptree &pt, Chris@16: const Ptree &default_ptree) Chris@16: { Chris@16: try { Chris@16: read_info(stream, pt); Chris@16: } catch(file_parser_error &) { Chris@16: pt = default_ptree; Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * Read INFO from a the given file and translate it to a property tree. The Chris@16: * tree's key type must be a string type, i.e. it must have a nested Chris@16: * value_type typedef that is a valid parameter for basic_ifstream. Chris@16: * @note Replaces the existing contents. Strong exception guarantee. Chris@16: * @throw info_parser_error If the file cannot be read, doesn't contain Chris@16: * valid INFO, or a conversion fails. Chris@16: */ Chris@16: template Chris@16: void read_info(const std::string &filename, Ptree &pt, Chris@16: const std::locale &loc = std::locale()) Chris@16: { Chris@16: std::basic_ifstream Chris@16: stream(filename.c_str()); Chris@16: if (!stream) { Chris@16: BOOST_PROPERTY_TREE_THROW(info_parser_error( Chris@16: "cannot open file for reading", filename, 0)); Chris@16: } Chris@16: stream.imbue(loc); Chris@16: Ptree local; Chris@16: read_info_internal(stream, local, filename, 0); Chris@16: pt.swap(local); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Read INFO from a the given file and translate it to a property tree. The Chris@16: * tree's key type must be a string type, i.e. it must have a nested Chris@16: * value_type typedef that is a valid parameter for basic_ifstream. Chris@16: * @note Replaces the existing contents. Strong exception guarantee. Chris@16: * @param default_ptree If parsing fails, pt is set to a copy of this tree. Chris@16: */ Chris@16: template Chris@16: void read_info(const std::string &filename, Chris@16: Ptree &pt, Chris@16: const Ptree &default_ptree, Chris@16: const std::locale &loc = std::locale()) Chris@16: { Chris@16: try { Chris@16: read_info(filename, pt, loc); Chris@16: } catch(file_parser_error &) { Chris@16: pt = default_ptree; Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * Writes a tree to the stream in INFO format. Chris@16: * @throw info_parser_error If the stream cannot be written to, or a Chris@16: * conversion fails. Chris@16: * @param settings The settings to use when writing the INFO data. Chris@16: */ Chris@16: template Chris@16: void write_info(std::basic_ostream &stream, Chris@16: const Ptree &pt, Chris@16: const info_writer_settings &settings = Chris@16: info_writer_settings()) Chris@16: { Chris@16: write_info_internal(stream, pt, std::string(), settings); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Writes a tree to the file in INFO format. The tree's key type must be a Chris@16: * string type, i.e. it must have a nested value_type typedef that is a Chris@16: * valid parameter for basic_ofstream. Chris@16: * @throw info_parser_error If the file cannot be written to, or a Chris@16: * conversion fails. Chris@16: * @param settings The settings to use when writing the INFO data. Chris@16: */ Chris@16: template Chris@16: void write_info(const std::string &filename, Chris@16: const Ptree &pt, Chris@16: const std::locale &loc = std::locale(), Chris@16: const info_writer_settings< Chris@16: typename Ptree::key_type::value_type Chris@16: > &settings = Chris@16: info_writer_make_settings< Chris@16: typename Ptree::key_type::value_type>()) Chris@16: { Chris@16: std::basic_ofstream Chris@16: stream(filename.c_str()); Chris@16: if (!stream) { Chris@16: BOOST_PROPERTY_TREE_THROW(info_parser_error( Chris@16: "cannot open file for writing", filename, 0)); Chris@16: } Chris@16: stream.imbue(loc); Chris@16: write_info_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 info_parser::info_parser_error; Chris@16: using info_parser::read_info; Chris@16: using info_parser::write_info; Chris@16: using info_parser::info_writer_settings; Chris@16: using info_parser::info_writer_make_settings; Chris@16: } } Chris@16: Chris@16: #endif