Deleting pieces: xml_delete

Previous: xml_free: Cleaning up afterwards ] [ Top: index ] [ Next: Children: xml_first and xml_last ]

Deleting a piece out of an XML structure is more than just freeing it; we have to close ranks before and after as well. (May 26, 2002) - yet another teensy code bloat: xml_delete_pretty not only deletes the piece passed to it, it also checks after it to see whether there is whitespace added by xml_append_pretty. If so, it deletes that as well. If the remaining content of the deleted piece's parent is whitespace, that gets deleted too, so that in all, an xml_append_pretty followed by an xml_delete_pretty should have null effect.
 
XMLAPI void xml_delete(XML * piece)
{
   ELEMENTLIST * list;
   if (!piece) return;
   if (piece->parent != NULL) {
      list = piece->parent->children;
      while (list != NULL && list->element != piece) list = list->next;
      if (list != NULL) {
         if (list->next != NULL) list->next->prev = list->prev;
         if (list->prev != NULL) list->prev->next = list->next;
      }
      if (list == piece->parent->children) piece->parent->children = list->next;
      if (list == piece->parent->lastchild) piece->parent->lastchild = list->prev;
      if (list != NULL) FREE ((void *) list);
   }
   xml_free (piece);
}
XMLAPI void xml_delete_pretty(XML * piece)
{
   XML * prev = xml_prev (piece);
   XML * next = xml_next (piece);

   if (!strcmp (xml_textval (next), "\n")) xml_delete (next);
   xml_delete (piece);
   if (!xml_prev (prev) && !strcmp (xml_textval (prev), "\n")) xml_delete (prev);
}
Previous: xml_free: Cleaning up afterwards ] [ Top: index ] [ Next: Children: xml_first and xml_last ]


This code and documentation are released under the terms of the GNU license. They are copyright (c) 2000-2003, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license. This presentation was created using LPML.