SendScilabJobs.c

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------------*/
00002 /* INRIA 2007 */
00003 /* Allan CORNET */
00004 /*-----------------------------------------------------------------------------------*/
00005 #include "CallScilab.h"
00006 #include "MALLOC.h"
00007 #include "scirun.h"
00008 /*-----------------------------------------------------------------------------------*/
00009 static BOOL RemoveCharsFromEOL(char *line,char CharToRemove);
00010 static BOOL RemoveComments(char *line);
00011 static BOOL CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs);
00012 static BOOL SetLastJob(char *JOB);
00013 static char *lastjob=NULL;
00014 /*-----------------------------------------------------------------------------------*/
00015 /* see CallScilab.h more informations*/
00016 /*-----------------------------------------------------------------------------------*/
00017 int SendScilabJob(char *job)
00018 {
00019         int retCode = -1;
00020         int lencommand=0;
00021         static char *command=NULL;
00022         
00023         char ScirunCommand[]="Err=execstr(TMP_EXEC_STRING,\"errcatch\",\"n\");quit;";
00024         char ClearTmpVariables[]="clear TMP_EXEC_STRING;clear Err;quit;";
00025         
00026         lencommand=strlen(job);
00027         command=MALLOC(sizeof(char)*(lencommand+1));
00028 
00029         if (command)
00030         {
00031                 /* clear prev. Err , TMP_EXEC_STRING scilab variables */
00032                 C2F(scirun)(ClearTmpVariables,strlen(ClearTmpVariables));
00033 
00034                 strcpy(command,job);
00035                 SetLastJob(command);
00036 
00037                 /* Creation Variable temporaire dans Scilab */
00038                 if (!C2F(cwritechain)("TMP_EXEC_STRING",&lencommand,(char*)command,(int)strlen("TMP_EXEC_STRING"),(int)strlen(command)) )
00039                 {
00040                         /* Probleme */
00041                         fprintf(stderr,"Error : SendScilabJob (1) 'TMP_EXEC_STRING'.\n");
00042                         retCode = -1;
00043 
00044                         if (command) {FREE(command);command=NULL;}
00045                         return retCode;
00046                 }
00047                 else
00048                 {
00049                         int m=0,n=0,lp=0;
00050 
00051                         C2F(scirun)(ScirunCommand,strlen(ScirunCommand));
00052 
00053                         /* get error code from scilab */
00054 
00055                         if ( ! C2F(cmatptr)("Err", &m, &n, &lp,strlen("Err")))
00056                         {
00057                                 fprintf(stderr,"Error : SendScilabJob (2) 'Err'.\n");
00058                                 retCode = -2;
00059                         }
00060                         else
00061                         {
00062                                 if (m*n == 1)
00063                                 {
00064                                         double code = -1;
00065                                         ReadMatrix("Err", &m, &n, &code);
00066                                         retCode = (int) code ;
00067                                 }
00068                                 else 
00069                                 {
00070                                         fprintf(stderr,"Error : SendScilabJob (3) 'Err'.\n");   
00071                                         retCode = -3;
00072                                 }
00073                         }
00074 
00075                         /* clear prev. Err , TMP_EXEC_STRING scilab variables */
00076                         C2F(scirun)(ClearTmpVariables,strlen(ClearTmpVariables));
00077                 }
00078                 if (command) {FREE(command);command=NULL;}
00079         }
00080         else
00081         {
00082                 fprintf(stderr,"Error : SendScilabJob (4) 'command' MALLOC.\n");
00083                 retCode = -4;
00084         }
00085 
00086         return retCode;
00087 }
00088 /*-----------------------------------------------------------------------------------*/
00089 static BOOL SetLastJob(char *JOB)
00090 {
00091         BOOL bOK=FALSE;
00092         
00093         if (lastjob) { FREE(lastjob);lastjob=NULL;}
00094 
00095         if (JOB)
00096         {
00097                 lastjob = MALLOC(sizeof(char)*(strlen(JOB)+1)); 
00098                 if (lastjob)
00099                 {
00100                         strcpy(lastjob,JOB);
00101                         bOK =TRUE;
00102                 }
00103         }
00104         return bOK;
00105 }
00106 /*-----------------------------------------------------------------------------------*/
00107 BOOL GetLastJob(char *JOB,int nbcharsJOB)
00108 {
00109         BOOL bOK=FALSE;
00110         if (JOB)
00111         {
00112                 if ((int)strlen(lastjob)<nbcharsJOB)
00113                 {
00114                         strcpy(JOB,lastjob);
00115                 }
00116                 else strncpy(JOB,lastjob,nbcharsJOB);
00117                 bOK=TRUE;
00118         }
00119         return bOK;
00120 }
00121 /*-----------------------------------------------------------------------------------*/
00122 int SendScilabJobs(char **jobs,int numberjobs)
00123 {
00124         #define BufferSecuritySize 64
00125 
00126         int retcode=-10;
00127 
00128         if (jobs)
00129         {
00130                 int i=0;
00131                 int nbcharsjobs=0;
00132                 char *bufCommands=NULL;
00133                 char **LOCALJOBS=NULL;
00134 
00135                 int jobsloop=0;
00136 
00137                 LOCALJOBS=(char**)MALLOC(sizeof(char*)*numberjobs);
00138 
00139                 if (LOCALJOBS)
00140                 {
00141                         for (i=0;i<numberjobs;i++)
00142                         {
00143                                 if (jobs[i])
00144                                 {
00145                                         nbcharsjobs = nbcharsjobs+(int)strlen(jobs[i]);
00146                                         LOCALJOBS[i]=(char*)MALLOC( sizeof(char)*(strlen(jobs[i])+BufferSecuritySize) );
00147                                         if (LOCALJOBS[i])
00148                                         {
00149                                                 strcpy(LOCALJOBS[i],jobs[i]);
00150                                         }
00151                                         else
00152                                         {
00153                                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
00154                                                 fprintf(stderr,"Error : SendScilabJobs (1) 'LOCALJOBS[%d] MALLOC'.\n",i);       
00155                                                 return retcode;
00156                                         }
00157                                 }
00158                                 else
00159                                 {
00160                                         fprintf(stderr,"Error : SendScilabJobs (2) 'jobs[%d] == NULL'.\n",i);   
00161                                         return retcode;
00162                                 }
00163                         }
00164 
00165                         bufCommands = (char*)MALLOC( sizeof(char)*(nbcharsjobs+numberjobs+BufferSecuritySize) );
00166 
00167                         if (bufCommands)
00168                         {
00169                                 strcpy(bufCommands,"");
00170 
00171                                 for (jobsloop=0;jobsloop<numberjobs;jobsloop++)
00172                                 {
00173                                         if (jobs[jobsloop])
00174                                         {
00175                                                 char *currentline=NULL;
00176                                                 BOOL AddSemiColon;
00177 
00178                                                 if (jobsloop == 0) AddSemiColon=FALSE;
00179                                                 else  AddSemiColon=TRUE;
00180 
00181                                 DOTDOTLOOP:
00182                                                 currentline=LOCALJOBS[jobsloop];
00183 
00184                                                 RemoveCharsFromEOL(currentline,'\n');
00185                                                 RemoveComments(currentline);
00186                                                 RemoveCharsFromEOL(currentline,' ');
00187 
00188                                                 if (RemoveCharsFromEOL(currentline,'.'))
00189                                                 {
00190                                                         RemoveCharsFromEOL(currentline,' ');
00191                                                         strcat(bufCommands,currentline);
00192                                                         jobsloop++;
00193                                                         AddSemiColon=FALSE;
00194                                                         goto DOTDOTLOOP;
00195                                                 }
00196                                                 else
00197                                                 {
00198                                                         if (!AddSemiColon)
00199                                                         {
00200                                                                 AddSemiColon=TRUE;
00201                                                                 strcat(currentline,";");
00202                                                         }
00203                                                         else
00204                                                         {
00205                                                                 strcat(bufCommands,";");
00206                                                         }
00207                                                         
00208                                                         strcat(bufCommands,currentline);
00209                                                 }
00210                                         }
00211                                 }
00212 
00213                                 retcode=SendScilabJob(bufCommands);
00214 
00215                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
00216                         }
00217                         else
00218                         {
00219                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
00220                                 fprintf(stderr,"Error : SendScilabJobs (3) 'bufCommands MALLOC'.\n");   
00221                                 return retcode;
00222                         }
00223                 }
00224                 else
00225                 {
00226                         CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
00227                         fprintf(stderr,"Error : SendScilabJobs (4) 'LOCALJOBS == NULL'.\n");    
00228                         return retcode;
00229                 }
00230         }
00231         else
00232         {
00233                 fprintf(stderr,"Error : SendScilabJobs (5) 'jobs == NULL'.\n"); 
00234                 retcode = -10;
00235         }
00236         
00237         return retcode;
00238 }
00239 /*-----------------------------------------------------------------------------------*/
00240 static BOOL RemoveCharsFromEOL(char *line,char CharToRemove)
00241 {
00242         int l=0;
00243         BOOL bOK=FALSE;
00244         int len=0;
00245 
00246         len=strlen(line);
00247         for (l=len-1;l>0;l--)
00248         {
00249                 if (line[l] == CharToRemove)
00250                 {
00251                         line[l] = '\0';
00252                         bOK=TRUE;
00253                 }
00254                 else break;
00255         }
00256         return bOK;
00257 }
00258 /*-----------------------------------------------------------------------------------*/
00259 static BOOL RemoveComments(char *line)
00260 {
00261         int l=0;
00262         BOOL bOK=FALSE;
00263         int len=0;
00264         int idx=-1;
00265 
00266         len=strlen(line);
00267         for (l=len-1;l>0;l--)
00268         {
00269                 if (line[l] == '/') 
00270                 {
00271                         if (l-1>0)
00272                         {
00273                                 if (line[l-1] == '/') 
00274                                 {
00275                                         idx=l-1;
00276                                         l=l-2;
00277                                 }
00278                         }
00279                 }
00280         }
00281 
00282         if (idx>=0) line[idx]='\0';
00283         
00284         return bOK;
00285 }
00286 /*-----------------------------------------------------------------------------------*/
00287 static BOOL CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs)
00288 {
00289         BOOL bOK=FALSE;
00290 
00291         if (bufCommands) {FREE(bufCommands);bufCommands=NULL;}
00292 
00293         if (LOCALJOBS)
00294         {
00295                 int i=0;
00296                 for (i=0; i < numberjobs;i++)
00297                 {
00298                         if (LOCALJOBS[i]) {FREE(LOCALJOBS[i]);LOCALJOBS[i]=NULL;}
00299                 }
00300                 LOCALJOBS=NULL;
00301         }
00302 
00303         bOK=TRUE;
00304         
00305         return bOK;
00306 
00307 }
00308 /*-----------------------------------------------------------------------------------*/

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