
/***********************************************************************
*
*      Projet  : DilibPro
*      Module  : Sxml
*      Fichier : SxmlNodeList.c
*      Auteur  : J. Ducloy
*      $Id: SxmlNodeList.c,v 1.4 2003/07/07 09:23:06 parmentf Exp $
*
************************************************************************
* 
*     Copyright (C) 2002 - J DUCLOY & CNRS INIST
*
************************************************************************/
/**
   @file

   @brief Functions for SxmlNodeList handling
   
   An SxmlNodeList is a list of SxmlNode (that is to say a list of
   SxmlNodePointers). So, the children of an SxmlNodeList are
   SxmlNodePointers.
   
   @author &copy; 2001 INIST-CNRS
   @author Jacques DUCLOY
 */

#include "SxmlNode.h"
#include "Except.h"

/**
   Creates an SxmlNodeList

   Creates an SxmlNode having @c XML_NODE_NODE_LIST as a type.

   @return the created SxmlNodeList
   @warning the returned SxmlNodeList has to be freed.

 */
SxmlNodeList *SxmlNodeListCreate()
{
  SxmlNode *l1;
  l1=SxmlNodeCreate(XML_NODE_NODE_LIST);
  return l1;
}

/**
   Appends @a n1 to @a l1.

   @param l1 list to which to append @a n1
   @param n1 node to append to @a l1
   @return @a n1 (the node appended)
 */
SxmlNode *SxmlNodeListAppend(SxmlNodeList *l1, SxmlNode *n1)
{
  SxmlNode *c1;
  c1=SxmlContainerCreate(XML_CONTAINER_XML_NODE);
  SxmlSetNodeValue(c1,(char *)n1);
  SxmlAppendChild(l1,c1);
  return n1;
}


/**
   Gives the next node in @a l1.

   Use this function to go through to whole list of nodes.
   Before going through the nodes, a SxmlReset is needed on @a l1.

   Example:
   @code
   SxmlReset(nl);
   while((node = SxmlNodeListNextNode(nl))) {
     ...  Use node 
   }
   @endcode

   @param l1 list to go through (SxmlNodeList *)
   @return the next node in @a l1

   @see SxmlReset
 */

SxmlNode *SxmlNodeListNextNode(SxmlNodeList *l1)
{
  SxmlNode *e1;
  if ((e1=SxmlNextNode(l1)))
    return (SxmlNode *)SxmlNodeValue(e1);
  else return NULL;
}

SxmlNode *SxmlNodeListPull(SxmlNodeList *l1)
{
  SxmlNode *top;
  top=SxmlLastChild(l1);
  if (top)SxmlFree(top);
  return SxmlLastChild(l1);
}

/**
   Gives the last item of @a l1, the SxmlNodeList.

   @param l1 the SxmlNodeList
   @return the last item of @a l1
 */
SxmlNode *SxmlNodeListLastItem(SxmlNode *l1)
{
  return((SxmlNode *)SxmlNodeValue(SxmlLastChild(l1)));
}

/**
   Frees the last item of @a l1.

   @param l1 the SxmlNodeList
   @returns the last node of @a l1

   @todo determine whether or not the returned value is already freed!
 */
SxmlNode *SxmlNodeListFreeLast(SxmlNode *l1)
{
  SxmlNode *value = (SxmlNode *)SxmlNodeValue(SxmlLastChild(l1));
  SxmlFree(SxmlLastChild(l1));
  return (value);
}

