/**********************************************************************
*
*  projet   : DilibPro
*  module   : SgmlPath
*  commande : SgmlReplace
*  fichier  : SgmlReplace.c
*  Auteur   : Catherine CZYSZ
*  Date     : Avril 94
*  $Id: SgmlReplace.c,v 1.2 2003/03/17 17:08:01 parmentf Exp $
*
***********************************************************************/
#include <stdio.h>
#include "StrSearch.h"
#include "SgmlInput.h"
#include "SgmlPath.h"
#include <string.h>

char *val, *str;
StrSearchTable *table;
int getopt();
extern char *optarg;


/* parametres -t table de transcodage -i liste des chemins  */

void usage()
{
  fprintf(stderr,"usage: SgmlReplace [-t table] [-i pathlist]...\n");
  exit(1);
}
		
		 			     
/* programme principal */

int main(argc,argv)
      int argc;
      char **argv;

{
  char *tabtrans;
  int c;
  SgmlPathList *liPa;
  SgmlNode *noeud;
  
  liPa=SgmlPathList_Create();
  
  while((c=getopt(argc,argv,"t:i:"))!=EOF)
    
    {switch (c){

    case 't':
      tabtrans=optarg;
      table=StrSearchGetTable(tabtrans,100);
      break;
      
    case 'i':
      SgmlPathList_GetOpt(liPa,argc,argv);
      break;
      
    default:
      usage();
    }
    };
  
     
  while(SgmlInputNextRecord())
    {

      /*Remplacements d'elts Sgml correspondants a une liste de chemins */
      
      SgmlPathList_Init(liPa,SgmlInputRecord);

      while((noeud=SgmlPathList_Next(liPa)))
	{
	  if((str=SgmlLeafGetData(noeud)))
	    {
	      if((val=StrSearch(table,str)))
		SgmlLeafPutString(noeud,val);
	      
	    }
	}
      
      SgmlInputRecordPrint();
      
    }
  return 0;
}


