Chris@16
|
1 // ----------------------------------------------------------------------------
|
Chris@16
|
2 // Copyright (C) 2002-2006 Marcin Kalicinski
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
5 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 // For more information, see www.boost.org
|
Chris@16
|
9 // ----------------------------------------------------------------------------
|
Chris@16
|
10 #ifndef BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_READ_HPP_INCLUDED
|
Chris@16
|
11 #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_READ_HPP_INCLUDED
|
Chris@16
|
12
|
Chris@16
|
13 #include "boost/property_tree/ptree.hpp"
|
Chris@16
|
14 #include "boost/property_tree/detail/info_parser_error.hpp"
|
Chris@16
|
15 #include "boost/property_tree/detail/info_parser_utils.hpp"
|
Chris@16
|
16 #include <iterator>
|
Chris@16
|
17 #include <string>
|
Chris@16
|
18 #include <stack>
|
Chris@16
|
19 #include <fstream>
|
Chris@16
|
20 #include <cctype>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost { namespace property_tree { namespace info_parser
|
Chris@16
|
23 {
|
Chris@16
|
24
|
Chris@16
|
25 // Expand known escape sequences
|
Chris@16
|
26 template<class It>
|
Chris@16
|
27 std::basic_string<typename std::iterator_traits<It>::value_type>
|
Chris@16
|
28 expand_escapes(It b, It e)
|
Chris@16
|
29 {
|
Chris@16
|
30 typedef typename std::iterator_traits<It>::value_type Ch;
|
Chris@16
|
31 std::basic_string<Ch> result;
|
Chris@16
|
32 while (b != e)
|
Chris@16
|
33 {
|
Chris@16
|
34 if (*b == Ch('\\'))
|
Chris@16
|
35 {
|
Chris@16
|
36 ++b;
|
Chris@16
|
37 if (b == e)
|
Chris@16
|
38 {
|
Chris@16
|
39 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
40 "character expected after backslash", "", 0));
|
Chris@16
|
41 }
|
Chris@16
|
42 else if (*b == Ch('0')) result += Ch('\0');
|
Chris@16
|
43 else if (*b == Ch('a')) result += Ch('\a');
|
Chris@16
|
44 else if (*b == Ch('b')) result += Ch('\b');
|
Chris@16
|
45 else if (*b == Ch('f')) result += Ch('\f');
|
Chris@16
|
46 else if (*b == Ch('n')) result += Ch('\n');
|
Chris@16
|
47 else if (*b == Ch('r')) result += Ch('\r');
|
Chris@16
|
48 else if (*b == Ch('t')) result += Ch('\t');
|
Chris@16
|
49 else if (*b == Ch('v')) result += Ch('\v');
|
Chris@16
|
50 else if (*b == Ch('"')) result += Ch('"');
|
Chris@16
|
51 else if (*b == Ch('\'')) result += Ch('\'');
|
Chris@16
|
52 else if (*b == Ch('\\')) result += Ch('\\');
|
Chris@16
|
53 else
|
Chris@16
|
54 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
55 "unknown escape sequence", "", 0));
|
Chris@16
|
56 }
|
Chris@16
|
57 else
|
Chris@16
|
58 result += *b;
|
Chris@16
|
59 ++b;
|
Chris@16
|
60 }
|
Chris@16
|
61 return result;
|
Chris@16
|
62 }
|
Chris@101
|
63
|
Chris@101
|
64 // Detect whitespace in a not very smart way.
|
Chris@101
|
65 template <class Ch>
|
Chris@101
|
66 bool is_ascii_space(Ch c)
|
Chris@101
|
67 {
|
Chris@101
|
68 // Everything outside ASCII is not space.
|
Chris@101
|
69 unsigned n = c;
|
Chris@101
|
70 if (n > 127)
|
Chris@101
|
71 return false;
|
Chris@101
|
72 return isspace(c) != 0;
|
Chris@101
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 // Advance pointer past whitespace
|
Chris@16
|
76 template<class Ch>
|
Chris@16
|
77 void skip_whitespace(const Ch *&text)
|
Chris@16
|
78 {
|
Chris@16
|
79 using namespace std;
|
Chris@101
|
80 while (is_ascii_space(*text))
|
Chris@16
|
81 ++text;
|
Chris@16
|
82 }
|
Chris@16
|
83
|
Chris@16
|
84 // Extract word (whitespace delimited) and advance pointer accordingly
|
Chris@16
|
85 template<class Ch>
|
Chris@16
|
86 std::basic_string<Ch> read_word(const Ch *&text)
|
Chris@16
|
87 {
|
Chris@16
|
88 using namespace std;
|
Chris@16
|
89 skip_whitespace(text);
|
Chris@16
|
90 const Ch *start = text;
|
Chris@101
|
91 while (!is_ascii_space(*text) && *text != Ch(';') && *text != Ch('\0'))
|
Chris@16
|
92 ++text;
|
Chris@16
|
93 return expand_escapes(start, text);
|
Chris@16
|
94 }
|
Chris@16
|
95
|
Chris@16
|
96 // Extract line (eol delimited) and advance pointer accordingly
|
Chris@16
|
97 template<class Ch>
|
Chris@16
|
98 std::basic_string<Ch> read_line(const Ch *&text)
|
Chris@16
|
99 {
|
Chris@16
|
100 using namespace std;
|
Chris@16
|
101 skip_whitespace(text);
|
Chris@16
|
102 const Ch *start = text;
|
Chris@16
|
103 while (*text != Ch('\0') && *text != Ch(';'))
|
Chris@16
|
104 ++text;
|
Chris@101
|
105 while (text > start && is_ascii_space(*(text - 1)))
|
Chris@16
|
106 --text;
|
Chris@16
|
107 return expand_escapes(start, text);
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 // Extract string (inside ""), and advance pointer accordingly
|
Chris@16
|
111 // Set need_more_lines to true if \ continuator found
|
Chris@16
|
112 template<class Ch>
|
Chris@16
|
113 std::basic_string<Ch> read_string(const Ch *&text, bool *need_more_lines)
|
Chris@16
|
114 {
|
Chris@16
|
115 skip_whitespace(text);
|
Chris@16
|
116 if (*text == Ch('\"'))
|
Chris@16
|
117 {
|
Chris@16
|
118
|
Chris@16
|
119 // Skip "
|
Chris@16
|
120 ++text;
|
Chris@16
|
121
|
Chris@16
|
122 // Find end of string, but skip escaped "
|
Chris@16
|
123 bool escaped = false;
|
Chris@16
|
124 const Ch *start = text;
|
Chris@16
|
125 while ((escaped || *text != Ch('\"')) && *text != Ch('\0'))
|
Chris@16
|
126 {
|
Chris@16
|
127 escaped = (!escaped && *text == Ch('\\'));
|
Chris@16
|
128 ++text;
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 // If end of string found
|
Chris@16
|
132 if (*text == Ch('\"'))
|
Chris@16
|
133 {
|
Chris@16
|
134 std::basic_string<Ch> result = expand_escapes(start, text++);
|
Chris@16
|
135 skip_whitespace(text);
|
Chris@16
|
136 if (*text == Ch('\\'))
|
Chris@16
|
137 {
|
Chris@16
|
138 if (!need_more_lines)
|
Chris@16
|
139 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
140 "unexpected \\", "", 0));
|
Chris@16
|
141 ++text;
|
Chris@16
|
142 skip_whitespace(text);
|
Chris@16
|
143 if (*text == Ch('\0') || *text == Ch(';'))
|
Chris@16
|
144 *need_more_lines = true;
|
Chris@16
|
145 else
|
Chris@16
|
146 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
147 "expected end of line after \\", "", 0));
|
Chris@16
|
148 }
|
Chris@16
|
149 else
|
Chris@16
|
150 if (need_more_lines)
|
Chris@16
|
151 *need_more_lines = false;
|
Chris@16
|
152 return result;
|
Chris@16
|
153 }
|
Chris@16
|
154 else
|
Chris@16
|
155 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
156 "unexpected end of line", "", 0));
|
Chris@16
|
157
|
Chris@16
|
158 }
|
Chris@16
|
159 else
|
Chris@16
|
160 BOOST_PROPERTY_TREE_THROW(info_parser_error("expected \"", "", 0));
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 // Extract key
|
Chris@16
|
164 template<class Ch>
|
Chris@16
|
165 std::basic_string<Ch> read_key(const Ch *&text)
|
Chris@16
|
166 {
|
Chris@16
|
167 skip_whitespace(text);
|
Chris@16
|
168 if (*text == Ch('\"'))
|
Chris@16
|
169 return read_string(text, NULL);
|
Chris@16
|
170 else
|
Chris@16
|
171 return read_word(text);
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 // Extract data
|
Chris@16
|
175 template<class Ch>
|
Chris@16
|
176 std::basic_string<Ch> read_data(const Ch *&text, bool *need_more_lines)
|
Chris@16
|
177 {
|
Chris@16
|
178 skip_whitespace(text);
|
Chris@16
|
179 if (*text == Ch('\"'))
|
Chris@16
|
180 return read_string(text, need_more_lines);
|
Chris@16
|
181 else
|
Chris@16
|
182 {
|
Chris@16
|
183 *need_more_lines = false;
|
Chris@16
|
184 return read_word(text);
|
Chris@16
|
185 }
|
Chris@16
|
186 }
|
Chris@16
|
187
|
Chris@16
|
188 // Build ptree from info stream
|
Chris@16
|
189 template<class Ptree, class Ch>
|
Chris@16
|
190 void read_info_internal(std::basic_istream<Ch> &stream,
|
Chris@16
|
191 Ptree &pt,
|
Chris@16
|
192 const std::string &filename,
|
Chris@16
|
193 int include_depth)
|
Chris@16
|
194 {
|
Chris@16
|
195 typedef std::basic_string<Ch> str_t;
|
Chris@16
|
196 // Possible parser states
|
Chris@16
|
197 enum state_t {
|
Chris@16
|
198 s_key, // Parser expects key
|
Chris@16
|
199 s_data, // Parser expects data
|
Chris@16
|
200 s_data_cont // Parser expects data continuation
|
Chris@16
|
201 };
|
Chris@16
|
202
|
Chris@16
|
203 unsigned long line_no = 0;
|
Chris@16
|
204 state_t state = s_key; // Parser state
|
Chris@16
|
205 Ptree *last = NULL; // Pointer to last created ptree
|
Chris@16
|
206 // Define line here to minimize reallocations
|
Chris@16
|
207 str_t line;
|
Chris@16
|
208
|
Chris@16
|
209 // Initialize ptree stack (used to handle nesting)
|
Chris@16
|
210 std::stack<Ptree *> stack;
|
Chris@16
|
211 stack.push(&pt); // Push root ptree on stack initially
|
Chris@16
|
212
|
Chris@16
|
213 try {
|
Chris@16
|
214 // While there are characters in the stream
|
Chris@16
|
215 while (stream.good()) {
|
Chris@16
|
216 // Read one line from stream
|
Chris@16
|
217 ++line_no;
|
Chris@16
|
218 std::getline(stream, line);
|
Chris@16
|
219 if (!stream.good() && !stream.eof())
|
Chris@16
|
220 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
221 "read error", filename, line_no));
|
Chris@16
|
222 const Ch *text = line.c_str();
|
Chris@16
|
223
|
Chris@16
|
224 // If directive found
|
Chris@16
|
225 skip_whitespace(text);
|
Chris@16
|
226 if (*text == Ch('#')) {
|
Chris@16
|
227 // Determine directive type
|
Chris@16
|
228 ++text; // skip #
|
Chris@16
|
229 std::basic_string<Ch> directive = read_word(text);
|
Chris@16
|
230 if (directive == convert_chtype<Ch, char>("include")) {
|
Chris@16
|
231 // #include
|
Chris@16
|
232 if (include_depth > 100) {
|
Chris@16
|
233 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
234 "include depth too large, "
|
Chris@16
|
235 "probably recursive include",
|
Chris@16
|
236 filename, line_no));
|
Chris@16
|
237 }
|
Chris@16
|
238 str_t s = read_string(text, NULL);
|
Chris@16
|
239 std::string inc_name =
|
Chris@16
|
240 convert_chtype<char, Ch>(s.c_str());
|
Chris@16
|
241 std::basic_ifstream<Ch> inc_stream(inc_name.c_str());
|
Chris@16
|
242 if (!inc_stream.good())
|
Chris@16
|
243 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
244 "cannot open include file " + inc_name,
|
Chris@16
|
245 filename, line_no));
|
Chris@16
|
246 read_info_internal(inc_stream, *stack.top(),
|
Chris@16
|
247 inc_name, include_depth + 1);
|
Chris@16
|
248 } else { // Unknown directive
|
Chris@16
|
249 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
250 "unknown directive", filename, line_no));
|
Chris@16
|
251 }
|
Chris@16
|
252
|
Chris@16
|
253 // Directive must be followed by end of line
|
Chris@16
|
254 skip_whitespace(text);
|
Chris@16
|
255 if (*text != Ch('\0')) {
|
Chris@16
|
256 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
257 "expected end of line", filename, line_no));
|
Chris@16
|
258 }
|
Chris@16
|
259
|
Chris@16
|
260 // Go to next line
|
Chris@16
|
261 continue;
|
Chris@16
|
262 }
|
Chris@16
|
263
|
Chris@16
|
264 // While there are characters left in line
|
Chris@16
|
265 while (1) {
|
Chris@16
|
266
|
Chris@16
|
267 // Stop parsing on end of line or comment
|
Chris@16
|
268 skip_whitespace(text);
|
Chris@16
|
269 if (*text == Ch('\0') || *text == Ch(';')) {
|
Chris@16
|
270 if (state == s_data) // If there was no data set state to s_key
|
Chris@16
|
271 state = s_key;
|
Chris@16
|
272 break;
|
Chris@16
|
273 }
|
Chris@16
|
274
|
Chris@16
|
275 // Process according to current parser state
|
Chris@16
|
276 switch (state)
|
Chris@16
|
277 {
|
Chris@16
|
278
|
Chris@16
|
279 // Parser expects key
|
Chris@16
|
280 case s_key:
|
Chris@16
|
281 {
|
Chris@16
|
282
|
Chris@16
|
283 if (*text == Ch('{')) // Brace opening found
|
Chris@16
|
284 {
|
Chris@16
|
285 if (!last)
|
Chris@16
|
286 BOOST_PROPERTY_TREE_THROW(info_parser_error("unexpected {", "", 0));
|
Chris@16
|
287 stack.push(last);
|
Chris@16
|
288 last = NULL;
|
Chris@16
|
289 ++text;
|
Chris@16
|
290 }
|
Chris@16
|
291 else if (*text == Ch('}')) // Brace closing found
|
Chris@16
|
292 {
|
Chris@16
|
293 if (stack.size() <= 1)
|
Chris@16
|
294 BOOST_PROPERTY_TREE_THROW(info_parser_error("unmatched }", "", 0));
|
Chris@16
|
295 stack.pop();
|
Chris@16
|
296 last = NULL;
|
Chris@16
|
297 ++text;
|
Chris@16
|
298 }
|
Chris@16
|
299 else // Key text found
|
Chris@16
|
300 {
|
Chris@16
|
301 std::basic_string<Ch> key = read_key(text);
|
Chris@16
|
302 last = &stack.top()->push_back(
|
Chris@16
|
303 std::make_pair(key, Ptree()))->second;
|
Chris@16
|
304 state = s_data;
|
Chris@16
|
305 }
|
Chris@16
|
306
|
Chris@16
|
307 }; break;
|
Chris@16
|
308
|
Chris@16
|
309 // Parser expects data
|
Chris@16
|
310 case s_data:
|
Chris@16
|
311 {
|
Chris@16
|
312
|
Chris@16
|
313 // Last ptree must be defined because we are going to add data to it
|
Chris@16
|
314 BOOST_ASSERT(last);
|
Chris@16
|
315
|
Chris@16
|
316 if (*text == Ch('{')) // Brace opening found
|
Chris@16
|
317 {
|
Chris@16
|
318 stack.push(last);
|
Chris@16
|
319 last = NULL;
|
Chris@16
|
320 ++text;
|
Chris@16
|
321 state = s_key;
|
Chris@16
|
322 }
|
Chris@16
|
323 else if (*text == Ch('}')) // Brace closing found
|
Chris@16
|
324 {
|
Chris@16
|
325 if (stack.size() <= 1)
|
Chris@16
|
326 BOOST_PROPERTY_TREE_THROW(info_parser_error("unmatched }", "", 0));
|
Chris@16
|
327 stack.pop();
|
Chris@16
|
328 last = NULL;
|
Chris@16
|
329 ++text;
|
Chris@16
|
330 state = s_key;
|
Chris@16
|
331 }
|
Chris@16
|
332 else // Data text found
|
Chris@16
|
333 {
|
Chris@16
|
334 bool need_more_lines;
|
Chris@16
|
335 std::basic_string<Ch> data = read_data(text, &need_more_lines);
|
Chris@16
|
336 last->data() = data;
|
Chris@16
|
337 state = need_more_lines ? s_data_cont : s_key;
|
Chris@16
|
338 }
|
Chris@16
|
339
|
Chris@16
|
340
|
Chris@16
|
341 }; break;
|
Chris@16
|
342
|
Chris@16
|
343 // Parser expects continuation of data after \ on previous line
|
Chris@16
|
344 case s_data_cont:
|
Chris@16
|
345 {
|
Chris@16
|
346
|
Chris@16
|
347 // Last ptree must be defined because we are going to update its data
|
Chris@16
|
348 BOOST_ASSERT(last);
|
Chris@16
|
349
|
Chris@16
|
350 if (*text == Ch('\"')) // Continuation must start with "
|
Chris@16
|
351 {
|
Chris@16
|
352 bool need_more_lines;
|
Chris@16
|
353 std::basic_string<Ch> data = read_string(text, &need_more_lines);
|
Chris@16
|
354 last->put_value(last->template get_value<std::basic_string<Ch> >() + data);
|
Chris@16
|
355 state = need_more_lines ? s_data_cont : s_key;
|
Chris@16
|
356 }
|
Chris@16
|
357 else
|
Chris@16
|
358 BOOST_PROPERTY_TREE_THROW(info_parser_error("expected \" after \\ in previous line", "", 0));
|
Chris@16
|
359
|
Chris@16
|
360 }; break;
|
Chris@16
|
361
|
Chris@16
|
362 // Should never happen
|
Chris@16
|
363 default:
|
Chris@16
|
364 BOOST_ASSERT(0);
|
Chris@16
|
365
|
Chris@16
|
366 }
|
Chris@16
|
367 }
|
Chris@16
|
368 }
|
Chris@16
|
369
|
Chris@16
|
370 // Check if stack has initial size, otherwise some {'s have not been closed
|
Chris@16
|
371 if (stack.size() != 1)
|
Chris@16
|
372 BOOST_PROPERTY_TREE_THROW(info_parser_error("unmatched {", "", 0));
|
Chris@16
|
373
|
Chris@16
|
374 }
|
Chris@16
|
375 catch (info_parser_error &e)
|
Chris@16
|
376 {
|
Chris@16
|
377 // If line undefined rethrow error with correct filename and line
|
Chris@16
|
378 if (e.line() == 0)
|
Chris@16
|
379 {
|
Chris@16
|
380 BOOST_PROPERTY_TREE_THROW(info_parser_error(e.message(), filename, line_no));
|
Chris@16
|
381 }
|
Chris@16
|
382 else
|
Chris@16
|
383 BOOST_PROPERTY_TREE_THROW(e);
|
Chris@16
|
384
|
Chris@16
|
385 }
|
Chris@16
|
386
|
Chris@16
|
387 }
|
Chris@16
|
388
|
Chris@16
|
389 } } }
|
Chris@16
|
390
|
Chris@16
|
391 #endif
|