Slatexpr.c

Go to the documentation of this file.
00001 /* Copyright ENPC/Chancelier Jean-Philippe */
00002 
00003 #ifdef __STDC__
00004 # include <stdarg.h>
00005 #else
00006 # include <varargs.h>
00007 
00008 char *getenv();
00009 #endif 
00010 
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <ctype.h>
00014 #include <stdio.h>
00015 
00016 #include "../../modules/core/includes/machine.h"
00017 #include "util.h" 
00018 
00019 static int Sed __PARAMS((int,char *,FILE *,char *,char *,char *,char *,char *,char *));
00020 
00021 static void FileNameChange __PARAMS((char *filein,char *fileout,char *,char *));
00022 static  void dos2win32 __PARAMS((char *filename,char *filename1));
00023 int ScilabPsToTeX __PARAMS((char orientation,char *filenamein,char *filenameout,double xs,double ys));
00024 
00025 /**************************************************
00026  * S;latexpr  Usage :  [-orientation] xs ys filename.ps 
00027  * used to produce an EPSF file from a scilab postscript file 
00028  * without Postscript preamble
00029  **************************************************/
00030 
00031 char * UsageStr[]={
00032   "\tfilename.ps : Postscript file produced by Scilab\n",
00033   "\torientation : p[ortrait] or l[andscape]  \n",
00034   "fin"};
00035 
00036 char file1[256],file2[256];
00037 
00038 #ifdef _MSC_VER
00039 extern void SciEnv(void);
00040 #endif 
00041 
00042 int main(int argc, char **argv)
00043 {
00044   double xs=1.0,ys=1.0;
00045   char orientation='p';
00046 #ifdef _MSC_VER
00047   SciEnv();
00048 #endif 
00049   if (argc >= 6 || argc <= 3 )
00050     { int i=0;
00051       fprintf(stderr,"Usage  : %s [-orientation] xs ys filename.ps \n",argv[0]);
00052       while (strcmp(UsageStr[i],"fin")!=0)
00053           fprintf(stderr,"%s",UsageStr[i]),i++;
00054       return(1);
00055     }
00056   switch ( argc ) 
00057     {
00058     case 4 :
00059       if ( strncmp( argv[1] ,"-p",2) == 0 || ( strncmp( argv[1] ,"-l",2) == 0) )
00060         {
00061           fprintf(stderr,"Slatexpr: wrong number of arguments \n");
00062           return(1);
00063         }
00064       orientation = 'p';
00065       strcpy(file1,argv[3]);
00066       sscanf(argv[1],"%lf",&xs);
00067       sscanf(argv[2],"%lf",&ys);
00068       break;
00069     case 5:
00070       strcpy(file1,argv[4]);
00071       if ( strncmp( argv[1] ,"-p",2) == 0) 
00072         orientation = 'p';
00073       else if ( strncmp( argv[1] ,"-l",2) == 0) 
00074         orientation = 'l';
00075       else 
00076        fprintf(stderr,"Invalid first argument %s\n",argv[1]);
00077       sscanf(argv[2],"%lf",&xs);
00078       sscanf(argv[3],"%lf",&ys);
00079       break;
00080     }
00082   dos2win32(file1,file2);
00084   ScilabPsToTeX(orientation,file2,file1,xs,ys);
00085   return(0);
00086 }
00087 
00088 
00089 #ifdef __STDC__
00090 
00091 void sciprint(char *fmt, ...)
00092 {
00093   va_list args;
00094   va_start(args,fmt);
00095   vfprintf(stdout, fmt, args );
00096   va_end(args);
00097 }
00098 #else 
00099 
00100 /*VARARGS0*/
00101 void sciprint(va_alist) va_dcl
00102 {
00103   va_list ap;
00104   char *format;
00105   va_start(ap);
00106   format = va_arg(ap, char *);
00107   (void)  vfprintf(stdout, format, ap );
00108   va_end(ap);
00109 }
00110 #endif 
00111 
00112 
00113 static  void dos2win32(char *filename, char *filename1)
00114 {
00115 #ifdef CVT_PATH_BEG
00116   if ( filename[1] == ':' ) 
00117     {
00118       *filename1++ = '/';
00119       *filename1++ = '/';
00120       *filename1++ = *filename++;
00121       filename++;
00122     }
00123 #endif
00124   while ( *filename != '\0' ) 
00125     {
00126       *filename1++ = *filename++;
00127       if ( *(filename1-1) == '\\' ) *(filename1-1) = '/' ;
00128     }
00129   *filename1 = '\0';
00130 }
00131 
00135 /**************************************************
00136  * Converts a scilab Eps file to an Epsf file 
00137  * by adding a preamble and produces a tex file
00138  **************************************************/
00139 
00140 static char entete[256],base[54];
00141 
00142 int ScilabPsToTeX(char orientation, char *filein, char *fileout, double xs, double ys)
00143 {
00144   double wide,high,widecm,highcm,hscale,vscale;
00145   int flag = 0,rep;
00146   FILE *fo;
00147   char *env;
00148   env = getenv("SCI");
00149   if (env == NULL) 
00150     {
00151       sciprint("Environment variable SCI must be defined\n");
00152       return(1);
00153     }
00154 
00158   sprintf(entete,"%s/tools/printer/NperiPos.ps",env);
00159 
00160   FileNameChange(filein,fileout,base,"eps");
00161   if ( strcmp(fileout,filein) == 0 ) 
00162     {
00163       flag = 1;
00164       strcat(fileout,".temp");
00165     }
00166   
00167   if ( (fo = fopen(fileout,"w"))== 0 ) 
00168     {
00169       sciprint(" Can't open file %s\n",fileout);
00170       exit(1);
00171     }
00172 
00173   fprintf(fo,"%%!PS-Adobe-2.0 EPSF-2.0\n");
00174   if ( orientation == 'p' ) 
00175     fprintf(fo,"%%%%BoundingBox:  0 0 300 212\n");
00176   else 
00177     fprintf(fo,"%%%%BoundingBox:  0 0 212 300\n");
00178 
00179   Sed(0,entete,fo,"%!PS-ADOBE","%%",(char*) 0,(char *)0,(char*) 0,(char *)0);
00180 
00181   if ( orientation == 'p' )
00182         rep=Sed(1,filein,fo,(char*) 0,(char *)0,(char*) 0,(char *)0,
00183                 (char*) 0,(char *)0);
00184   else 
00185     
00186     rep=Sed(1,filein,fo,"[0.5 10 div 0 0 0.5 10 div neg  0 2120 10 div] concat",
00187         "90 rotate 0 600 neg translate [0.5 10 div 0 0 0.5 10 div neg  0 3000 5 div] concat",
00188         (char*) 0,(char *)0,(char*) 0,(char *)0);
00189   fclose(fo);
00190 
00191   if ( rep >= 1 ) 
00192     {
00193       if ( rep == 1) 
00194         sciprint("input file doesn't need to be converted to Epsf\n");
00195       remove(fileout);
00196       exit(0);
00197     }
00198   
00199   if ( flag == 1) 
00200     {
00202       fo = fopen(filein,"w");
00203       Sed(0,fileout,fo,(char*) 0,(char *)0,(char*) 0,(char *)0,(char*) 0,(char *)0);
00204       fclose(fo);
00205     }
00206   else
00207     {
00208       remove(filein);
00209     }
00210   
00213   if ( orientation == 'p' ) 
00214     {
00215       wide = 300 *xs;
00216       high = 212 *ys;
00217     }
00218   else 
00219     {
00220       wide = 212 *xs;
00221       high = 300 *ys;
00222     }
00223   widecm = wide *2.835;
00224   highcm = high *2.835;
00225   hscale = xs*100;
00226   vscale = ys*100;
00227 
00228   FileNameChange(filein,fileout,base,"tex");
00229   if ( (fo = fopen(fileout,"w"))== 0 ) 
00230     {
00231       sciprint(" Can't open file %s\n",fileout);
00232       exit(1);
00233     }
00234 #ifndef DOC
00235   fprintf(fo,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
00236   fprintf(fo,"%% Usage: -To include a Figure with a caption, insert the TWO following lines\n");
00237   fprintf(fo,"%%        in your Latex file:\n");
00238   fprintf(fo,"%% \\input{This_file_name} \n");
00239   fprintf(fo,"%% \\dessin{The_caption}{The_label}\n");
00240   fprintf(fo,"%%         -To include just a picture, insert the lines \n");
00241   fprintf(fo,"%%         between \\fbox{\\begin{picture}...  and \\end{picture}} below \n");
00242   fprintf(fo,"%%          \n");
00243   fprintf(fo,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
00244 
00245   fprintf(fo," \n");
00246 
00247   fprintf(fo," \\long\\def\\Checksifdef#1#2#3{%%\n");
00248   fprintf(fo,"\\expandafter\\ifx\\csname #1\\endcsname\\relax#2\\else#3\\fi}\n");
00249   fprintf(fo,"\\Checksifdef{Figdir}{\\gdef\\Figdir{}}{}\n");
00250   fprintf(fo,"\\def\\dessin#1#2{\n");
00251 #endif
00252   fprintf(fo,"\\begin{figure}[hbtp]\n");
00253   fprintf(fo,"\\begin{center}\n");
00254   fprintf(fo,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \n");
00255   fprintf(fo,"%%If you prefer cm, uncomment the following two lines\n");
00256   fprintf(fo,"%%\\setlength{\\unitlength}{1mm}\n");
00257   fprintf(fo,"%%\\fbox{\\begin{picture}(%.2f,%.2f)\n",widecm,highcm);
00258   fprintf(fo,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \n");
00259   fprintf(fo,"\\fbox{\\begin{picture}(%.2f,%.2f)\n",wide,high);
00260   FileNameChange(filein,fileout,base,"eps");
00261 #ifdef EPSFIG
00262   fprintf(fo,"\\epsfig{file=\\Figdir %s.eps,width=%.2fpt,height=%.2fpt}\n",
00263           base,wide,high);
00264 #else
00265   fprintf(fo,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
00266   fprintf(fo,"%% If you want do not want to use epsfig ,comment the following line \n");
00267   fprintf(fo,"%% and uncomment the \\special line \n");
00268   fprintf(fo,"\\epsfig{file=\\Figdir %s.eps,width=%.2fpt,height=%.2fpt}\n",
00269           base,wide,high);
00270   fprintf(fo,"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
00271   fprintf(fo,"%%\\special{psfile=\\Figdir %s.eps hscale=%.2f vscale=%.2f}\n",
00272           base,hscale,vscale);
00273 #endif
00274   fprintf(fo,"\\end{picture}}\n");
00275   fprintf(fo,"\\end{center}\n");
00276 #ifndef DOC 
00277   fprintf(fo,"\\caption{\\label{#2}#1}\n");
00278   fprintf(fo,"\\end{figure}}\n");
00279 #endif
00280   fclose(fo);
00281   return(0);
00282 }
00283 
00284 /*****************************************
00285  * xxx/xxx/xxx/ffff.zzz -->  ffff.syf 
00286  * and name = "ffff"
00287  ****************************************/
00288 
00289 static void FileNameChange(char *filein, char *fileout, char *name, char *suf)
00290 {
00291   char *p=filein,*p1;
00292   p1=strchr(p,'/');
00293   while ( p1 != 0 ) 
00294     {
00295       p=p1+1;
00296       p1 = strchr(p,'/');
00297     }
00298   p1 = strchr(p,'.');
00299   if ( p1 != 0 ) 
00300     {
00301       *p1 = '\0';
00302       sprintf(fileout,"%s.%s",filein,suf);
00303       sprintf(base,"%s",p);
00304       *p1 = '.';
00305     }
00306   else 
00307     {
00308       sprintf(fileout,"%s.%s",filein,suf);
00309       sprintf(base,"%s",p);
00310     }
00311   /* 
00312      sciprint("[%s]=>[%s]\n",filein,fileout); 
00313      sciprint("real base %s\n",base);
00314      */
00315 }
00316 
00317 
00318 /**************************************************
00319  * copies file to fileo performing some substitutions 
00320  **************************************************/
00321 
00322 static int Sed(int flag, char *file, FILE *fileo, char *strin1, char *strout1, char *strin2, char *strout2, char *strin3, char *strout3)
00323 {
00324   FILE *fd;
00325 
00326   static char *buff = NULL;
00327   static int buflen = 512;
00328   if ( buff == NULL) 
00329     {
00330       buff = malloc(buflen*sizeof(char));
00331       if ( buff == NULL) 
00332         {
00333           fprintf(stderr,"Running out of space \n");
00334           exit(1);
00335         }
00336     }
00337 
00338   fd=fopen(file,"r");
00339   if (fd != 0)
00340     { int stop=0;
00341       while ( stop != 1)
00342         { 
00343            read_one_line (&buff,&stop,fd,&buflen); 
00344            if ( flag == 1 ) 
00345              {
00346                if ( strncmp(buff,"%!PS-Adobe-2.0 EPSF-2.0",
00347                             strlen("%!PS-Adobe-2.0 EPSF-2.0"))== 0)
00348                  {
00349                    fclose(fd);
00350                    return(1);
00351                  }
00352              }
00353            if ( strin1 != (char *) 0 && strncmp(buff,strin1,strlen(strin1))==0)
00354              fprintf(fileo,"%s\n",strout1);
00355            else
00356              {
00357                if (  strin2 != (char *) 0 && strncmp(buff,strin2,strlen(strin2))==0)
00358                  fprintf(fileo,"%s\n",strout2);
00359                else 
00360                  {
00361                    if ( strin3 != (char *) 0 && strncmp(buff,strin3,strlen(strin3))==0)
00362                      fprintf(fileo,"%s\n",strout3);
00363                    else
00364                      fprintf(fileo,"%s",buff);
00365                  }
00366              }
00367          }
00368       fclose(fd);
00369     }
00370   else 
00371     {
00372       sciprint("file %s not found \n",file);
00373       return(2);
00374     }
00375   return(0);
00376 }
00377 

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