1 #ifndef RAPIDXML_PRINT_HPP_INCLUDED 2 #define RAPIDXML_PRINT_HPP_INCLUDED 9 #include "rapidxml.hpp" 12 #ifndef RAPIDXML_NO_STREAMS 23 const int print_no_indenting = 0x1;
36 template<
class OutIt,
class Ch>
37 inline OutIt copy_chars(
const Ch *begin,
const Ch *end, OutIt out)
46 template<
class OutIt,
class Ch>
47 inline OutIt copy_and_expand_chars(
const Ch *begin,
const Ch *end, Ch noexpand, OutIt out)
51 if (*begin == noexpand)
60 *out++ = Ch(
'&'); *out++ = Ch(
'l'); *out++ = Ch(
't'); *out++ = Ch(
';');
63 *out++ = Ch(
'&'); *out++ = Ch(
'g'); *out++ = Ch(
't'); *out++ = Ch(
';');
66 *out++ = Ch(
'&'); *out++ = Ch(
'a'); *out++ = Ch(
'p'); *out++ = Ch(
'o'); *out++ = Ch(
's'); *out++ = Ch(
';');
69 *out++ = Ch(
'&'); *out++ = Ch(
'q'); *out++ = Ch(
'u'); *out++ = Ch(
'o'); *out++ = Ch(
't'); *out++ = Ch(
';');
72 *out++ = Ch(
'&'); *out++ = Ch(
'a'); *out++ = Ch(
'm'); *out++ = Ch(
'p'); *out++ = Ch(
';');
84 template<
class OutIt,
class Ch>
85 inline OutIt fill_chars(OutIt out,
int n, Ch ch)
87 for (
int i = 0; i < n; ++i)
93 template<
class Ch, Ch ch>
94 inline bool find_char(
const Ch *begin,
const Ch *end)
108 template<
class OutIt,
class Ch>
inline OutIt print_children(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
109 template<
class OutIt,
class Ch>
inline OutIt print_element_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
110 template<
class OutIt,
class Ch>
inline OutIt print_data_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
111 template<
class OutIt,
class Ch>
inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
112 template<
class OutIt,
class Ch>
inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
113 template<
class OutIt,
class Ch>
inline OutIt print_comment_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
114 template<
class OutIt,
class Ch>
inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
115 template<
class OutIt,
class Ch>
inline OutIt print_pi_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent);
120 template<
class OutIt,
class Ch>
121 inline OutIt print_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
124 switch (node->type())
129 out = print_children(out, node, flags, indent);
134 out = print_element_node(out, node, flags, indent);
139 out = print_data_node(out, node, flags, indent);
144 out = print_cdata_node(out, node, flags, indent);
148 case node_declaration:
149 out = print_declaration_node(out, node, flags, indent);
154 out = print_comment_node(out, node, flags, indent);
159 out = print_doctype_node(out, node, flags, indent);
164 out = print_pi_node(out, node, flags, indent);
174 if (!(flags & print_no_indenting))
175 *out = Ch(
'\n'), ++out;
182 template<
class OutIt,
class Ch>
183 inline OutIt print_children(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
185 for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling())
186 out = print_node(out, child, flags, indent);
191 template<
class OutIt,
class Ch>
192 inline OutIt print_attributes(OutIt out,
const xml_node<Ch> *node,
int )
194 for (xml_attribute<Ch> *attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
196 if (attribute->name() && attribute->value())
199 *out = Ch(
' '), ++out;
200 out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
201 *out = Ch(
'='), ++out;
203 if (find_char<Ch, Ch(
'"')>(attribute->value(), attribute->value() + attribute->value_size()))
205 *out = Ch(
'\''), ++out;
206 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'"'), out);
207 *out = Ch(
'\''), ++out;
211 *out = Ch(
'"'), ++out;
212 out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch(
'\''), out);
213 *out = Ch(
'"'), ++out;
221 template<
class OutIt,
class Ch>
222 inline OutIt print_data_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
224 assert(node->type() == node_data);
225 if (!(flags & print_no_indenting))
226 out = fill_chars(out, indent, Ch(
'\t'));
227 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
232 template<
class OutIt,
class Ch>
233 inline OutIt print_cdata_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
235 assert(node->type() == node_cdata);
236 if (!(flags & print_no_indenting))
237 out = fill_chars(out, indent, Ch(
'\t'));
238 *out = Ch(
'<'); ++out;
239 *out = Ch(
'!'); ++out;
240 *out = Ch(
'['); ++out;
241 *out = Ch(
'C'); ++out;
242 *out = Ch(
'D'); ++out;
243 *out = Ch(
'A'); ++out;
244 *out = Ch(
'T'); ++out;
245 *out = Ch(
'A'); ++out;
246 *out = Ch(
'['); ++out;
247 out = copy_chars(node->value(), node->value() + node->value_size(), out);
248 *out = Ch(
']'); ++out;
249 *out = Ch(
']'); ++out;
250 *out = Ch(
'>'); ++out;
255 template<
class OutIt,
class Ch>
256 inline OutIt print_element_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
258 assert(node->type() == node_element);
261 if (!(flags & print_no_indenting))
262 out = fill_chars(out, indent, Ch(
'\t'));
263 *out = Ch(
'<'), ++out;
264 out = copy_chars(node->name(), node->name() + node->name_size(), out);
265 out = print_attributes(out, node, flags);
268 if (node->value_size() == 0 && !node->first_node())
271 *out = Ch(
'/'), ++out;
272 *out = Ch(
'>'), ++out;
277 *out = Ch(
'>'), ++out;
280 xml_node<Ch> *child = node->first_node();
284 out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
286 else if (child->next_sibling() == 0 && child->type() == node_data)
289 out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
294 if (!(flags & print_no_indenting))
295 *out = Ch(
'\n'), ++out;
296 out = print_children(out, node, flags, indent + 1);
297 if (!(flags & print_no_indenting))
298 out = fill_chars(out, indent, Ch(
'\t'));
302 *out = Ch(
'<'), ++out;
303 *out = Ch(
'/'), ++out;
304 out = copy_chars(node->name(), node->name() + node->name_size(), out);
305 *out = Ch(
'>'), ++out;
311 template<
class OutIt,
class Ch>
312 inline OutIt print_declaration_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
315 if (!(flags & print_no_indenting))
316 out = fill_chars(out, indent, Ch(
'\t'));
317 *out = Ch(
'<'), ++out;
318 *out = Ch(
'?'), ++out;
319 *out = Ch(
'x'), ++out;
320 *out = Ch(
'm'), ++out;
321 *out = Ch(
'l'), ++out;
324 out = print_attributes(out, node, flags);
327 *out = Ch(
'?'), ++out;
328 *out = Ch(
'>'), ++out;
334 template<
class OutIt,
class Ch>
335 inline OutIt print_comment_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
337 assert(node->type() == node_comment);
338 if (!(flags & print_no_indenting))
339 out = fill_chars(out, indent, Ch(
'\t'));
340 *out = Ch(
'<'), ++out;
341 *out = Ch(
'!'), ++out;
342 *out = Ch(
'-'), ++out;
343 *out = Ch(
'-'), ++out;
344 out = copy_chars(node->value(), node->value() + node->value_size(), out);
345 *out = Ch(
'-'), ++out;
346 *out = Ch(
'-'), ++out;
347 *out = Ch(
'>'), ++out;
352 template<
class OutIt,
class Ch>
353 inline OutIt print_doctype_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
355 assert(node->type() == node_doctype);
356 if (!(flags & print_no_indenting))
357 out = fill_chars(out, indent, Ch(
'\t'));
358 *out = Ch(
'<'), ++out;
359 *out = Ch(
'!'), ++out;
360 *out = Ch(
'D'), ++out;
361 *out = Ch(
'O'), ++out;
362 *out = Ch(
'C'), ++out;
363 *out = Ch(
'T'), ++out;
364 *out = Ch(
'Y'), ++out;
365 *out = Ch(
'P'), ++out;
366 *out = Ch(
'E'), ++out;
367 *out = Ch(
' '), ++out;
368 out = copy_chars(node->value(), node->value() + node->value_size(), out);
369 *out = Ch(
'>'), ++out;
374 template<
class OutIt,
class Ch>
375 inline OutIt print_pi_node(OutIt out,
const xml_node<Ch> *node,
int flags,
int indent)
377 assert(node->type() == node_pi);
378 if (!(flags & print_no_indenting))
379 out = fill_chars(out, indent, Ch(
'\t'));
380 *out = Ch(
'<'), ++out;
381 *out = Ch(
'?'), ++out;
382 out = copy_chars(node->name(), node->name() + node->name_size(), out);
383 *out = Ch(
' '), ++out;
384 out = copy_chars(node->value(), node->value() + node->value_size(), out);
385 *out = Ch(
'?'), ++out;
386 *out = Ch(
'>'), ++out;
401 template<
class OutIt,
class Ch>
402 inline OutIt print(OutIt out,
const xml_node<Ch> &node,
int flags = 0)
404 return internal::print_node(out, &node, flags, 0);
407 #ifndef RAPIDXML_NO_STREAMS 415 inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out,
const xml_node<Ch> &node,
int flags = 0)
417 print(std::ostream_iterator<Ch>(out), node, flags);
426 inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out,
const xml_node<Ch> &node)
428 return print(out, node);
Definition: allocators.h:422
Definition: rapidxml.hpp:57