/****************************************************************************
*
*      Projet  : DilibPro
*      Module  : Sgml
*      Fichier : SgmlCmp.c
*      Auteur  : J. Ducloy
*
*      Date    : Mai 94
*
****************************************************************************
*
* Copyright (c) 1994 CNRS/CRIN & INRIA Lorraine
* 
****************************************************************************/

#include "SgmlNode.h"

#include <string.h>
#include <stdio.h>

char *malloc(); 

int SgmlCmpAtt(n1,n2)
    SgmlNode *n1;
    SgmlNode *n2;
{
  int c1;
  if(n1)
    {
      if(n2)
	{
	  if ((c1=strcmp (SgmlAttId(n1),SgmlAttId(n2)))==0)
	    {
	      return strcmp (SgmlAttVal(n1), SgmlAttVal(n2));
	    }
	  return c1;
	}
      else return 1;
    }
  else {if(n2)return -1; else return 0;}
}

int SgmlCmpSubListAtt(n1,n2)
    SgmlNode *n1;
    SgmlNode *n2;
{
  int c1;
  if(n1)
    {
      if ((c1=SgmlCmpAtt(n1,n2))==0) 
	return SgmlCmpSubListAtt(SgmlNext(n1), SgmlNext(n2));
      else return c1;
    }
  else
    {
      if (n2) return -1; else return 0;
    }
}

int SgmlCmpListAtt(n1,n2)
     SgmlNode *n1;
     SgmlNode *n2;
{
  if(n1)
    {
      if (n2) return SgmlCmpSubListAtt(SgmlFirst(n1), SgmlFirst(n2));
      else return 1;
    }
  else
    {
      if (n2) return -1; else return 0;
    }
}

int SgmlCmpMark(n1,n2)
     SgmlNode *n1;
     SgmlNode *n2;
{
  int c1;
  if(n1)
    {
      if (n2)
	{
	  if((c1=strcmp(SgmlTag(n1),SgmlTag(n2)))==0)
	    {
	      return SgmlCmpListAtt(SgmlAttList(n1),SgmlAttList(n2));
	    }
	  else return c1;
	}
      else return 1;
    }
  else
    {
      if (n2) return -1; else return 0;
    }
}
int SgmlCmpElement();
int SgmlCmpListElement(n1,n2)
     SgmlNode *n1;
     SgmlNode *n2;
{
  int c1;
  if(n1)
    {
      if(n2)
	{
	  if((c1=SgmlCmpElement(n1,n2))==0)
	    return(SgmlCmpListElement(SgmlNext(n1),SgmlNext(n2)));
	  else return c1;
	}
      else return 1;
    }
  if(n2) return -1; else return 0;
}
/***********************************************************************
*   SgmlCmpElement (n1, n2);
*   compare 2 Elements supposés coherents 
*        (SGML Element ou Data mais pas ATT ou LATT)
*************************************************************************/
int SgmlCmpElement(n1,n2)
    SgmlNode *n1;
    SgmlNode *n2;
{
  int c1;
  if(n1)
    {
      if (n2)
	{
	  switch(SgmlType(n1))
	    {
	    case 'M':
	      {switch(SgmlType(n2))
	       {
	       case 'M':
		 if ((c1=SgmlCmpMark(n1,n2))==0)
		   return(SgmlCmpListElement(SgmlFirst(n1),SgmlFirst(n2)));
		 else return c1;
		 
	       case 'D':
		 return 1;
	       }
	     }
	    case 'D':
	      {switch(SgmlType(n2))
	       {
	       case 'M': 
		 return -1;
	       case 'D':
		 return strcmp(SgmlDataString(n1),SgmlDataString(n2));
	       }
	     }
	    }
	}
      else return 1;
    }
  else
    {
      if (n2) return -1; else return 0;
    }
  return 0;
}


int SgmlCmp(n1,n2)
    SgmlNode *n1;
    SgmlNode *n2;
{
  if(n1)
    {
      if (n2)
	{
	  switch(SgmlType(n1))
	    {
	    case 'M':
	    case 'D':
	      return SgmlCmpElement(n1,n2);
	    case 'L':
	      return SgmlCmpListAtt(n1,n2);
	    case 'A':
	      return SgmlCmpAtt(n1,n2);
	    }
	} 
      else return 1;
    }
  else
    {
      if (n2) return -1; else return 0;
    }
  return 0;
}
