info_parser_write.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_DETAIL_INFO_PARSER_WRITE_HPP_INCLUDED
  11. #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_WRITE_HPP_INCLUDED
  12. #include "boost/property_tree/ptree.hpp"
  13. #include "boost/property_tree/detail/info_parser_utils.hpp"
  14. #include <string>
  15. namespace boost { namespace property_tree { namespace info_parser
  16. {
  17. template<class Ch>
  18. void write_info_indent(std::basic_ostream<Ch> &stream,
  19. int indent,
  20. const info_writer_settings<Ch> &settings
  21. )
  22. {
  23. stream << std::basic_string<Ch>(indent * settings.indent_count, settings.indent_char);
  24. }
  25. // Create necessary escape sequences from illegal characters
  26. template<class Ch>
  27. std::basic_string<Ch> create_escapes(const std::basic_string<Ch> &s)
  28. {
  29. std::basic_string<Ch> result;
  30. typename std::basic_string<Ch>::const_iterator b = s.begin();
  31. typename std::basic_string<Ch>::const_iterator e = s.end();
  32. while (b != e)
  33. {
  34. if (*b == Ch('\0')) result += Ch('\\'), result += Ch('0');
  35. else if (*b == Ch('\a')) result += Ch('\\'), result += Ch('a');
  36. else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b');
  37. else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');
  38. else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n');
  39. else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r');
  40. else if (*b == Ch('\v')) result += Ch('\\'), result += Ch('v');
  41. else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
  42. else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\');
  43. else
  44. result += *b;
  45. ++b;
  46. }
  47. return result;
  48. }
  49. template<class Ch>
  50. bool is_simple_key(const std::basic_string<Ch> &key)
  51. {
  52. const static std::basic_string<Ch> chars = convert_chtype<Ch, char>(" \t{};\n\"");
  53. return !key.empty() && key.find_first_of(chars) == key.npos;
  54. }
  55. template<class Ch>
  56. bool is_simple_data(const std::basic_string<Ch> &data)
  57. {
  58. const static std::basic_string<Ch> chars = convert_chtype<Ch, char>(" \t{};\n\"");
  59. return !data.empty() && data.find_first_of(chars) == data.npos;
  60. }
  61. template<class Ptree>
  62. void write_info_helper(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
  63. const Ptree &pt,
  64. int indent,
  65. const info_writer_settings<typename Ptree::key_type::value_type> &settings)
  66. {
  67. // Character type
  68. typedef typename Ptree::key_type::value_type Ch;
  69. // Write data
  70. if (indent >= 0)
  71. {
  72. if (!pt.data().empty())
  73. {
  74. std::basic_string<Ch> data = create_escapes(pt.template get_value<std::basic_string<Ch> >());
  75. if (is_simple_data(data))
  76. stream << Ch(' ') << data << Ch('\n');
  77. else
  78. stream << Ch(' ') << Ch('\"') << data << Ch('\"') << Ch('\n');
  79. }
  80. else if (pt.empty())
  81. stream << Ch(' ') << Ch('\"') << Ch('\"') << Ch('\n');
  82. else
  83. stream << Ch('\n');
  84. }
  85. // Write keys
  86. if (!pt.empty())
  87. {
  88. // Open brace
  89. if (indent >= 0)
  90. {
  91. write_info_indent( stream, indent, settings);
  92. stream << Ch('{') << Ch('\n');
  93. }
  94. // Write keys
  95. typename Ptree::const_iterator it = pt.begin();
  96. for (; it != pt.end(); ++it)
  97. {
  98. // Output key
  99. std::basic_string<Ch> key = create_escapes(it->first);
  100. write_info_indent( stream, indent+1, settings);
  101. if (is_simple_key(key))
  102. stream << key;
  103. else
  104. stream << Ch('\"') << key << Ch('\"');
  105. // Output data and children
  106. write_info_helper(stream, it->second, indent + 1, settings);
  107. }
  108. // Close brace
  109. if (indent >= 0)
  110. {
  111. write_info_indent( stream, indent, settings);
  112. stream << Ch('}') << Ch('\n');
  113. }
  114. }
  115. }
  116. // Write ptree to info stream
  117. template<class Ptree>
  118. void write_info_internal(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
  119. const Ptree &pt,
  120. const std::string &filename,
  121. const info_writer_settings<typename Ptree::key_type::value_type> &settings)
  122. {
  123. write_info_helper(stream, pt, -1, settings);
  124. if (!stream.good())
  125. BOOST_PROPERTY_TREE_THROW(info_parser_error("write error", filename, 0));
  126. }
  127. } } }
  128. #endif