inserting things: xml_insertbefore and xml_insertafter

Previous: Siblings: xml_next and xml_prev ] [ Top: index ] [ Next: xml_copy: making fresh copies of XML ]

(September 24, 2001): This page had been sitting here for a year, and now I finally have a use for it (it supports xml_replacewithcontent). Each function does what it says, and returns a pointer to the "new" XML it's given.
 
XMLAPI XML * xml_insertbefore (XML * orig, XML * new)
{
   ELEMENTLIST * list;
   ELEMENTLIST * ch;

   if (orig == NULL) return new;
   if (orig->parent == NULL) return new;
   if (new == NULL) return new;

   list = orig->parent->children;
   while (list != NULL && list->element != orig) list = list->next;
   if (list == NULL) return new;

   new->parent = orig->parent;

   ch = (ELEMENTLIST *) MALLOC (sizeof(struct _list));
   ch->element = new;
   ch->next = list;
   ch->prev = list->prev;
   if (!ch->prev) orig->parent->children = ch;
   else           ch->prev->next = ch;
   list->prev = ch;

   return (new);
}
XMLAPI XML * xml_insertafter (XML * orig, XML * new)
{
   ELEMENTLIST * list;
   ELEMENTLIST * ch;

   if (orig == NULL) return new;
   if (orig->parent == NULL) return new;
   if (new == NULL) return new;

   list = orig->parent->children;
   while (list != NULL && list->element != orig) list = list->next;
   if (list == NULL) return new;

   new->parent = orig->parent;

   ch = (ELEMENTLIST *) MALLOC (sizeof(struct _list));
   ch->element = new;
   ch->prev = list;
   ch->next = list->next;
   if (!ch->next) orig->parent->lastchild = ch;
   else           ch->next->prev = ch;
   list->next = ch;

   return (new);
}
Previous: Siblings: xml_next and xml_prev ] [ Top: index ] [ Next: xml_copy: making fresh copies of XML ]


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.