/******************************************************************************
*
* Projet    : DilibSgml
* Module    : SgmlString
* Fichier   : SgmlToString.c
* Auteur    : Ducloy
*
****************************************************************************
*
* Copyright (c) 1994 CNRS/CRIN & INRIA Lorraine
* 
****************************************************************************
*				     SgmlToString.c 
*
*
*        Fonctions d'impression s'une structure Sgml
*
****************************************************************************/


#include "SgmlString.h"
#include "Buffer.h"
#include "Except.h"

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

     void SgmlCatToString ();

static int firstCall=1;
Buffer *bufferSgmlToString;

/**********************************************************************


***********************************************************************/
void SgmlCatMarkToString (noeud)
         SgmlNode* noeud;
{
  char *localFormat;
  char *prevStrFormat;
  char *nextStrFormat;

  localFormat=SgmlNodeFormat(noeud);

    switch ( SgmlSubType(noeud) ){
    case 'S':
    case 'E':
      nextStrFormat=strstr(localFormat,"%s");
      if(!(nextStrFormat))
	ExceptSetError("SgmlToString","01","format incorrect ",localFormat,"",1);
      BufferStrncat(bufferSgmlToString,localFormat, nextStrFormat-localFormat);
      nextStrFormat+=2;
      prevStrFormat=nextStrFormat;
      BufferStrcat(bufferSgmlToString,SgmlTag(noeud));
      if(!(nextStrFormat=strstr(nextStrFormat,"%s"))) 
	ExceptSetError("SgmlToString","01","format incorrect ",localFormat,"",1);
      if ( SgmlAttList(noeud) ) 
	{
          BufferStrncat(bufferSgmlToString,prevStrFormat, nextStrFormat-prevStrFormat);
	  SgmlCatToString(SgmlAttList(noeud));
	}
      nextStrFormat+=2;
      if ( SgmlSubType(noeud)=='E')
	{
	  BufferStrcat(bufferSgmlToString,nextStrFormat);
	  return;
	}
      else
	{
	  prevStrFormat=nextStrFormat;
	  if(!(nextStrFormat=strstr(nextStrFormat,"%s"))) 
	    ExceptSetError("SgmlToString","01","format incorrect ",localFormat,"",1);
	  BufferStrncat(bufferSgmlToString,prevStrFormat, nextStrFormat-prevStrFormat);
	  SgmlDo (noeud, fils , 
		  {SgmlCatToString(fils);}); 
	  nextStrFormat+=2;
	  prevStrFormat=nextStrFormat;
	  if(!(nextStrFormat=strstr(nextStrFormat,"%s"))) 
	    ExceptSetError("SgmlToString","01","format incorrect ",localFormat,"",1);
	  BufferStrncat(bufferSgmlToString,prevStrFormat, nextStrFormat-prevStrFormat);
	  BufferStrcat(bufferSgmlToString,SgmlTag(noeud));
          nextStrFormat+=2;
          BufferStrcat(bufferSgmlToString,nextStrFormat);
	}
      break;

    case 'R':
	  SgmlDo (noeud, fils , 
		  {SgmlCatToString(fils);
		   if(SgmlNext(fils)) BufferStrcat(bufferSgmlToString,"\t");
                   else BufferStrcat(bufferSgmlToString,"\n");
                  }); 
      break;
    }
}

void SgmlCatToString (noeud)
         SgmlNode* noeud;
{
  char *localFormat;
  char *nextStrFormat;
  char *prevStrFormat;

  localFormat=SgmlNodeFormat(noeud);

  switch ( SgmlType(noeud) ){

  case 'M'  :
    SgmlCatMarkToString (noeud);
     break;

  case 'L' :
    
    SgmlDo (noeud, fils , 
	    { SgmlCatToString(fils); });
    break;

  case 'A' :
  case 'v':
    if(!(nextStrFormat=strstr(localFormat,"%s"))) 
      ExceptSetError("SgmlToString","01","format incorrect ",localFormat,"",1);
    BufferStrncat(bufferSgmlToString,localFormat, nextStrFormat-localFormat);
    BufferStrcat(bufferSgmlToString,SgmlAttId(noeud));
    nextStrFormat+=2;
    prevStrFormat=nextStrFormat;
    if(!(nextStrFormat=strstr(nextStrFormat,"%s"))) 
      ExceptSetError("SgmlToString","01","format incorrect ",localFormat,"",1);
    BufferStrncat(bufferSgmlToString,prevStrFormat, nextStrFormat-prevStrFormat);
    BufferStrcat(bufferSgmlToString,SgmlAttVal(noeud));
    BufferStrcat(bufferSgmlToString,nextStrFormat+2);
    break;

  case 'D' :
    if(!(nextStrFormat=strstr(localFormat,"%s"))) 
      ExceptSetError("SgmlToString","01","format incorrect ",localFormat,"",1);
    BufferStrncat(bufferSgmlToString,localFormat, nextStrFormat-localFormat);
    BufferStrcat(bufferSgmlToString,SgmlDataString(noeud));
    BufferStrcat(bufferSgmlToString,nextStrFormat+2);
    break ;

  case 'C' :
    if (SgmlSubType(noeud)=='C')
      {
	if(!(nextStrFormat=strstr(localFormat,"%s"))) 
	  ExceptSetError("SgmlToString","01","format incorrect ",localFormat,"",1);
	BufferStrncat(bufferSgmlToString,localFormat, nextStrFormat-localFormat);
	BufferStrcat(bufferSgmlToString,SgmlDataString(noeud));
	BufferStrcat(bufferSgmlToString,nextStrFormat+2);
      }
    else
      {
	BufferStrcat(bufferSgmlToString,SgmlDataString(noeud));
      }
    break ;
 
  }
}


/************************************************************************

   SgmlToString

***********************************************************************/

char* SgmlToString (noeud)

         SgmlNode* noeud;
{
  if(!noeud)return("");
     if (firstCall)
       {
	 firstCall=0;
         bufferSgmlToString =BufferCreate(100,100);
       }
     else BufferReset (bufferSgmlToString);
     SgmlCatToString(noeud);
    return (BufferSave(bufferSgmlToString)) ;
  }

char* SgmlToStringPtr (noeud)

         SgmlNode* noeud;
{
     if (firstCall)
       {
	 firstCall=0;
         bufferSgmlToString =BufferCreate(100,100);
       }
     else BufferReset (bufferSgmlToString);
     SgmlCatToString(noeud);
    return (BufferString(bufferSgmlToString)) ;
  }
