/*   -*- coding: utf-8 -*-  */
/**********************************************************************
*
*  module   : Hfd
*  fonction : HfdIndexAnd
*  fichier  : HfdIndexBool.c
*  Auteur   : Jacques DUCLOY
*  Date     : Janvier 94
*******************************************************************************
*
* Copyleft (c) 2015 LorExplor
* 
***********************************************************************/

#include "HfdIndex.h"
#include "string.h"

#define nextE1()               \
{                              \
   e1=SxmlNextSibling(e1);            \
   if(e1)s1=SxmlLeafText(e1);	      \
   else s1=NULL;                      \
}

#define nextE2()               \
{                              \
   e2=SxmlNextSibling(e2);            \
   if (e2) s2=SxmlLeafText(e2);	      \
   else s2=NULL;                      \
}

#define putE2() {SxmlAppendChild(r12, SxmlLeafCreate(SxmlNodeName(e2),s2));}
#define putE1() {SxmlAppendChild(r12, SxmlLeafCreate(SxmlNodeName(e1),s1));}

SxmlNode *HfdIndexAnd(SxmlNode *l1, SxmlNode *l2)
{
  if(l1&&l2)
    {
      SxmlNode *e1;
      SxmlNode *e2;
      e1=SxmlFirstChild(l1);
      e2=SxmlFirstChild(l2);
      if(e1&&e2)
	{
	  char *s1;
	  char *s2;
	  SxmlNode *r12;

	  r12 = SxmlElementCreate(SxmlNodeName(l1));

	  s1=SxmlLeafText(e1);
	  s2=SxmlLeafText(e2);

	  while(1)
	    {
	      int test;
	      if((!e2)||(!e1))
		{
		  if(SxmlFirstChild(r12))return r12;
		  else 
		    {
		      SxmlFree(r12);
		      return NULL;
		    }
		}
	      if((test=strcmp(s1,s2))==0)
		{                               /* e1 == e2 */
		  putE1();
		  nextE1();
		  nextE2();
		}
	      else
		{
		  if (test>0)                   /* e1 > e2 */
		    {
		      nextE2();
		    }
		  else                          /* e1 < e2 */
		    {
		      nextE1();
		    }
		}
	    }
	}
      return NULL;
    }
  return NULL;
} 

SxmlNode *HfdIndexAndNot(SxmlNode *l1, SxmlNode *l2)
{
  if(l1&&l2)
    {
      SxmlNode *e1;
      SxmlNode *e2;
      int test;
      e1=SxmlFirstChild(l1);
      e2=SxmlFirstChild(l2);
      if(e1&&e2)
	{
	  char *s1;
	  char *s2;
	  SxmlNode *r12;

	  r12 = SxmlElementCreate(SxmlNodeName(l1));

	  s1=SxmlLeafText(e1);
	  s2=SxmlLeafText(e2);

	  while(1)
	    {
	      int test;
	      if((!e2)||(!e1))
		{
		  if (e2)        /*  end of e1 */
		    {
		      if(SxmlFirstChild(r12))return r12;
		      else 
			{
			  SxmlFree(r12);
			  return NULL;
			}
		    }
		  if (e1)
		    {
		      putE1();
		      nextE1();
		      continue;
		    }
		  if(SxmlFirstChild(r12))return r12;
		  else 
		    {
		      SxmlFree(r12);
		      return NULL;
		    }
		} /* en if (e2) */
	      /*              e1 || e2  */
	      test=strcmp(s1,s2);
	      if (test==0)
		{
		  nextE1();
		  nextE2();
		  continue;
		}
	      if (test>0)  
		{
		  nextE2(); /* avance d'un element dans la liste l2. */
		  continue;
		}
	      /* test < 0 */
	      putE1();
	      nextE1();
	      continue;
	    }
	}
      if (!e2)
	{
	  if (e1) return SxmlClone(l1);
	}
      return NULL;
    }
  if (!l2) 
    {
      if (l1) return SxmlClone(l1);
      return NULL;
    }
  return NULL;
}

SxmlNode *HfdIndexOr(SxmlNode *l1, SxmlNode *l2)
{
    if(l1&&l2)
      {
	SxmlNode *e1;
	SxmlNode *e2;
	e1=SxmlFirstChild(l1);
	e2=SxmlFirstChild(l2);
	if(e1&&e2)
	  {
	    char *s1;
	    char *s2;
	    SxmlNode *r12;
	    
	    r12 = SxmlElementCreate(SxmlNodeName(l1));

	    s1=SxmlLeafText(e1);
	    s2=SxmlLeafText(e2);

	    while (1)
	      {
		int test;
		if((!e2)||(!e1))
		  {
		    if((!e1)&&(!e2))
		      {
			return r12;
		      }
		    if(e1)
		      {
			putE1();
			nextE1();
		      }
		    else
		      {
			putE2();
			nextE2();
		      }
		    continue;
		  }
		test=strcmp(s1,s2);
		if(test==0)
		  {                               /* e1 == e2 */
		    putE1();
		    nextE1();
		    nextE2();
		    continue;
		  }
		if (test>0)                   /* e1 > e2 */
		  {
		    putE2();
		    nextE2();
		  }
		else                          /* e1 < e2 */
		  {
		    putE1();
		    nextE1();
		  }
		continue;
	      } /* end while */
	  }
	if (e1) return SxmlClone(l1);
	if (e2) return SxmlClone(l2);
	return NULL;
      }
    if (l1) return SxmlClone(l1);
    if (l2) return SxmlClone(l2);
    return NULL;
}
