summaryrefslogtreecommitdiff
path: root/support/w2latex/rtf.formule.c
blob: 923aa63386adbde2789ed371e1e8b5f898fadc0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**************************************************************************************************
**	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);
    }
}