wmtex.c

Go to the documentation of this file.
00001 /* Copyright (C) 1998-2002 Chancelier Jean-Philippe */
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <ctype.h>
00005 #include <stdio.h>
00006 #include "machine.h"
00007 
00008 #include "wcommon.h" 
00009 
00010 #include "Messages.h"
00011 #include "Warnings.h"
00012 #include "Errors.h"
00013 
00014 
00015 static int Sed __PARAMS ((int, char *, FILE *, char *, char *, char *, char *, char *, char *));
00016 static void readOneLine __PARAMS ((char *buff, int *stop, FILE * fd));
00017 static void FileNameChange __PARAMS ((char *filein, char *fileout, char *, char *));
00018 
00019 extern char *getSCIpath(void);
00020 
00021 /**************************************************
00022  * Converts a scilab Eps file to an Epsf file 
00023  * by adding a preamble and produces a tex file
00024  **************************************************/
00025 
00026 static char entete[256], base[54];
00027 
00028 int ScilabPsToTeX (char orientation, char *filein, char *fileout, double xs, double ys)
00029 {
00030   double wide, high, widecm, highcm, hscale, vscale;
00031   int flag = 0, rep;
00032   FILE *fo;
00033   char *env;
00034   env = getSCIpath();
00035   if (env == NULL)
00036     {
00037       sciprint (MSG_ERROR49);
00038       return (1);
00039     }
00040 
00044   sprintf (entete, "%s/tools/printer/NperiPos.ps", env);
00045 
00046   FileNameChange (filein, fileout, base, "eps");
00047   if (strcmp (fileout, filein) == 0)
00048     {
00049       flag = 1;
00050       strcat (fileout, ".temp");
00051     }
00052 
00053   if ((fo = fopen (fileout, "w")) == 0)
00054     {
00055       sciprint (MSG_ERROR50, fileout);
00056       return 1;
00057     }
00058 
00059   fprintf (fo, "%%!PS-Adobe-2.0 EPSF-2.0\n");
00060   if (orientation == 'p')
00061     fprintf (fo, "%%%%BoundingBox:  0 0 300 212\n");
00062   else
00063     fprintf (fo, "%%%%BoundingBox:  0 0 212 300\n");
00064 
00065   Sed (0, entete, fo, "%!PS-ADOBE", "%%", (char *) 0, (char *) 0, (char *) 0, (char *) 0);
00066 
00067   if (orientation == 'p')
00068     rep = Sed (1, filein, fo, (char *) 0, (char *) 0, (char *) 0, (char *) 0,
00069                (char *) 0, (char *) 0);
00070   else
00071     rep = Sed (1, filein, fo, "[0.5 10 div 0 0 0.5 10 div neg  0 2120 10 div] concat",
00072                "90 rotate 0 600 neg translate [0.5 10 div 0 0 0.5 10 div neg  0 3000 5 div] concat",
00073                (char *) 0, (char *) 0, (char *) 0, (char *) 0);
00074   fclose (fo);
00075 
00076   if (rep >= 1)
00077     {
00078       if (rep == 1)
00079         sciprint (MSG_ERROR51);
00080       remove (fileout);
00081       return 0;
00082     }
00083 
00084   if (flag == 1)
00085     {
00087       fo = fopen (filein, "w");
00088       Sed (0, fileout, fo, (char *) 0, (char *) 0, (char *) 0, (char *) 0, (char *) 0, (char *) 0);
00089       fclose (fo);
00090     }
00091   else
00092     {
00093       remove (filein);
00094     }
00095 
00098   if (orientation == 'p')
00099     {
00100       wide = 300 * xs;
00101       high = 212 * ys;
00102     }
00103   else
00104     {
00105       wide = 212 * xs;
00106       high = 300 * ys;
00107     }
00108   widecm = wide * 2.835;
00109   highcm = high * 2.835;
00110   hscale = xs * 100;
00111   vscale = ys * 100;
00112 
00113   FileNameChange (filein, fileout, base, "tex");
00114   if ((fo = fopen (fileout, "w")) == 0)
00115     {
00116       sciprint (MSG_ERROR51, fileout);
00117       return 1;
00118     }
00119 #ifndef DOC
00120 
00121   fprintf (fo, "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
00122   fprintf (fo, MSG_SCIMSG46);
00123   fprintf (fo, MSG_SCIMSG47);
00124   fprintf (fo, MSG_SCIMSG48);
00125   fprintf (fo, MSG_SCIMSG49);
00126   fprintf (fo, MSG_SCIMSG50);
00127   fprintf (fo, MSG_SCIMSG51);
00128   fprintf (fo, "%%          \n");
00129   fprintf (fo, "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
00130 
00131   fprintf (fo, " \n");
00132 
00133 
00134   fprintf (fo, " \\long\\def\\Checksifdef#1#2#3{%%\n");
00135   fprintf (fo, "\\expandafter\\ifx\\csname #1\\endcsname\\relax#2\\else#3\\fi}\n");
00136   fprintf (fo, "\\Checksifdef{Figdir}{\\gdef\\Figdir{}}{}\n");
00137   fprintf (fo, "\\def\\dessin#1#2{\n");
00138 #endif
00139   fprintf (fo, "\\begin{figure}[hbtp]\n");
00140   fprintf (fo, "\\begin{center}\n");
00141   fprintf (fo, MSG_SCIMSG52);
00142   fprintf (fo, "%%\\setlength{\\unitlength}{1mm}\n");
00143   fprintf (fo, "%%\\fbox{\\begin{picture}(%.2f,%.2f)\n", widecm, highcm);
00144   fprintf (fo, "\\fbox{\\begin{picture}(%.2f,%.2f)\n", wide, high);
00145   FileNameChange (filein, fileout, base, "eps");
00146 #ifdef EPSFIG
00147   fprintf (fo, "\\epsfig{file=\\Figdir %s.eps,width=%.2fpt,height=%.2fpt}\n",
00148            base, wide.high);
00149 #else
00150   fprintf (fo, "%% if you want to use epsfig uncomment the following line \n");
00151   fprintf (fo, "%% and comment the special line \n");
00152   fprintf (fo, "%%\\epsfig{file=\\Figdir %s.eps,width=%.2fpt,height=%.2fpt}\n",
00153            base, wide, high);
00154   fprintf (fo, "\\special{psfile=\\Figdir %s.eps hscale=%.2f vscale=%.2f}\n",
00155            base, hscale, vscale);
00156 #endif
00157   fprintf (fo, "\\end{picture}}\n");
00158   fprintf (fo, "\\end{center}\n");
00159 #ifndef DOC
00160   fprintf (fo, "\\caption{\\label{#2}#1}\n");
00161   fprintf (fo, "\\end{figure}}\n");
00162 #endif
00163   fclose (fo);
00164   return (0);
00165 }
00166 
00167 /*****************************************
00168  * xxx/xxx/xxx/ffff.zzz -->  ffff.syf 
00169  * and name = "ffff"
00170  ****************************************/
00171 
00172 static void 
00173 FileNameChange (filein, fileout, name, suf)
00174      char *filein, *fileout, *name, *suf;
00175 {
00176   char *p = filein, *p1;
00177   p1 = strchr (p, '/');
00178   while (p1 != 0)
00179     {
00180       p = p1 + 1;
00181       p1 = strchr (p, '/');
00182     }
00183   p1 = strchr (p, '.');
00184   if (p1 != 0)
00185     {
00186       *p1 = '\0';
00187       sprintf (fileout, "%s.%s", filein, suf);
00188       sprintf (base, "%s", p);
00189       *p1 = '.';
00190     }
00191   else
00192     {
00193       sprintf (fileout, "%s.%s", filein, suf);
00194       sprintf (base, "%s", p);
00195     }
00196   /* 
00197      sciprint("[%s]=>[%s]\n",filein,fileout); 
00198      sciprint("real base %s\n",base);
00199    */
00200 }
00201 
00202 
00203 /**************************************************
00204  * copies file to fileo performing some substitutions 
00205  **************************************************/
00206 
00207 static int 
00208 Sed (flag, file, fileo, strin1, strout1, strin2, strout2, strin3, strout3)
00209      char file[], strin1[], strout1[], strout3[];
00210      char strin2[], strout2[], strin3[];
00211      FILE *fileo;
00212      int flag;
00213 {
00214   FILE *fd;
00215   fd = fopen (file, "r");
00216   if (fd != 0)
00217     {
00218       int stop = 0;
00219       while (stop != 1)
00220         {
00221           char buff[512];
00222           readOneLine (buff, &stop, fd);
00223           if (flag == 1)
00224             {
00225               if (strncmp (buff, "%!PS-Adobe-2.0 EPSF-2.0",
00226                            strlen ("%!PS-Adobe-2.0 EPSF-2.0")) == 0)
00227                 {
00228                   fclose (fd);
00229                   return (1);
00230                 }
00231             }
00232           if (strin1 != (char *) 0 && strncmp (buff, strin1, strlen (strin1)) == 0)
00233             fprintf (fileo, "%s\n", strout1);
00234           else
00235             {
00236               if (strin2 != (char *) 0 && strncmp (buff, strin2, strlen (strin2)) == 0)
00237                 fprintf (fileo, "%s\n", strout2);
00238               else
00239                 {
00240                   if (strin3 != (char *) 0 && strncmp (buff, strin3, strlen (strin3)) == 0)
00241                     fprintf (fileo, "%s\n", strout3);
00242                   else
00243                     fprintf (fileo, "%s", buff);
00244                 }
00245             }
00246         }
00247       fclose (fd);
00248     }
00249   else
00250     {
00251       sciprint (MSG_ERROR52, file);
00252       return (2);
00253     }
00254   return (0);
00255 }
00256 
00257 /*-----------------------------------------------
00258   lit une ligne dans fd et la stocke dans buff
00259 ---------------------------------------------------*/
00260 
00261 static void 
00262 readOneLine (buff, stop, fd)
00263      char buff[];
00264      int *stop;
00265      FILE *fd;
00266 {
00267   int i, c;
00268   for (i = 0; (c = getc (fd)) != '\n' && c != EOF; i++)
00269     buff[i] = c;
00270   buff[i] = '\n';
00271   buff[i + 1] = '\0';
00272   if (c == EOF)
00273     {
00274       *stop = 1;
00275     }
00276 }

Generated on Sun Mar 4 15:03:55 2007 for Scilab [trunk] by  doxygen 1.5.1