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