/***********************************************************************
 
   Module   : Inist
   Commande : InistIlib2Sxml (ex PascalFromIlib)
   Fichier  : InistIlib2Sxml.c
   Auteur   : DUCLOY
   Date     : 20/1/97 - modifié 2012
   $Id: PascalFromIlib.c,v 1.3 2005/06/22 15:50:53 parmentf Exp $
 
************************************************************************/

#include "SxmlNode.h"
#include "Buffer.h"
#include <string.h>
#include <stdlib.h>

char buf2c[2];
char buf3c[3];
char buf4c[4];

int main (int argc, char **argv)
{
  SxmlNode *resul;
  SxmlNode *pascal;
  SxmlNode *partA;  
  SxmlNode *partR;
  SxmlNode *partS;
  SxmlNode *partT;
  SxmlNode *partU;
  SxmlNode *partV;
  SxmlNode *partW;
  
  SxmlNode *field;
  SxmlNode *nextField;
  SxmlNode *attDir;
  char *header;
  char *dirVal;

  Buffer *bufPascal;

  buf2c[1]=0;
  buf3c[2]=0;
  buf4c[3]=0;

  bufPascal=BufferCreate(1000,500);

  while ((BufferGets(bufPascal)))
    {
      if (strcmp(BufferString(bufPascal),"\015")==0)continue;
      if (BufferTailCmp(bufPascal,"\015")==0)BufferTailCut(bufPascal,1);
      pascal=SxmlFromString(BufferString(bufPascal));
      resul =SxmlElementCreate("inist");
      header =SxmlGetAttribute(pascal, "label");

      buf2c[0]=header[6];
      SxmlSetAttribute(resul,"h6",buf2c);

      SxmlAppendChild(resul,partA=SxmlElementCreate("pA"));
      partR=NULL;
      partS=NULL;
      partT=NULL;
      partU=NULL;
      partV=NULL;
      partW=NULL;


      field=SxmlFirstChild(pascal);
      while(field)
	{
	  nextField=SxmlNextSibling(field);
	  field=SxmlRemoveChild(field);
	  attDir=SxmlGetAttributeNode(field,"dir");
	  SxmlRemoveChild(attDir);

	  dirVal=SxmlNodeValue(attDir);
	  buf3c[0]=dirVal[0];
	  buf3c[1]=dirVal[1];
	  buf2c[0]=dirVal[2];
	  buf4c[0]=dirVal[3];
	  buf4c[1]=dirVal[4];
	  buf4c[2]=dirVal[5];

	  if(strcmp(buf3c,"00") !=0)SxmlSetAttribute(field,"i1",buf3c);
	  if(strcmp(buf2c,"0")  !=0)SxmlSetAttribute(field,"i2",buf2c);
	  if(strcmp(buf4c,"000")!=0)SxmlSetAttribute(field,"l" ,buf4c);
	    
	  switch((SxmlNodeName(field))[1])
	    {
	    case 'A':
	    case 'C':
	    case 'N':
	      SxmlAppendChild (partA,field);
	      break;
	    case 'R':
	      if(!partR)SxmlAppendChild(resul,partR=SxmlElementCreate("pR"));
	      SxmlAppendChild (partR,field);
	      (SxmlNodeName(field))[1]='A';
	      break;
	    case 'S':
	      if(!partS)SxmlAppendChild(resul,partS=SxmlElementCreate("pS"));
	      SxmlAppendChild (partR,field);
	      (SxmlNodeName(field))[1]='A';
	      break;
	    case 'T':
	      if(!partT)SxmlAppendChild(resul,partT=SxmlElementCreate("pS"));
	      SxmlAppendChild (partT,field);
	      (SxmlNodeName(field))[1]='A';
	      break;
	    case 'U':
	      if(!partU)SxmlAppendChild(resul,partU=SxmlElementCreate("pS"));
	      SxmlAppendChild (partU,field);
	      (SxmlNodeName(field))[1]='A';
	      break;
	    case 'V':
	      if(!partV)SxmlAppendChild(resul,partV=SxmlElementCreate("pS"));
	      SxmlAppendChild (partV,field);
	      (SxmlNodeName(field))[1]='A';
	      break;
	    case 'W':
	      if(!partW)SxmlAppendChild(resul,partW=SxmlElementCreate("pS"));
	      SxmlAppendChild (partW,field);
	      (SxmlNodeName(field))[1]='A';
	      break;
	    }
	  SxmlFree(attDir);
	  field=nextField;
	}

      SxmlPrint (resul);
      putchar('\n');
      SxmlFree(resul);
      SxmlFree(pascal);
    }

  return 0;
}

