1 // ---------------------------------------------------------------------------- 2 // Copyright (C) 2002-2006 Marcin Kalicinski 3 // 4 // Distributed under the Boost Software License, Version 1.0. 5 // (See accompanying file LICENSE_1_0.txt or copy at 6 // http://www.boost.org/LICENSE_1_0.txt) 7 // 8 // For more information, see www.boost.org 9 // ---------------------------------------------------------------------------- 10 #ifndef BOOST_PROPERTY_TREE_INFO_PARSER_HPP_INCLUDED 11 #define BOOST_PROPERTY_TREE_INFO_PARSER_HPP_INCLUDED 12 13 #include <boost/property_tree/ptree.hpp> 14 #include <boost/property_tree/detail/info_parser_error.hpp> 15 #include <boost/property_tree/detail/info_parser_writer_settings.hpp> 16 #include <boost/property_tree/detail/info_parser_read.hpp> 17 #include <boost/property_tree/detail/info_parser_write.hpp> 18 #include <istream> 19 20 namespace boost { namespace property_tree { namespace info_parser 21 { 22 23 /** 24 * Read INFO from a the given stream and translate it to a property tree. 25 * @note Replaces the existing contents. Strong exception guarantee. 26 * @throw info_parser_error If the stream cannot be read, doesn't contain 27 * valid INFO, or a conversion fails. 28 */ 29 template<class Ptree, class Ch> read_info(std::basic_istream<Ch> & stream,Ptree & pt)30 void read_info(std::basic_istream<Ch> &stream, Ptree &pt) 31 { 32 Ptree local; 33 read_info_internal(stream, local, std::string(), 0); 34 pt.swap(local); 35 } 36 37 /** 38 * Read INFO from a the given stream and translate it to a property tree. 39 * @note Replaces the existing contents. Strong exception guarantee. 40 * @param default_ptree If parsing fails, pt is set to a copy of this tree. 41 */ 42 template<class Ptree, class Ch> read_info(std::basic_istream<Ch> & stream,Ptree & pt,const Ptree & default_ptree)43 void read_info(std::basic_istream<Ch> &stream, Ptree &pt, 44 const Ptree &default_ptree) 45 { 46 try { 47 read_info(stream, pt); 48 } catch(file_parser_error &) { 49 pt = default_ptree; 50 } 51 } 52 53 /** 54 * Read INFO from a the given file and translate it to a property tree. The 55 * tree's key type must be a string type, i.e. it must have a nested 56 * value_type typedef that is a valid parameter for basic_ifstream. 57 * @note Replaces the existing contents. Strong exception guarantee. 58 * @throw info_parser_error If the file cannot be read, doesn't contain 59 * valid INFO, or a conversion fails. 60 */ 61 template<class Ptree> read_info(const std::string & filename,Ptree & pt,const std::locale & loc=std::locale ())62 void read_info(const std::string &filename, Ptree &pt, 63 const std::locale &loc = std::locale()) 64 { 65 std::basic_ifstream<typename Ptree::key_type::value_type> 66 stream(filename.c_str()); 67 if (!stream) { 68 BOOST_PROPERTY_TREE_THROW(info_parser_error( 69 "cannot open file for reading", filename, 0)); 70 } 71 stream.imbue(loc); 72 Ptree local; 73 read_info_internal(stream, local, filename, 0); 74 pt.swap(local); 75 } 76 77 /** 78 * Read INFO from a the given file and translate it to a property tree. The 79 * tree's key type must be a string type, i.e. it must have a nested 80 * value_type typedef that is a valid parameter for basic_ifstream. 81 * @note Replaces the existing contents. Strong exception guarantee. 82 * @param default_ptree If parsing fails, pt is set to a copy of this tree. 83 */ 84 template<class Ptree> read_info(const std::string & filename,Ptree & pt,const Ptree & default_ptree,const std::locale & loc=std::locale ())85 void read_info(const std::string &filename, 86 Ptree &pt, 87 const Ptree &default_ptree, 88 const std::locale &loc = std::locale()) 89 { 90 try { 91 read_info(filename, pt, loc); 92 } catch(file_parser_error &) { 93 pt = default_ptree; 94 } 95 } 96 97 /** 98 * Writes a tree to the stream in INFO format. 99 * @throw info_parser_error If the stream cannot be written to, or a 100 * conversion fails. 101 * @param settings The settings to use when writing the INFO data. 102 */ 103 template<class Ptree, class Ch> write_info(std::basic_ostream<Ch> & stream,const Ptree & pt,const info_writer_settings<Ch> & settings=info_writer_settings<Ch> ())104 void write_info(std::basic_ostream<Ch> &stream, 105 const Ptree &pt, 106 const info_writer_settings<Ch> &settings = 107 info_writer_settings<Ch>()) 108 { 109 write_info_internal(stream, pt, std::string(), settings); 110 } 111 112 /** 113 * Writes a tree to the file in INFO format. The tree's key type must be a 114 * string type, i.e. it must have a nested value_type typedef that is a 115 * valid parameter for basic_ofstream. 116 * @throw info_parser_error If the file cannot be written to, or a 117 * conversion fails. 118 * @param settings The settings to use when writing the INFO data. 119 */ 120 template<class Ptree> write_info(const std::string & filename,const Ptree & pt,const std::locale & loc=std::locale (),const info_writer_settings<typename Ptree::key_type::value_type> & settings=info_writer_make_settings<typename Ptree::key_type::value_type> ())121 void write_info(const std::string &filename, 122 const Ptree &pt, 123 const std::locale &loc = std::locale(), 124 const info_writer_settings< 125 typename Ptree::key_type::value_type 126 > &settings = 127 info_writer_make_settings< 128 typename Ptree::key_type::value_type>()) 129 { 130 std::basic_ofstream<typename Ptree::key_type::value_type> 131 stream(filename.c_str()); 132 if (!stream) { 133 BOOST_PROPERTY_TREE_THROW(info_parser_error( 134 "cannot open file for writing", filename, 0)); 135 } 136 stream.imbue(loc); 137 write_info_internal(stream, pt, filename, settings); 138 } 139 140 } } } 141 142 namespace boost { namespace property_tree 143 { 144 using info_parser::info_parser_error; 145 using info_parser::read_info; 146 using info_parser::write_info; 147 using info_parser::info_writer_settings; 148 using info_parser::info_writer_make_settings; 149 } } 150 151 #endif 152