1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
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)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_READ_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_READ_HPP_INCLUDED
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_utils.hpp"
22 namespace boost { namespace property_tree { namespace info_parser
25 // Expand known escape sequences
27 std::basic_string<typename std::iterator_traits<It>::value_type>
28 expand_escapes(It b, It e)
30 typedef typename std::iterator_traits<It>::value_type Ch;
31 std::basic_string<Ch> result;
39 BOOST_PROPERTY_TREE_THROW(info_parser_error(
40 "character expected after backslash", "", 0));
42 else if (*b == Ch('0')) result += Ch('\0');
43 else if (*b == Ch('a')) result += Ch('\a');
44 else if (*b == Ch('b')) result += Ch('\b');
45 else if (*b == Ch('f')) result += Ch('\f');
46 else if (*b == Ch('n')) result += Ch('\n');
47 else if (*b == Ch('r')) result += Ch('\r');
48 else if (*b == Ch('t')) result += Ch('\t');
49 else if (*b == Ch('v')) result += Ch('\v');
50 else if (*b == Ch('"')) result += Ch('"');
51 else if (*b == Ch('\'')) result += Ch('\'');
52 else if (*b == Ch('\\')) result += Ch('\\');
54 BOOST_PROPERTY_TREE_THROW(info_parser_error(
55 "unknown escape sequence", "", 0));
64 // Detect whitespace in a not very smart way.
66 bool is_ascii_space(Ch c)
68 // Everything outside ASCII is not space.
72 return std::isspace(c) != 0;
75 // Advance pointer past whitespace
77 void skip_whitespace(const Ch *&text)
80 while (is_ascii_space(*text))
84 // Extract word (whitespace delimited) and advance pointer accordingly
86 std::basic_string<Ch> read_word(const Ch *&text)
89 skip_whitespace(text);
90 const Ch *start = text;
91 while (!is_ascii_space(*text) && *text != Ch(';') && *text != Ch('\0'))
93 return expand_escapes(start, text);
96 // Extract line (eol delimited) and advance pointer accordingly
98 std::basic_string<Ch> read_line(const Ch *&text)
101 skip_whitespace(text);
102 const Ch *start = text;
103 while (*text != Ch('\0') && *text != Ch(';'))
105 while (text > start && is_ascii_space(*(text - 1)))
107 return expand_escapes(start, text);
110 // Extract string (inside ""), and advance pointer accordingly
111 // Set need_more_lines to true if \ continuator found
113 std::basic_string<Ch> read_string(const Ch *&text, bool *need_more_lines)
115 skip_whitespace(text);
116 if (*text == Ch('\"'))
122 // Find end of string, but skip escaped "
123 bool escaped = false;
124 const Ch *start = text;
125 while ((escaped || *text != Ch('\"')) && *text != Ch('\0'))
127 escaped = (!escaped && *text == Ch('\\'));
131 // If end of string found
132 if (*text == Ch('\"'))
134 std::basic_string<Ch> result = expand_escapes(start, text++);
135 skip_whitespace(text);
136 if (*text == Ch('\\'))
138 if (!need_more_lines)
139 BOOST_PROPERTY_TREE_THROW(info_parser_error(
140 "unexpected \\", "", 0));
142 skip_whitespace(text);
143 if (*text == Ch('\0') || *text == Ch(';'))
144 *need_more_lines = true;
146 BOOST_PROPERTY_TREE_THROW(info_parser_error(
147 "expected end of line after \\", "", 0));
151 *need_more_lines = false;
155 BOOST_PROPERTY_TREE_THROW(info_parser_error(
156 "unexpected end of line", "", 0));
160 BOOST_PROPERTY_TREE_THROW(info_parser_error("expected \"", "", 0));
165 std::basic_string<Ch> read_key(const Ch *&text)
167 skip_whitespace(text);
168 if (*text == Ch('\"'))
169 return read_string(text, NULL);
171 return read_word(text);
176 std::basic_string<Ch> read_data(const Ch *&text, bool *need_more_lines)
178 skip_whitespace(text);
179 if (*text == Ch('\"'))
180 return read_string(text, need_more_lines);
183 *need_more_lines = false;
184 return read_word(text);
188 // Build ptree from info stream
189 template<class Ptree, class Ch>
190 void read_info_internal(std::basic_istream<Ch> &stream,
192 const std::string &filename,
195 typedef std::basic_string<Ch> str_t;
196 // Possible parser states
198 s_key, // Parser expects key
199 s_data, // Parser expects data
200 s_data_cont // Parser expects data continuation
203 unsigned long line_no = 0;
204 state_t state = s_key; // Parser state
205 Ptree *last = NULL; // Pointer to last created ptree
206 // Define line here to minimize reallocations
209 // Initialize ptree stack (used to handle nesting)
210 std::stack<Ptree *> stack;
211 stack.push(&pt); // Push root ptree on stack initially
214 // While there are characters in the stream
215 while (stream.good()) {
216 // Read one line from stream
218 std::getline(stream, line);
219 if (!stream.good() && !stream.eof())
220 BOOST_PROPERTY_TREE_THROW(info_parser_error(
221 "read error", filename, line_no));
222 const Ch *text = line.c_str();
224 // If directive found
225 skip_whitespace(text);
226 if (*text == Ch('#')) {
227 // Determine directive type
229 std::basic_string<Ch> directive = read_word(text);
230 if (directive == convert_chtype<Ch, char>("include")) {
232 if (include_depth > 100) {
233 BOOST_PROPERTY_TREE_THROW(info_parser_error(
234 "include depth too large, "
235 "probably recursive include",
238 str_t s = read_string(text, NULL);
239 std::string inc_name =
240 convert_chtype<char, Ch>(s.c_str());
241 std::basic_ifstream<Ch> inc_stream(inc_name.c_str());
242 if (!inc_stream.good())
243 BOOST_PROPERTY_TREE_THROW(info_parser_error(
244 "cannot open include file " + inc_name,
246 read_info_internal(inc_stream, *stack.top(),
247 inc_name, include_depth + 1);
248 } else { // Unknown directive
249 BOOST_PROPERTY_TREE_THROW(info_parser_error(
250 "unknown directive", filename, line_no));
253 // Directive must be followed by end of line
254 skip_whitespace(text);
255 if (*text != Ch('\0')) {
256 BOOST_PROPERTY_TREE_THROW(info_parser_error(
257 "expected end of line", filename, line_no));
264 // While there are characters left in line
267 // Stop parsing on end of line or comment
268 skip_whitespace(text);
269 if (*text == Ch('\0') || *text == Ch(';')) {
270 if (state == s_data) // If there was no data set state to s_key
275 // Process according to current parser state
279 // Parser expects key
283 if (*text == Ch('{')) // Brace opening found
286 BOOST_PROPERTY_TREE_THROW(info_parser_error("unexpected {", "", 0));
291 else if (*text == Ch('}')) // Brace closing found
293 if (stack.size() <= 1)
294 BOOST_PROPERTY_TREE_THROW(info_parser_error("unmatched }", "", 0));
299 else // Key text found
301 std::basic_string<Ch> key = read_key(text);
302 last = &stack.top()->push_back(
303 std::make_pair(key, Ptree()))->second;
309 // Parser expects data
313 // Last ptree must be defined because we are going to add data to it
316 if (*text == Ch('{')) // Brace opening found
323 else if (*text == Ch('}')) // Brace closing found
325 if (stack.size() <= 1)
326 BOOST_PROPERTY_TREE_THROW(info_parser_error("unmatched }", "", 0));
332 else // Data text found
334 bool need_more_lines;
335 std::basic_string<Ch> data = read_data(text, &need_more_lines);
337 state = need_more_lines ? s_data_cont : s_key;
343 // Parser expects continuation of data after \ on previous line
347 // Last ptree must be defined because we are going to update its data
350 if (*text == Ch('\"')) // Continuation must start with "
352 bool need_more_lines;
353 std::basic_string<Ch> data = read_string(text, &need_more_lines);
354 last->put_value(last->template get_value<std::basic_string<Ch> >() + data);
355 state = need_more_lines ? s_data_cont : s_key;
358 BOOST_PROPERTY_TREE_THROW(info_parser_error("expected \" after \\ in previous line", "", 0));
362 // Should never happen
370 // Check if stack has initial size, otherwise some {'s have not been closed
371 if (stack.size() != 1)
372 BOOST_PROPERTY_TREE_THROW(info_parser_error("unmatched {", "", 0));
375 catch (info_parser_error &e)
377 // If line undefined rethrow error with correct filename and line
380 BOOST_PROPERTY_TREE_THROW(info_parser_error(e.message(), filename, line_no));
383 BOOST_PROPERTY_TREE_THROW(e);