/*-----------------------------------------------------------------------------------*/ /* INRIA 2007 */ /* Allan CORNET */ /*-----------------------------------------------------------------------------------*/ #include "CallScilab.h" #ifdef WIN32 #include "../os_specific/win_mem_alloc.h" /* MALLOC */ #else #include "../os_specific/sci_mem_alloc.h" /* MALLOC */ #endif /*-----------------------------------------------------------------------------------*/ extern int C2F(scirun)(char * startup, int lstartup); /*-----------------------------------------------------------------------------------*/ static int RemoveCharsFromEOL(char *line,char CharToRemove); static int RemoveComments(char *line); static int CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs); static int SetLastJob(char *JOB); static char *lastjob=NULL; static int SendScilabJob2(char *job); /*-----------------------------------------------------------------------------------*/ /* see CallScilab.h more informations*/ /*-----------------------------------------------------------------------------------*/ static int SendScilabJob2(char *job) { int retCode = -1; int lencommand=0; static char *command=NULL; char ScirunCommand[]="Err=execstr(TMP_EXEC_STRING,\"errcatch\",\"n\");quit;"; char ClearTmpVariables[]="clear TMP_EXEC_STRING;clear Err;quit;"; lencommand=strlen(job); command=MALLOC(sizeof(char)*(lencommand+1)); if (command) { /* clear prev. Err , TMP_EXEC_STRING scilab variables */ C2F(scirun)(ClearTmpVariables,strlen(ClearTmpVariables)); strcpy(command,job); SetLastJob(command); /* Creation Variable temporaire dans Scilab */ if (!C2F(cwritechain)("TMP_EXEC_STRING",&lencommand,(char*)command,(int)strlen("TMP_EXEC_STRING"),(int)strlen(command)) ) { /* Probleme */ fprintf(stderr,"Error : SendScilabJob (1) 'TMP_EXEC_STRING'.\n"); retCode = -1; if (command) {FREE(command);command=NULL;} return retCode; } else { int m=0,n=0,lp=0; C2F(scirun)(ScirunCommand,strlen(ScirunCommand)); /* get error code from scilab */ if ( ! C2F(cmatptr)("Err", &m, &n, &lp,strlen("Err"))) { fprintf(stderr,"Error : SendScilabJob (2) 'Err'.\n"); retCode = -2; } else { if (m*n == 1) { double code = -1; ReadMatrix("Err", &m, &n, &code); retCode = (int) code ; } else { fprintf(stderr,"Error : SendScilabJob (3) 'Err'.\n"); retCode = -3; } } /* clear prev. Err , TMP_EXEC_STRING scilab variables */ C2F(scirun)(ClearTmpVariables,strlen(ClearTmpVariables)); } if (command) {FREE(command);command=NULL;} } else { fprintf(stderr,"Error : SendScilabJob (4) 'command' MALLOC.\n"); retCode = -4; } return retCode; } /*-----------------------------------------------------------------------------------*/ static int SetLastJob(char *JOB) { int bOK=0; if (lastjob) { FREE(lastjob);lastjob=NULL;} if (JOB) { lastjob = MALLOC(sizeof(char)*(strlen(JOB)+1)); if (lastjob) { strcpy(lastjob,JOB); bOK = 1; } } return bOK; } /*-----------------------------------------------------------------------------------*/ int GetLastJob(char *JOB,int nbcharsJOB) { int bOK=0; if (JOB) { if ((int)strlen(lastjob)0;l--) { if (line[l] == CharToRemove) { line[l] = '\0'; bOK=1; } else break; } return bOK; } /*-----------------------------------------------------------------------------------*/ static int RemoveComments(char *line) { int l=0; int bOK=0; int len=0; int idx=-1; len=strlen(line); for (l=len-1;l>0;l--) { if (line[l] == '/') { if (l-1>0) { if (line[l-1] == '/') { idx=l-1; l=l-2; } } } } if (idx>=0) line[idx]='\0'; bOK = 1; return bOK; } /*-----------------------------------------------------------------------------------*/ static int CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs) { int bOK=0; if (bufCommands) {FREE(bufCommands);bufCommands=NULL;} if (LOCALJOBS) { int i=0; for (i=0; i < numberjobs;i++) { if (LOCALJOBS[i]) {FREE(LOCALJOBS[i]);LOCALJOBS[i]=NULL;} } LOCALJOBS=NULL; } bOK=1; return bOK; } /*-----------------------------------------------------------------------------------*/