From b979d4c2bd1e8f445befd53ea8357b594f1ca3b2 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine@gmail.com" Date: Tue, 19 Oct 2010 14:29:02 +0000 Subject: Added prepend_attribute, prepend_child and prepend_copy functions git-svn-id: http://pugixml.googlecode.com/svn/trunk@769 99668b35-9821-0410-8761-19e4c4f06640 --- src/pugixml.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pugixml.hpp | 4 ++++ 2 files changed, 75 insertions(+) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index f53ce1d..241eaed 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -3775,6 +3775,31 @@ namespace pugi return a; } + xml_attribute xml_node::prepend_attribute(const char_t* name) + { + if (type() != node_element && type() != node_declaration) return xml_attribute(); + + xml_attribute a(allocate_attribute(get_allocator(_root))); + if (!a) return xml_attribute(); + + a.set_name(name); + + xml_attribute_struct* head = _root->first_attribute; + + if (head) + { + a._attr->prev_attribute_c = head->prev_attribute_c; + head->prev_attribute_c = a._attr; + } + else + a._attr->prev_attribute_c = a._attr; + + a._attr->next_attribute = head; + _root->first_attribute = a._attr; + + return a; + } + xml_attribute xml_node::insert_attribute_before(const char_t* name, const xml_attribute& attr) { if ((type() != node_element && type() != node_declaration) || attr.empty()) return xml_attribute(); @@ -3841,6 +3866,16 @@ namespace pugi return result; } + xml_attribute xml_node::prepend_copy(const xml_attribute& proto) + { + if (!proto) return xml_attribute(); + + xml_attribute result = prepend_attribute(proto.name()); + result.set_value(proto.value()); + + return result; + } + xml_attribute xml_node::insert_copy_after(const xml_attribute& proto, const xml_attribute& attr) { if (!proto) return xml_attribute(); @@ -3872,6 +3907,33 @@ namespace pugi return n; } + xml_node xml_node::prepend_child(xml_node_type type) + { + if (!allow_insert_child(this->type(), type)) return xml_node(); + + xml_node n(allocate_node(get_allocator(_root), type)); + if (!n) return xml_node(); + + n._root->parent = _root; + + xml_node_struct* head = _root->first_child; + + if (head) + { + n._root->prev_sibling_c = head->prev_sibling_c; + head->prev_sibling_c = n._root; + } + else + n._root->prev_sibling_c = n._root; + + n._root->next_sibling = head; + _root->first_child = n._root; + + if (type == node_declaration) n.set_name(PUGIXML_TEXT("xml")); + + return n; + } + xml_node xml_node::insert_child_before(xml_node_type type, const xml_node& node) { if (!allow_insert_child(this->type(), type)) return xml_node(); @@ -3929,6 +3991,15 @@ namespace pugi return result; } + xml_node xml_node::prepend_copy(const xml_node& proto) + { + xml_node result = prepend_child(proto.type()); + + if (result) recursive_copy_skip(result, proto, result); + + return result; + } + xml_node xml_node::insert_copy_after(const xml_node& proto, const xml_node& node) { xml_node result = insert_child_after(proto.type(), node); diff --git a/src/pugixml.hpp b/src/pugixml.hpp index 81fc6c3..cccfb7f 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -412,21 +412,25 @@ namespace pugi // Add attribute with specified name. Returns added attribute, or empty attribute on errors. xml_attribute append_attribute(const char_t* name); + xml_attribute prepend_attribute(const char_t* name); xml_attribute insert_attribute_after(const char_t* name, const xml_attribute& attr); xml_attribute insert_attribute_before(const char_t* name, const xml_attribute& attr); // Add a copy of the specified attribute. Returns added attribute, or empty attribute on errors. xml_attribute append_copy(const xml_attribute& proto); + xml_attribute prepend_copy(const xml_attribute& proto); xml_attribute insert_copy_after(const xml_attribute& proto, const xml_attribute& attr); xml_attribute insert_copy_before(const xml_attribute& proto, const xml_attribute& attr); // Add child node with specified type. Returns added node, or empty node on errors. xml_node append_child(xml_node_type type = node_element); + xml_node prepend_child(xml_node_type type = node_element); xml_node insert_child_after(xml_node_type type, const xml_node& node); xml_node insert_child_before(xml_node_type type, const xml_node& node); // Add a copy of the specified node as a child. Returns added node, or empty node on errors. xml_node append_copy(const xml_node& proto); + xml_node prepend_copy(const xml_node& proto); xml_node insert_copy_after(const xml_node& proto, const xml_node& node); xml_node insert_copy_before(const xml_node& proto, const xml_node& node); -- cgit v1.2.3