wmprint.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 
00007 #include "machine.h"
00008 
00009 #include "Messages.h"
00010 #include "Warnings.h"
00011 #include "Errors.h"
00012 #include "setgetSCIpath.h"
00013 #include "sciprint.h"
00014 
00015 
00016 static int Sed __PARAMS ((int, char *, FILE *, char *, char *, char *, char *, char *, char *));
00017 static void readOneLine __PARAMS ((char *buff, int *stop, FILE * fd));
00018 static void ConvertName __PARAMS ((char *filein, char *fileout));
00019 
00020 /**************************************************
00021  * Converts a scilab Eps file to an Epsf file 
00022  * by adding a preamble 
00023  **************************************************/
00024 
00025 static char entete[256];
00026 
00027 int ScilabPsToEps (char orientation, char *filein, char *fileout)
00028 {
00029   int flag = 0, rep;
00030   FILE *fo;
00031   char *env;
00032   env = getSCIpath();
00033   if (env == NULL)
00034     {
00035       sciprint (MSG_ERROR49);
00036       return (1);
00037     }
00038   sprintf (entete, "%s/tools/printer/NperiPos.ps", env);
00039 
00040   ConvertName (filein, fileout);
00041   if (strcmp (fileout, filein) == 0)
00042     {
00043       flag = 1;
00044       strcat (fileout, ".temp");
00045     }
00046 
00047   if ((fo = fopen (fileout, "w")) == 0)
00048     {
00049       sciprint (MSG_ERROR50, fileout);
00050       return 1;
00051     }
00052 
00053   fprintf (fo, "%%!PS-Adobe-2.0 EPSF-2.0\n");
00054   if (orientation == 'p')
00055     fprintf (fo, "%%%%BoundingBox: 0 200 600 624\n");
00056   else
00057     fprintf (fo, "%%%%BoundingBox: 0 0 600 780\n");
00058 
00059   Sed (0, entete, fo, "%!PS-ADOBE", "%%", (char *) 0, (char *) 0, (char *) 0, (char *) 0);
00060 
00061   if (orientation == 'p')
00062     rep = Sed (1, filein, fo, "[0.5 10 div 0 0 0.5 10 div neg  0 2120 10 div] concat",
00063                "[0.5 5 div 0 0 0.5 5 div neg  0 3120 5 div] concat",
00064                (char *) 0, (char *) 0, (char *) 0, (char *) 0);
00065   else
00066     rep = Sed (1, filein, fo, "[0.5 10 div 0 0 0.5 10 div neg  0 2120 10 div] concat",
00067                "90 rotate 10 640 neg translate [0.5 5 div 0 0 0.5 5 div neg  0 3120 5 div] concat",
00068                (char *) 0, (char *) 0, (char *) 0, (char *) 0);
00069   fclose (fo);
00070 
00071   if (rep >= 1)
00072     {
00073       if (rep == 1)
00074         sciprint (MSG_ERROR51);
00075       remove (fileout);
00076       return (0);
00077     }
00078 
00079   if (flag == 1)
00080     {
00082       fo = fopen (filein, "w");
00083       Sed (0, fileout, fo, (char *) 0, (char *) 0, (char *) 0, (char *) 0, (char *) 0, (char *) 0);
00084       fclose (fo);
00085     }
00086   else
00087     {
00088       remove (filein);
00089     }
00090   return (0);
00091 }
00092 
00093 
00094 static void 
00095 ConvertName (filein, fileout)
00096      char *filein, *fileout;
00097 {
00098   char *p = filein, *p1;
00099   p1 = strchr (p, '/');
00100   while (p1 != 0)
00101     {
00102       p = p1 + 1;
00103       p1 = strchr (p, '/');
00104     }
00105   p = strchr (p, '.');
00106   if (p != 0)
00107     {
00108       *p = '\0';
00109       sprintf (fileout, "%s.eps", filein);
00110       *p = '.';
00111     }
00112   else
00113     sprintf (fileout, "%s.eps", filein);
00115 }
00116 
00117 
00118 /**************************************************
00119  * copies file to fileo performing some substitutions 
00120  **************************************************/
00121 
00122 static int 
00123 Sed (flag, file, fileo, strin1, strout1, strin2, strout2, strin3, strout3)
00124      char file[], strin1[], strout1[], strout3[];
00125      char strin2[], strout2[], strin3[];
00126      FILE *fileo;
00127      int flag;
00128 {
00129   FILE *fd;
00130   fd = fopen (file, "r");
00131   if (fd != 0)
00132     {
00133       int stop = 0;
00134       while (stop != 1)
00135         {
00136           char buff[512];
00137           readOneLine (buff, &stop, fd);
00138           if (flag == 1)
00139             {
00140               if (strncmp (buff, "%!PS-Adobe-2.0 EPSF-2.0",
00141                            strlen ("%!PS-Adobe-2.0 EPSF-2.0")) == 0)
00142                 {
00143                   fclose (fd);
00144                   return (1);
00145                 }
00146             }
00147           if (strin1 != (char *) 0 && strncmp (buff, strin1, strlen (strin1)) == 0)
00148             fprintf (fileo, "%s\n", strout1);
00149           else
00150             {
00151               if (strin2 != (char *) 0 && strncmp (buff, strin2, strlen (strin2)) == 0)
00152                 fprintf (fileo, "%s\n", strout2);
00153               else
00154                 {
00155                   if (strin3 != (char *) 0 && strncmp (buff, strin3, strlen (strin3)) == 0)
00156                     fprintf (fileo, "%s\n", strout3);
00157                   else
00158                     fprintf (fileo, "%s", buff);
00159                 }
00160             }
00161         }
00162       fclose (fd);
00163     }
00164   else
00165     {
00166       sciprint (MSG_ERROR52, file);
00167       return (2);
00168     }
00169   return (0);
00170 }
00171 
00172 /*-----------------------------------------------
00173   lit une ligne dans fd et la stocke dans buff
00174 ---------------------------------------------------*/
00175 
00176 static void 
00177 readOneLine (buff, stop, fd)
00178      char buff[];
00179      int *stop;
00180      FILE *fd;
00181 {
00182   int i, c;
00183   for (i = 0; (c = getc (fd)) != '\n' && c != EOF; i++)
00184     buff[i] = c;
00185   buff[i] = '\n';
00186   buff[i + 1] = '\0';
00187   if (c == EOF)
00188     {
00189       *stop = 1;
00190     }
00191 }

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