/**************************************************************************************************
**	Function name : moulineFormule
**
**	Description : Apres detection d'une formule, on la mouline de facon a rectifier les
                      absurdites de formatage de RTF-Fields.
**	Input : void 
**	Output : void
**************************************************************************************************/
void moulineFormule(void)
{
  int c;
  int i=0;
  int j=0;
  int k=0;
  int l=0;
  int FLAG_Inblock=0;
  int MFlongeur=5000;
  char MFcalutemp;
  char * MFtableau;
  char * MFtableautemp;
  char TABFmt[5][128];
  /* Esperons qu'il n'y aura pas plus de 5 blocs imbriques avec 128 caracteres de mots de commande... */
  MFtableau=(char *) malloc (MFlongeur);
  fclose(auxiliaire);
  if ((auxiliaire=fopen(nomsort,"r"))==NULL)
    {
      fprintf(stderr,"FATAL ERROR: Could not open Auxiliary File:%s\n",nomsort);
      fclose(entree);
      fclose(sortie);
      exit(0);
    }
  rewind(auxiliaire);
  c=fgetc(auxiliaire);
  while(c!=EOF)
    {
      if (i==MFlongeur)
	{
	  MFlongeur*=2;
	  MFtableau=(char * ) realloc (MFtableau, MFlongeur + 1);
	}
      if (c!='\n')     /* On supprime les retours a la ligne a l'interieur des formules */
	MFtableau[i++]=c;
      c=fgetc(auxiliaire);
    }
  MFtableau[i]='\0';    
  fclose(auxiliaire);
  if ((auxiliaire=fopen(nomsort,"w"))==NULL)
    {
      fprintf(stderr,"FATAL ERROR: Could not open Auxiliary File:%s\n",nomsort);
      fclose(entree);
      fclose(sortie);
      exit(0);
    }
  /* On a recupere la formule dans le sale format de RTF. On essaye de la purifier.*/
  /* Toute formule commence par "eq " */
  i=0;
  MFtableautemp=(char *) malloc (strlen(MFtableau));
  while (i<strlen(MFtableau))
    {
      if (MFtableau[i]=='{')
	FLAG_Inblock++;
      else if (MFtableau[i]=='}')
	FLAG_Inblock--;
      else if (MFtableau[i]=='\\' && MFtableau[i+1]!='\\' && FLAG_Inblock && MFtableau[i+1]!='\'')
	{
	  /* Lire les mots de commande et les enregistrer dans un tableau. 
	     Remarque: On les conserve toujours dans MFtableau */
	  j=0;
	  while (MFtableau[i]!=' ' && i<strlen(MFtableau) && MFtableau[i]!='}' &&
		 MFtableau[i]!=',' && MFtableau[i]!='(' && MFtableau[i]!=')' && 
		 MFtableau[i]!=';')
	    TABFmt[FLAG_Inblock][j++]=MFtableau[i++];
	  TABFmt[FLAG_Inblock][j++]=' ';   /* On ajoute un separateur: En mode maths les espaces sont
					      ignores par TeX :-) */
	  TABFmt[FLAG_Inblock][j]='\0';
	  i--;
	}
      else 
	if (FLAG_Inblock && (MFtableau[i]==',' || MFtableau[i]==';' || MFtableau[i]==')' || MFtableau[i]=='('))
	  {
	    MFcalutemp=MFtableau[i];
	    strcpy(MFtableautemp,&MFtableau[i+1]);
	    /* Fermeture du bloc */
	    MFtableau[i++]='}';
	    /* Ecriture du caractere special */
	    MFtableau[i++]=MFcalutemp;
	    /* Reouverture du bloc avec les caracteristiques precedentes */
	    MFtableau[i++]='{';
	    k=0;
	    while(k<=j)
	      {
		MFtableau[i+k]=TABFmt[FLAG_Inblock][k];
		k++;
	      }
	    i=strlen(MFtableau);
	    strcat(&MFtableau[i],MFtableautemp);
	    i--;
	  }
	else 
	  if (MFtableau[i]=='\\' && MFtableau[i+1]=='\\' && FLAG_Inblock)
	    {
	      /* Dans un bloc interne a l'equation, il y a un mot de commande de
		 type equation. */
	      /* On sauvegarde la chaine de mots de commande et ce qui suit*/
	      strcpy(MFtableautemp,&MFtableau[i]);
	      /* Fermeture du bloc */
	      MFtableau[i++]='}';
	      /* Ecriture de la chaine de mots de commande */
	      l=0;
	      do
		{
		  MFtableau[i++]=MFtableautemp[l++];
		} while (l<strlen(MFtableautemp) && MFtableautemp[l-1]!='(');
	      /* Reouverture du bloc avec les caracteristiques precedentes */
	      MFtableau[i++]='{';
	      k=0;
	      while(k<=j)
		{
		  MFtableau[i+k]=TABFmt[FLAG_Inblock][k];
		  k++;
		}
	      i=strlen(MFtableau);
	      strcat(&MFtableau[i],&MFtableautemp[l]);
	      i--;
	    }
	   
      i++;
    }
  
  /* On re-ecris la formule purifiee dans le fichier auxiliaire. */
  fprintf(auxiliaire,"%s \\pzpendeq",MFtableau);
  free(MFtableau);
  fclose(auxiliaire);
  if ((auxiliaire=fopen(nomsort,"r"))==NULL)
    {
      fprintf(stderr,"FATAL ERROR: Could not open Auxiliary File:%s\n",nomsort);
      fclose(entree);
      fclose(sortie);
      exit(0);
    }
}

/**************************************************************************************************
**	Function name : moulineFormuleMAC
**
**	Description : Apres detection d'une formule MAC, on la mouline de facon a rectifier les
                      absurdites de formatage de RTF-Fields.
**	Input : void 
**	Output : void
**************************************************************************************************/
void moulineFormuleMAC(void)
{
  int c,oldc;
  int i=0;
  int j=0;
  int k=0;
  int l=0;
  int FLAG_Inblock=0;
  int FLAG_WasInBlock=0;
  int MFlongeur=5000;
  char MFcalutemp;
  char * MFtableau;
  char * MFtableautemp;
  char TABFmt[5][128];
  /* Esperons qu'il n'y aura pas plus de 5 blocs imbriques avec 128 caracteres de mots de commande... */
  MFtableau=(char *) malloc (MFlongeur);
  fclose(auxiliaire);
  if ((auxiliaire=fopen(nomsort,"r"))==NULL)
    {
      fprintf(stderr,"FATAL ERROR: Could not open Auxiliary File:%s\n",nomsort);
      fclose(entree);
      fclose(sortie);
      exit(0);
    }
  rewind(auxiliaire);
  c=fgetc(auxiliaire);
  oldc=c;
  while(c!=EOF)
    {
      if (i==MFlongeur)
	{
	  MFlongeur*=2;
	  MFtableau=(char * ) realloc (MFtableau, MFlongeur + 1);
	}
      if (c!='\n' && c!='|' && c!=';' && c!=' ' && c!=',')  
	/* On supprime les retours a la ligne et les espaces a l'interieur des formules */
	MFtableau[i++]=c;        
      if (c=='|' && oldc=='\\')                /* Et on remplace les '|' par des backslashs */
	MFtableau[i++]='\\';
      if (c==';')
	MFtableau[i++]=',';      /* On change les ';' en ','. On verra apres ce qui est plus adapte */
      if (c==',')
	MFtableau[i++]=';';      /* On change les ',' en ';'. On verra apres ce qui est plus adapte */
      oldc=c;
      c=fgetc(auxiliaire);
    }
  MFtableau[i]='\0';    
  fclose(auxiliaire);
  if ((auxiliaire=fopen(nomsort,"w"))==NULL)
    {
      fprintf(stderr,"FATAL ERROR: Could not open Auxiliary File:%s\n",nomsort);
      fclose(entree);
      fclose(sortie);
      exit(0);
    }
  /* On a recupere la formule dans le sale format de RTF. On essaye de la purifier.*/
  /* Toute formule commence par "eq " */
  i=0;
  MFtableautemp=(char *) malloc (strlen(MFtableau));
  while (i<strlen(MFtableau))
    {
      if (MFtableau[i]=='{' && i>0 && MFtableau[i-1]!='\\')
	FLAG_Inblock++;
      else 
	if (MFtableau[i]=='}' && i>0 && MFtableau[i-1]!='\\')
	  {
	    FLAG_Inblock--;
	    if (FLAG_Inblock==-1)
	      {
		FLAG_Inblock=0;
		FLAG_WasInBlock=1;
	      };
	  }
	else 
	  if (MFtableau[i]=='\\' && i>0 && i+2<=strlen(MFtableau) &&
	      MFtableau[i+1]=='\\' && MFtableau[i+2]=='\\')
	    {
	      strcpy(&MFtableau[i],&MFtableau[i+1]); /* Le pipe a ete transforme en antislash, donc il y en
							a quatre maintenant (un en trop). On le vire. */
	    }
	  else
	    if ( (MFtableau[i]=='\\' && MFtableau[i+1]!='\\') && FLAG_Inblock && MFtableau[i+1]!='\'')
	      {
		/* Lire les mots de commande et les enregistrer dans un tableau. 
		   Remarque: On les conserve toujours dans MFtableau */
		j=0;
		while (MFtableau[i]!=' ' && i<strlen(MFtableau) && MFtableau[i]!='}' &&
		       MFtableau[i]!=',' && MFtableau[i]!='(' && MFtableau[i]!=')' && 
		       MFtableau[i]!=';' && !(MFtableau[i]=='\\' && MFtableau[i+1]=='\\')
		       && !(MFtableau[i]=='\\' && MFtableau[i+1]=='\''))
		  TABFmt[FLAG_Inblock][j++]=MFtableau[i++];
		TABFmt[FLAG_Inblock][j++]=' ';   /* On ajoute un separateur: On mode maths les espaces sont
						    ignores par TeX :-) */
		TABFmt[FLAG_Inblock][j]='\0';
		i--;
	      }
	    else 
	      if (FLAG_Inblock && (MFtableau[i]==',' || MFtableau[i]==';' || MFtableau[i]==')' || MFtableau[i]=='('))
		{
		  MFcalutemp=MFtableau[i];
		  strcpy(MFtableautemp,&MFtableau[i+1]);
		  /* Fermeture du bloc */
		  MFtableau[i++]='}';
		  /* Ecriture du caractere special */
		  MFtableau[i++]=MFcalutemp;
		  /* Reouverture du bloc avec les caracteristiques precedentes */
		  MFtableau[i++]='{';
		  k=0;
		  while(k<=j)
		    {
		      MFtableau[i+k]=TABFmt[FLAG_Inblock][k];
		      k++;
		    }
		  i=strlen(MFtableau);
		  strcat(&MFtableau[i],MFtableautemp);
		  i--;
		}
	    else 
	      if (MFtableau[i]=='\\' && MFtableau[i+1]=='\\' && FLAG_Inblock)
		{
		  /* Dans un bloc interne a l'equation, il y a un mot de commande de
		     type equation. */
	      /* On sauvegarde la chaine de mots de commande et ce qui suit*/
	      strcpy(MFtableautemp,&MFtableau[i]);
	      /* Fermeture du bloc */
	      MFtableau[i++]='}';
	      /* Ecriture de la chaine de mots de commande */
	      l=0;
	      do
		{
		  MFtableau[i++]=MFtableautemp[l++];
		} while (l<strlen(MFtableautemp) && MFtableautemp[l-1]!='(');
	      /* Reouverture du bloc avec les caracteristiques precedentes */
	      MFtableau[i++]='{';
	      k=0;
	      while(k<=j)
		{
		MFtableau[i+k]=TABFmt[FLAG_Inblock][k];
		k++;
		}
	      i=strlen(MFtableau);
	      strcat(&MFtableau[i],&MFtableautemp[l]);
	      i--;
		};
	    
	    if (FLAG_WasInBlock==1)
	      {
	      FLAG_WasInBlock=-1;
	      strcpy(&MFtableau[i],&MFtableau[i+1]);
	      }
	    else i++;
    }
  /*  if (FLAG_WasInBlock==-1 ) { MFtableau[i++]='}'; MFtableau[i]='\0'; }*/
  if (FLAG_Inblock && FLAG_WasInBlock==0) 
    { 
      MFtableau[i++]='}'; 
      MFtableau[i]='\0'; 
      /* On re-ecris la formule purifiee dans le fichier auxiliaire. */
      fprintf(auxiliaire,"%s \\pzpendeq {%s",MFtableau,TABFmt[FLAG_Inblock]);
    }
  else
    {
      /* On re-ecris la formule purifiee dans le fichier auxiliaire. */
      if (FLAG_WasInBlock==-1)
	{
	  fprintf(auxiliaire,"%s \\pzpendeq }",MFtableau);
	}
      else
	{
	  fprintf(auxiliaire,"%s \\pzpendeq",MFtableau);
	}
    }
  free(MFtableau);
  fclose(auxiliaire);
  if ((auxiliaire=fopen(nomsort,"r"))==NULL)
    {
      fprintf(stderr,"FATAL ERROR: Could not open Auxiliary File:%s\n",nomsort);
      fclose(entree);
      fclose(sortie);
      exit(0);
    }
}