void read_xml_node( pugi::xml_node node, Ptree &pt, int flags) { typedef typename Ptree::key_type::value_type Ch; switch ( node.type() ) { case pugi::node_element: { Ptree &tmp = pt.push_back(std::make_pair( node.name(), Ptree()))->second; for ( pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute() ) tmp.put( xmlattr<Ch>() + "." + attr.name(), attr.value()); for ( pugi::xml_node child = node.first_child(); child; child = child.next_sibling()) read_xml_node(child, tmp, flags); } break; case pugi::node_pcdata: { if (flags & no_concat_text) pt.push_back(std::make_pair(xmltext<Ch>(), Ptree( node.value() ))); else pt.data() += node.value(); } break; case pugi::node_comment: { if (!(flags & no_comments)) pt.push_back(std::make_pair(xmlcomment<Ch>(), Ptree( node.value() ))); } break; default: // skip other types break; } }
void read_xml_node(detail::pdalboostrapidxml::xml_node<Ch> *node, Ptree &pt, int flags) { using namespace detail::pdalboostrapidxml; switch (node->type()) { // Element nodes case node_element: { // Create node Ptree &pt_node = pt.push_back(std::make_pair(node->name(), Ptree()))->second; // Copy attributes if (node->first_attribute()) { Ptree &pt_attr_root = pt_node.push_back( std::make_pair(xmlattr<typename Ptree::key_type>(), Ptree()))->second; for (xml_attribute<Ch> *attr = node->first_attribute(); attr; attr = attr->next_attribute()) { Ptree &pt_attr = pt_attr_root.push_back( std::make_pair(attr->name(), Ptree()))->second; pt_attr.data() = typename Ptree::key_type(attr->value(), attr->value_size()); } } // Copy children for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling()) read_xml_node(child, pt_node, flags); } break; // Data nodes case node_data: case node_cdata: { if (flags & no_concat_text) pt.push_back(std::make_pair(xmltext<typename Ptree::key_type>(), Ptree(node->value()))); else pt.data() += typename Ptree::key_type(node->value(), node->value_size()); } break; // Comment nodes case node_comment: { if (!(flags & no_comments)) pt.push_back(std::make_pair(xmlcomment<typename Ptree::key_type>(), Ptree(typename Ptree::key_type(node->value(), node->value_size())))); } break; default: // Skip other node types break; } }
typename Ptree::size_type total_data_size(const Ptree &pt) { typename Ptree::size_type size = pt.data().size(); for (typename Ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) size += total_data_size(it->second); return size; }
void write_info_helper(std::basic_ostream<typename Ptree::char_type> &stream, const Ptree &pt, int indent) { // Character type typedef typename Ptree::char_type Ch; // Write data if (indent >= 0) { if (!pt.data().empty()) { std::basic_string<Ch> data = create_escapes(pt.template get_own<std::basic_string<Ch> >()); if (is_simple_data(data)) stream << Ch(' ') << data << Ch('\n'); else stream << Ch(' ') << Ch('\"') << data << Ch('\"') << Ch('\n'); } else if (pt.empty()) stream << Ch(' ') << Ch('\"') << Ch('\"') << Ch('\n'); else stream << Ch('\n'); } // Write keys if (!pt.empty()) { // Open brace if (indent >= 0) stream << std::basic_string<Ch>(4 * indent, Ch(' ')) << Ch('{') << Ch('\n'); // Write keys typename Ptree::const_iterator it = pt.begin(); for (; it != pt.end(); ++it) { // Output key std::basic_string<Ch> key = create_escapes(it->first); stream << std::basic_string<Ch>(4 * (indent + 1), Ch(' ')); if (is_simple_key(key)) stream << key; else stream << Ch('\"') << key << Ch('\"'); // Output data and children write_info_helper(stream, it->second, indent + 1); } // Close brace if (indent >= 0) stream << std::basic_string<Ch>(4 * indent, Ch(' ')) << Ch('}') << Ch('\n'); } }
void write_ini(std::basic_ostream< typename Ptree::key_type::value_type > &stream, const Ptree &pt, int flags = 0) { using detail::check_dupes; typedef typename Ptree::key_type::value_type Ch; typedef std::basic_string<Ch> Str; BOOST_ASSERT(validate_flags(flags)); (void)flags; if (!pt.data().empty()) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "ptree has data on root", "", 0)); check_dupes(pt); for (typename Ptree::const_iterator it = pt.begin(), end = pt.end(); it != end; ++it) { check_dupes(it->second); if (it->second.empty()) { stream << it->first << Ch('=') << it->second.template get_value< std::basic_string<Ch> >() << Ch('\n'); } else { if (!it->second.data().empty()) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "mixed data and children", "", 0)); stream << Ch('[') << it->first << Ch(']') << Ch('\n'); for (typename Ptree::const_iterator it2 = it->second.begin(), end2 = it->second.end(); it2 != end2; ++it2) { if (!it2->second.empty()) BOOST_PROPERTY_TREE_THROW(ini_parser_error( "ptree is too deep", "", 0)); stream << it2->first << Ch('=') << it2->second.template get_value< std::basic_string<Ch> >() << Ch('\n'); } } } }
void write_xml_element(std::basic_ostream<typename Ptree::key_type::value_type> &stream, const std::basic_string<typename Ptree::key_type::value_type> &key, const Ptree &pt, int indent, const xml_writer_settings<typename Ptree::key_type::value_type> & settings) { typedef typename Ptree::key_type::value_type Ch; typedef typename Ptree::const_iterator It; bool want_pretty = settings.indent_count > 0; // Find if elements present bool has_elements = false; bool has_attrs_only = pt.data().empty(); for (It it = pt.begin(), end = pt.end(); it != end; ++it) { if (it->first != xmlattr<Ch>() ) { has_attrs_only = false; if (it->first != xmltext<Ch>()) { has_elements = true; break; } } } // Write element if (pt.data().empty() && pt.empty()) // Empty key { if (indent >= 0) { write_xml_indent(stream,indent,settings); stream << Ch('<') << key << Ch('/') << Ch('>'); if (want_pretty) stream << Ch('\n'); } } else // Nonempty key { // Write opening tag, attributes and data if (indent >= 0) { // Write opening brace and key write_xml_indent(stream,indent,settings); stream << Ch('<') << key; // Write attributes if (optional<const Ptree &> attribs = pt.get_child_optional(xmlattr<Ch>())) for (It it = attribs.get().begin(); it != attribs.get().end(); ++it) stream << Ch(' ') << it->first << Ch('=') << Ch('"') << encode_char_entities( it->second.template get_value<std::basic_string<Ch> >()) << Ch('"'); if ( has_attrs_only ) { // Write closing brace stream << Ch('/') << Ch('>'); if (want_pretty) stream << Ch('\n'); } else { // Write closing brace stream << Ch('>'); // Break line if needed and if we want pretty-printing if (has_elements && want_pretty) stream << Ch('\n'); } } // Write data text, if present if (!pt.data().empty()) write_xml_text(stream, pt.template get_value<std::basic_string<Ch> >(), indent + 1, has_elements && want_pretty, settings); // Write elements, comments and texts for (It it = pt.begin(); it != pt.end(); ++it) { if (it->first == xmlattr<Ch>()) continue; else if (it->first == xmlcomment<Ch>()) write_xml_comment(stream, it->second.template get_value<std::basic_string<Ch> >(), indent + 1, want_pretty, settings); else if (it->first == xmltext<Ch>()) write_xml_text(stream, it->second.template get_value<std::basic_string<Ch> >(), indent + 1, has_elements && want_pretty, settings); else write_xml_element(stream, it->first, it->second, indent + 1, settings); } // Write closing tag if (indent >= 0 && !has_attrs_only) { if (has_elements) write_xml_indent(stream,indent,settings); stream << Ch('<') << Ch('/') << key << Ch('>'); if (want_pretty) stream << Ch('\n'); } } }
void write_xml_element(std::basic_ostream<typename Ptree::char_type> &stream, const std::basic_string<typename Ptree::char_type> &key, const Ptree &pt, int indent) { typedef typename Ptree::char_type Ch; typedef typename std::basic_string<Ch> Str; typedef typename Ptree::const_iterator It; // Find if elements present bool has_elements = false; for (It it = pt.begin(), end = pt.end(); it != end; ++it) if (it->first != xmlattr<Ch>() && it->first != xmltext<Ch>()) { has_elements = true; break; } // Write element if (pt.data().empty() && pt.empty()) // Empty key { if (indent >= 0) stream << Str(4 * indent, Ch(' ')) << Ch('<') << key << Ch('/') << Ch('>') << std::endl; } else // Nonempty key { // Write opening tag, attributes and data if (indent >= 0) { // Write opening brace and key stream << Str(4 * indent, Ch(' ')); stream << Ch('<') << key; // Write attributes if (optional<const Ptree &> attribs = pt.get_child_optional(xmlattr<Ch>())) for (It it = attribs.get().begin(); it != attribs.get().end(); ++it) stream << Ch(' ') << it->first << Ch('=') << Ch('"') << it->second.template get_own<std::basic_string<Ch> >() << Ch('"'); // Write closing brace stream << Ch('>'); // Break line if needed if (has_elements) stream << Ch('\n'); } // Write data text, if present if (!pt.data().empty()) write_xml_text(stream, pt.template get_own<std::basic_string<Ch> >(), indent + 1, has_elements); // Write elements, comments and texts for (It it = pt.begin(); it != pt.end(); ++it) { if (it->first == xmlattr<Ch>()) continue; else if (it->first == xmlcomment<Ch>()) write_xml_comment(stream, it->second.template get_own<std::basic_string<Ch> >(), indent + 1); else if (it->first == xmltext<Ch>()) write_xml_text(stream, it->second.template get_own<std::basic_string<Ch> >(), indent + 1, has_elements); else write_xml_element(stream, it->first, it->second, indent + 1); } // Write closing tag if (indent >= 0) { if (has_elements) stream << Str(4 * indent, Ch(' ')); stream << Ch('<') << Ch('/') << key << Ch('>') << std::endl; } } }