/***********************************************************************
*
*  projet    : DilibPro
*  module    : Sgml
*  fonctions : 
*  fichier   : SgmlStrLeaf.c
*  Auteur    : Jacques DUCLOY
*  Date      : Janvier 94
*  $Id: SgmlStrLeaf.c,v 1.2 2005/06/22 14:24:54 parmentf Exp $
************************************************************************
*
* Copyright (c) 1994 CNRS/CRIN & INRIA Lorraine
* 
************************************************************************/
#include <string.h>
#include <stdlib.h>

int SgmlStrCmpCode;  /* #include "SgmlNode.h" */

char *SgmlStrGetLeaf(char *s1, char *tag)
{
  static char *bufString;
  static int   sizBufString=0;
  char *s2;
  char *s3;
  int stringLen;
  int tagLen;

  tagLen=strlen(tag);
  s2=s1;
  while((s2=strchr(s2,'<')))
    {
      s2++;
      if (strncmp(s2,tag,tagLen)==0)
	{
	  s2+=tagLen;
	  switch (*s2)
	    {
	    case ' ':
	      if(!(s2=strchr(s2,'>')))return NULL;
	    case '>':
	      s2++;
	      if(!(s3=strchr(s2,'<')))return NULL;
	      stringLen=s3-s2+1;
	      if(stringLen>sizBufString)
		{
		  if(sizBufString)free(bufString);
		  bufString=malloc(sizBufString=stringLen);
		}
	      bufString[0]=0;
	      strncat(bufString,s2,stringLen-1);
	      return bufString;
	    }
	}
    }
  return NULL;
}


int SgmlStrLeafCmp(char *s1, char *tag, char *str)
{
  char *s2;
  char *s3;
  int stringLen;
  int tagLen;
  int subStringLen;
  int mini;

  tagLen=strlen(tag);
  stringLen=strlen(str);

  s2=s1;

  while((s2=strchr(s2,'<')))
    {
      s2++;
      if (strncmp(s2,tag,tagLen)==0)
	{
	  s2+=tagLen;
	  switch (*s2)
	    {
	    case ' ':
	      if(!(s2=strchr(s2,'>')))return 0; /* return NULL */
	    case '>':
              s2++;
	      if(!(s3=strchr(s2,'<')))return 0; /* return NULL */
	      subStringLen=s3-s2;
	      if(stringLen>subStringLen)mini=subStringLen;
	      else mini=stringLen;
	      SgmlStrCmpCode=strncmp(s2,str,mini);
	      if((SgmlStrCmpCode==0)&&(stringLen!=subStringLen))
                {
                   if(subStringLen>stringLen) SgmlStrCmpCode=1;
		   else SgmlStrCmpCode=-1;
                }
              return 1;
	    }
	}
    }
  return 0; /* return NULL */
}

