

/***********************************************************************
*
* Projet   : DilibPro
* Module   : SgmlPath
* Fichier  : SgmlPathNum.c
* Auteur   : Ducloy
* Date     : 10/98
*
************************************************************************
* 
*     Copyrigth (C) 1998 CRIN - CNRS & INRIA Lorraine
*
*************************************************************************/
#include "SgmlPathNum.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

SgmlNode *SgmlPathNumGet(n1,path)
SgmlNode *n1;
char *path;
{
  char *posSl;
  SgmlNode *resu;
  if(!n1)return NULL;
  if(!path)return NULL;
  if(strlen(path)==0)return n1;
  if ((posSl=strchr(path,'/')))
    {
      int i;
      SgmlNode *n2;
      i=atoi(path);
      if((n2=SgmlGetSonNumber(n1,i)))
	resu=SgmlPathNumGet(n2,posSl+1);
      else resu=NULL;
    }
  else
    {
      int i;
      i=atoi(path);
      resu=SgmlGetSonNumber(n1,i);
    }
  return resu;
}

SgmlNode *SgmlPathNumSet(n1,path,n2)
SgmlNode *n1;
SgmlNode *n2;
char *path;
{
  SgmlNode *n3;
  char *posSl;
  int numPos;
  if(!n2)return n1;
  if ((n3=SgmlPathNumGet(n1,path)))
    {
      SgmlReplaceContent(n3,n2);
      return n1;
    }
  if (!n1)
    {
      SgmlNode *n3;
      if (!path)return NULL;
      if (strlen(path)==0)return (n2);
      n3=SgmlCreateMark("");
      if (SgmlPathNumSet(n3,path,n2))return n1;
      else return NULL;
    }
  numPos=atoi(path);
  if ((posSl=strchr(path,'/')))
    {
    }
  else
    {
      /* Unused variable */
/*       SgmlNode *c1; */
      int i;
      if (numPos==0)
	{
	  SgmlReplaceContent(n1,n2);
	  return n1;
	}
      i=numPos-SgmlCard(n1)-1;
      while(i--)SgmlAddSon(n1, SgmlCreateMark(""));
      SgmlAddSon(n1,n2);
      return n1;
    }
  return NULL;
  
}

SgmlNode *SgmlPathNumSetTag(n1,path,tag)
SgmlNode *n1;
char *tag;
char *path;
{
  SgmlNode *n3;
  if ((n3=SgmlPathNumGet(n1,path)))
    {
      SgmlSetTag(n3,tag);
      return n1;
    }
  return SgmlPathNumSet(n1,path,SgmlCreateMark(tag));
}
