inline
68 | P ptree_bad_path::path() const
69 | {
70 | return boost::any_cast(m_path);
71 | }
72 |
73 | }}
74 |
75 | #endif
76 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/file_parser_error.hpp:
--------------------------------------------------------------------------------
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_FILE_PARSER_ERROR_HPP_INCLUDED
11 | #define BOOST_PROPERTY_TREE_DETAIL_FILE_PARSER_ERROR_HPP_INCLUDED
12 |
13 | #include
14 | #include
15 | #include
16 |
17 | namespace boost { namespace property_tree
18 | {
19 |
20 | //! File parse error
21 | class file_parser_error: public ptree_error
22 | {
23 |
24 | public:
25 |
26 | ///////////////////////////////////////////////////////////////////////
27 | // Construction
28 |
29 | // Construct error
30 | file_parser_error(const std::string &msg,
31 | const std::string &file,
32 | unsigned long l) :
33 | ptree_error(format_what(msg, file, l)),
34 | m_message(msg), m_filename(file), m_line(l)
35 | {
36 | }
37 |
38 | ///////////////////////////////////////////////////////////////////////
39 | // Data access
40 |
41 | // Get error message (without line and file - use what() to get
42 | // full message)
43 | std::string message() const
44 | {
45 | return m_message;
46 | }
47 |
48 | // Get error filename
49 | std::string filename() const
50 | {
51 | return m_filename;
52 | }
53 |
54 | // Get error line number
55 | unsigned long line() const
56 | {
57 | return m_line;
58 | }
59 |
60 | private:
61 |
62 | std::string m_message;
63 | std::string m_filename;
64 | unsigned long m_line;
65 |
66 | // Format error message to be returned by std::runtime_error::what()
67 | static std::string format_what(const std::string &msg,
68 | const std::string &file,
69 | unsigned long l)
70 | {
71 | std::stringstream stream;
72 | stream << (file.empty() ? "" : file.c_str());
73 | if (l > 0)
74 | stream << '(' << l << ')';
75 | stream << ": " << msg;
76 | return stream.str();
77 | }
78 |
79 | };
80 |
81 | } }
82 |
83 | #endif
84 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/info_parser_error.hpp:
--------------------------------------------------------------------------------
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_ERROR_HPP_INCLUDED
11 | #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_ERROR_HPP_INCLUDED
12 |
13 | #include
14 | #include
15 |
16 | namespace boost { namespace property_tree { namespace info_parser
17 | {
18 |
19 | class info_parser_error: public file_parser_error
20 | {
21 | public:
22 | info_parser_error(const std::string &message,
23 | const std::string &filename,
24 | unsigned long line) :
25 | file_parser_error(message, filename, line)
26 | {
27 | }
28 | };
29 |
30 | } } }
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/info_parser_utils.hpp:
--------------------------------------------------------------------------------
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_CHCONV_HPP_INCLUDED
11 | #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_CHCONV_HPP_INCLUDED
12 |
13 | #include
14 |
15 | namespace boost { namespace property_tree { namespace info_parser
16 | {
17 |
18 | template
19 | std::basic_string convert_chtype(const ChSrc *text)
20 | {
21 | std::basic_string result;
22 | while (*text)
23 | {
24 | result += ChDest(*text);
25 | ++text;
26 | }
27 | return result;
28 | }
29 |
30 | } } }
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/info_parser_write.hpp:
--------------------------------------------------------------------------------
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
14 | #include
15 | #include
16 | #include
17 |
18 | #include
19 |
20 | namespace boost { namespace property_tree { namespace info_parser
21 | {
22 | template
23 | void write_info_indent(std::basic_ostream &stream,
24 | int indent,
25 | const info_writer_settings &settings
26 | )
27 | {
28 | stream << std::basic_string(indent * settings.indent_count, settings.indent_char);
29 | }
30 |
31 | // Create necessary escape sequences from illegal characters
32 | template
33 | std::basic_string create_escapes(const std::basic_string &s)
34 | {
35 | std::basic_string result;
36 | typename std::basic_string::const_iterator b = s.begin();
37 | typename std::basic_string::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
57 | bool is_simple_key(const std::basic_string &key)
58 | {
59 | const static std::basic_string chars = convert_chtype(" \t{};\n\"");
60 | return !key.empty() && key.find_first_of(chars) == key.npos;
61 | }
62 |
63 | template
64 | bool is_simple_data(const std::basic_string &data)
65 | {
66 | const static std::basic_string chars = convert_chtype(" \t{};\n\"");
67 | return !data.empty() && data.find_first_of(chars) == data.npos;
68 | }
69 |
70 | template
71 | void write_info_helper(std::basic_ostream &stream,
72 | const Ptree &pt,
73 | int indent,
74 | const info_writer_settings &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 data = create_escapes(pt.template get_value >());
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 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
138 | void write_info_internal(std::basic_ostream &stream,
139 | const Ptree &pt,
140 | const std::string &filename,
141 | const info_writer_settings &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 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/info_parser_writer_settings.hpp:
--------------------------------------------------------------------------------
1 | // ----------------------------------------------------------------------------
2 | // Copyright (C) 2002-2006 Marcin Kalicinski
3 | // Copyright (C) 2007 Alexey Baskakov
4 | //
5 | // Distributed under the Boost Software License, Version 1.0.
6 | // (See accompanying file LICENSE_1_0.txt or copy at
7 | // http://www.boost.org/LICENSE_1_0.txt)
8 | //
9 | // For more information, see www.boost.org
10 | // ----------------------------------------------------------------------------
11 | #ifndef BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_WRITER_SETTINGS_HPP_INCLUDED
12 | #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_WRITER_SETTINGS_HPP_INCLUDED
13 |
14 | #include
15 |
16 | namespace boost { namespace property_tree { namespace info_parser
17 | {
18 |
19 | template
20 | class info_writer_settings
21 | {
22 | public:
23 | info_writer_settings(Ch indent_char = Ch(' '), unsigned indent_count = 4):
24 | indent_char(indent_char),
25 | indent_count(indent_count)
26 | {
27 | }
28 | Ch indent_char;
29 | int indent_count;
30 | };
31 |
32 | template
33 | info_writer_settings info_writer_make_settings(Ch indent_char = Ch(' '), unsigned indent_count = 4)
34 | {
35 | return info_writer_settings(indent_char, indent_count);
36 | }
37 |
38 | } } }
39 |
40 | #endif
41 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/ptree_utils.hpp:
--------------------------------------------------------------------------------
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_PTREE_UTILS_HPP_INCLUDED
11 | #define BOOST_PROPERTY_TREE_DETAIL_PTREE_UTILS_HPP_INCLUDED
12 |
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 |
21 | namespace boost { namespace property_tree { namespace detail
22 | {
23 |
24 | template
25 | struct less_nocase
26 | {
27 | typedef typename T::value_type Ch;
28 | std::locale m_locale;
29 | inline bool operator()(Ch c1, Ch c2) const
30 | {
31 | return std::toupper(c1, m_locale) < std::toupper(c2, m_locale);
32 | }
33 | inline bool operator()(const T &t1, const T &t2) const
34 | {
35 | return std::lexicographical_compare(t1.begin(), t1.end(),
36 | t2.begin(), t2.end(), *this);
37 | }
38 | };
39 |
40 | template
41 | struct is_character : public boost::false_type {};
42 | template <>
43 | struct is_character : public boost::true_type {};
44 | template <>
45 | struct is_character : public boost::true_type {};
46 |
47 |
48 | BOOST_MPL_HAS_XXX_TRAIT_DEF(internal_type)
49 | BOOST_MPL_HAS_XXX_TRAIT_DEF(external_type)
50 | template
51 | struct is_translator : public boost::mpl::and_<
52 | has_internal_type, has_external_type > {};
53 |
54 |
55 |
56 | // Naively convert narrow string to another character type
57 | template
58 | Str widen(const char *text)
59 | {
60 | Str result;
61 | while (*text)
62 | {
63 | result += typename Str::value_type(*text);
64 | ++text;
65 | }
66 | return result;
67 | }
68 |
69 | // Naively convert string to narrow character type
70 | template
71 | Str narrow(const char_type *text)
72 | {
73 | Str result;
74 | while (*text)
75 | {
76 | if (*text < 0 || *text > static_cast((std::numeric_limits::max)()))
77 | result += '*';
78 | else
79 | result += typename Str::value_type(*text);
80 | ++text;
81 | }
82 | return result;
83 | }
84 |
85 | // Remove trailing and leading spaces
86 | template
87 | Str trim(const Str &s, const std::locale &loc = std::locale())
88 | {
89 | typename Str::const_iterator first = s.begin();
90 | typename Str::const_iterator end = s.end();
91 | while (first != end && std::isspace(*first, loc))
92 | ++first;
93 | if (first == end)
94 | return Str();
95 | typename Str::const_iterator last = end;
96 | do --last; while (std::isspace(*last, loc));
97 | if (first != s.begin() || last + 1 != end)
98 | return Str(first, last + 1);
99 | else
100 | return s;
101 | }
102 |
103 | } } }
104 |
105 | #endif
106 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/xml_parser_error.hpp:
--------------------------------------------------------------------------------
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_XML_PARSER_ERROR_HPP_INCLUDED
11 | #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_ERROR_HPP_INCLUDED
12 |
13 | #include
14 | #include
15 |
16 | namespace boost { namespace property_tree { namespace xml_parser
17 | {
18 |
19 | //! Xml parser error
20 | class xml_parser_error: public file_parser_error
21 | {
22 | public:
23 | xml_parser_error(const std::string &msg,
24 | const std::string &file,
25 | unsigned long l):
26 | file_parser_error(msg, file, l)
27 | {
28 | }
29 | };
30 |
31 | } } }
32 |
33 | #endif
34 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/xml_parser_flags.hpp:
--------------------------------------------------------------------------------
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_XML_PARSER_FLAGS_HPP_INCLUDED
11 | #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_FLAGS_HPP_INCLUDED
12 |
13 | namespace boost { namespace property_tree { namespace xml_parser
14 | {
15 |
16 | /// Text elements should be put in separate keys,
17 | /// not concatenated in parent data.
18 | static const int no_concat_text = 0x1;
19 | /// Comments should be omitted.
20 | static const int no_comments = 0x2;
21 | /// Whitespace should be collapsed and trimmed.
22 | static const int trim_whitespace = 0x4;
23 |
24 | inline bool validate_flags(int flags)
25 | {
26 | return (flags & ~(no_concat_text | no_comments | trim_whitespace)) == 0;
27 | }
28 |
29 | } } }
30 |
31 | #endif
32 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/xml_parser_read_rapidxml.hpp:
--------------------------------------------------------------------------------
1 | // ----------------------------------------------------------------------------
2 | // Copyright (C) 2007 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_XML_PARSER_READ_RAPIDXML_HPP_INCLUDED
11 | #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_READ_RAPIDXML_HPP_INCLUDED
12 |
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 |
20 | namespace boost { namespace property_tree { namespace xml_parser
21 | {
22 |
23 | template
24 | void read_xml_node(detail::rapidxml::xml_node *node,
25 | Ptree &pt, int flags)
26 | {
27 | using namespace detail::rapidxml;
28 | switch (node->type())
29 | {
30 | // Element nodes
31 | case node_element:
32 | {
33 | // Create node
34 | Ptree &pt_node = pt.push_back(std::make_pair(node->name(),
35 | Ptree()))->second;
36 |
37 | // Copy attributes
38 | if (node->first_attribute())
39 | {
40 | Ptree &pt_attr_root = pt_node.push_back(
41 | std::make_pair(xmlattr(), Ptree()))->second;
42 | for (xml_attribute *attr = node->first_attribute();
43 | attr; attr = attr->next_attribute())
44 | {
45 | Ptree &pt_attr = pt_attr_root.push_back(
46 | std::make_pair(attr->name(), Ptree()))->second;
47 | pt_attr.data() = typename Ptree::key_type(attr->value(), attr->value_size());
48 | }
49 | }
50 |
51 | // Copy children
52 | for (xml_node *child = node->first_node();
53 | child; child = child->next_sibling())
54 | read_xml_node(child, pt_node, flags);
55 | }
56 | break;
57 |
58 | // Data nodes
59 | case node_data:
60 | case node_cdata:
61 | {
62 | if (flags & no_concat_text)
63 | pt.push_back(std::make_pair(xmltext(),
64 | Ptree(node->value())));
65 | else
66 | pt.data() += typename Ptree::key_type(node->value(), node->value_size());
67 | }
68 | break;
69 |
70 | // Comment nodes
71 | case node_comment:
72 | {
73 | if (!(flags & no_comments))
74 | pt.push_back(std::make_pair(xmlcomment(),
75 | Ptree(typename Ptree::key_type(node->value(), node->value_size()))));
76 | }
77 | break;
78 |
79 | default:
80 | // Skip other node types
81 | break;
82 | }
83 | }
84 |
85 | template
86 | void read_xml_internal(std::basic_istream<
87 | typename Ptree::key_type::value_type> &stream,
88 | Ptree &pt,
89 | int flags,
90 | const std::string &filename)
91 | {
92 | typedef typename Ptree::key_type::value_type Ch;
93 | using namespace detail::rapidxml;
94 |
95 | // Load data into vector
96 | stream.unsetf(std::ios::skipws);
97 | std::vector v(std::istreambuf_iterator(stream.rdbuf()),
98 | std::istreambuf_iterator());
99 | if (!stream.good())
100 | BOOST_PROPERTY_TREE_THROW(
101 | xml_parser_error("read error", filename, 0));
102 | v.push_back(0); // zero-terminate
103 |
104 | try {
105 | // Parse using appropriate flags
106 | const int f_tws = parse_normalize_whitespace
107 | | parse_trim_whitespace;
108 | const int f_c = parse_comment_nodes;
109 | // Some compilers don't like the bitwise or in the template arg.
110 | const int f_tws_c = parse_normalize_whitespace
111 | | parse_trim_whitespace
112 | | parse_comment_nodes;
113 | xml_document doc;
114 | if (flags & no_comments) {
115 | if (flags & trim_whitespace)
116 | doc.BOOST_NESTED_TEMPLATE parse(&v.front());
117 | else
118 | doc.BOOST_NESTED_TEMPLATE parse<0>(&v.front());
119 | } else {
120 | if (flags & trim_whitespace)
121 | doc.BOOST_NESTED_TEMPLATE parse(&v.front());
122 | else
123 | doc.BOOST_NESTED_TEMPLATE parse(&v.front());
124 | }
125 |
126 | // Create ptree from nodes
127 | Ptree local;
128 | for (xml_node *child = doc.first_node();
129 | child; child = child->next_sibling())
130 | read_xml_node(child, local, flags);
131 |
132 | // Swap local and result ptrees
133 | pt.swap(local);
134 | } catch (parse_error &e) {
135 | long line = static_cast(
136 | std::count(&v.front(), e.where(), Ch('\n')) + 1);
137 | BOOST_PROPERTY_TREE_THROW(
138 | xml_parser_error(e.what(), filename, line));
139 | }
140 | }
141 |
142 | } } }
143 |
144 | #endif
145 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/xml_parser_utils.hpp:
--------------------------------------------------------------------------------
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_XML_PARSER_UTILS_HPP_INCLUDED
11 | #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_UTILS_HPP_INCLUDED
12 |
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 |
20 | namespace boost { namespace property_tree { namespace xml_parser
21 | {
22 |
23 | template
24 | Str condense(const Str &s)
25 | {
26 | typedef typename Str::value_type Ch;
27 | Str r;
28 | std::locale loc;
29 | bool space = false;
30 | typename Str::const_iterator end = s.end();
31 | for (typename Str::const_iterator it = s.begin();
32 | it != end; ++it)
33 | {
34 | if (isspace(*it, loc) || *it == Ch('\n'))
35 | {
36 | if (!space)
37 | r += Ch(' '), space = true;
38 | }
39 | else
40 | r += *it, space = false;
41 | }
42 | return r;
43 | }
44 |
45 |
46 | template
47 | Str encode_char_entities(const Str &s)
48 | {
49 | // Don't do anything for empty strings.
50 | if(s.empty()) return s;
51 |
52 | typedef typename Str::value_type Ch;
53 |
54 | Str r;
55 | // To properly round-trip spaces and not uglify the XML beyond
56 | // recognition, we have to encode them IF the text contains only spaces.
57 | Str sp(1, Ch(' '));
58 | if(s.find_first_not_of(sp) == Str::npos) {
59 | // The first will suffice.
60 | r = detail::widen(" ");
61 | r += Str(s.size() - 1, Ch(' '));
62 | } else {
63 | typename Str::const_iterator end = s.end();
64 | for (typename Str::const_iterator it = s.begin(); it != end; ++it)
65 | {
66 | switch (*it)
67 | {
68 | case Ch('<'): r += detail::widen("<"); break;
69 | case Ch('>'): r += detail::widen(">"); break;
70 | case Ch('&'): r += detail::widen("&"); break;
71 | case Ch('"'): r += detail::widen("""); break;
72 | case Ch('\''): r += detail::widen("'"); break;
73 | default: r += *it; break;
74 | }
75 | }
76 | }
77 | return r;
78 | }
79 |
80 | template
81 | Str decode_char_entities(const Str &s)
82 | {
83 | typedef typename Str::value_type Ch;
84 | Str r;
85 | typename Str::const_iterator end = s.end();
86 | for (typename Str::const_iterator it = s.begin(); it != end; ++it)
87 | {
88 | if (*it == Ch('&'))
89 | {
90 | typename Str::const_iterator semicolon = std::find(it + 1, end, Ch(';'));
91 | if (semicolon == end)
92 | BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
93 | Str ent(it + 1, semicolon);
94 | if (ent == detail::widen("lt")) r += Ch('<');
95 | else if (ent == detail::widen("gt")) r += Ch('>');
96 | else if (ent == detail::widen("amp")) r += Ch('&');
97 | else if (ent == detail::widen("quot")) r += Ch('"');
98 | else if (ent == detail::widen("apos")) r += Ch('\'');
99 | else
100 | BOOST_PROPERTY_TREE_THROW(xml_parser_error("invalid character entity", "", 0));
101 | it = semicolon;
102 | }
103 | else
104 | r += *it;
105 | }
106 | return r;
107 | }
108 |
109 | template
110 | const Str &xmldecl()
111 | {
112 | static Str s = detail::widen("");
113 | return s;
114 | }
115 |
116 | template
117 | const Str &xmlattr()
118 | {
119 | static Str s = detail::widen("");
120 | return s;
121 | }
122 |
123 | template
124 | const Str &xmlcomment()
125 | {
126 | static Str s = detail::widen("");
127 | return s;
128 | }
129 |
130 | template
131 | const Str &xmltext()
132 | {
133 | static Str s = detail::widen("");
134 | return s;
135 | }
136 |
137 | } } }
138 |
139 | #endif
140 |
--------------------------------------------------------------------------------
/include/boost/property_tree/detail/xml_parser_writer_settings.hpp:
--------------------------------------------------------------------------------
1 | // ----------------------------------------------------------------------------
2 | // Copyright (C) 2002-2007 Marcin Kalicinski
3 | // Copyright (C) 2007 Alexey Baskakov
4 | //
5 | // Distributed under the Boost Software License, Version 1.0.
6 | // (See accompanying file LICENSE_1_0.txt or copy at
7 | // http://www.boost.org/LICENSE_1_0.txt)
8 | //
9 | // For more information, see www.boost.org
10 | // ----------------------------------------------------------------------------
11 | #ifndef BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_WRITER_SETTINGS_HPP_INCLUDED
12 | #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_WRITER_SETTINGS_HPP_INCLUDED
13 |
14 | #include