info_parser.hpp 5.6 KB

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