/* sci_buf Scicos scilab global variable memory block * Type 4 simulation function ver 1.0 - scilab-4.1 * 29 Mars 2007 - INRIA - Author : A.Layec */ /* REVISION HISTORY : * $Log$ */ #include <string.h> #include "stack-c.h" #include "scicos_block.h" #include "machine.h" extern void str2sci(char** x,int n,int m); extern C2F(str2name)(); extern C2F(stackg)(); integer id[nsiz]; /* cregvar * * create a global scilab variable * in the stack * * input : varname : the variable name * m,n : the size of the variable * init : a ptr for the initial value * * output : return an error number * if any */ int cregvar(char **varname, int m, int n) { int i; int j; int k; int nbvars_sav; double *init; static char gname[] = "global" ; nbvars_sav=Nbvars; CreateVarFromPtr(1 + Rhs,"S",(i=1,&i),(j=1,&j),varname); SciString((i=Rhs+1,&i),gname,(j=1,&j),(k=1,&k)); Nbvars=0; /*allocation of initial value*/ if ((init=(double *) scicos_malloc(m*n*sizeof(double))) == NULL) { set_block_error(-16); return(0); } for(i=0;i<m*n;i++) init[i]=0; WriteMatrix(varname[0], &m, &n, init); scicos_free(init); Nbvars=nbvars_sav; return(0); } /* checkidgvar (not used) * input : varname * output : return an error number * if any */ int checkidgvar(char **varname) { int len; len=strlen(varname[0]); C2F(str2name)(varname[0], id, len); Fin = -1; C2F(stackg)(id); if (Fin == 0) { /**/ } else { if (*Infstk(Fin ) == 2) { /*Fin = *istk(iadr(*Lstk(Fin )) + 1 +1);*/ } else { /**/ } } return(0); } /* getidgvar * * retrieve scilab variable Lstk * value * * input : varname * output : return an error number * if any */ int getidgvar(char **varname) { int il; int len; len=strlen(varname[0]); C2F(str2name)(varname[0], id, len); Fin = -1; C2F(stackg)(id); if (Fin == 0) { /**/ } else { if (*Infstk(Fin ) == 2) { Fin = *istk(iadr(*Lstk(Fin )) + 1 +1); } else { /**/ } } il = iadr(*Lstk(Fin)); return(sadr(il+4)); } /* TODO * * ipar[0] : length of the buffer * ipar[1] : with event value (0: no, 1:yes) * ipar[2] : length of the variable name * ipar[3..3+ipar[2]-1] : code of the variable name * * discrete state : z[0] : buffer index counter */ /*work structure of that block*/ typedef struct work_st { char **varname; /*name of the scilab variable*/ } work_struct; /*prototype*/ void sci_buf(scicos_block *block,int flag) { /*local variable*/ int i,j,k; int m; int n; int evt; int ret; int gptr; /*ptr of the struct of that block*/ work_struct *work; /*ptr of the struct of that block*/ work = (work_struct*) *block->work; /*get with_evt */ evt = block->ipar[1]; /*get size of buffer*/ m=block->ipar[0]; n=block->nin; if(evt==1) n++; /*init*/ if(flag==4) { /*allocation*/ if ((work=(work_struct*) /*my_st*/ scicos_malloc(sizeof(work_struct))) == NULL) { set_block_error(-16); return; } if ((work->varname=(char**) /*my_st*/ scicos_malloc(sizeof(char*))) == NULL) { set_block_error(-16); return; } if ((work->varname[0]=(char*) /*my_st*/ scicos_malloc(sizeof(char)*(block->ipar[2]+1))) == NULL) { set_block_error(-16); return; } /*store my_st in *block->work*/ *block->work=(void *)work; /* check inputs size */ if (block->insz[0]>block->ipar[0]) { Scierror(888,"Size of input is > of the buffer size.\n"); set_block_error(-3); return; } /**/ C2F(cvstr)(&block->ipar[2],&block->ipar[3],work->varname[0],(j=1,&j),\ (unsigned long)block->ipar[2]); work->varname[0][block->ipar[2]]='\0'; /*create global scilab variable*/ ret=cregvar(work->varname,m,n); } /*store only in flag 2*/ else if(flag==2) { j=(int)block->z[0]; gptr = getidgvar(work->varname); if (j+block->insz[0]<m) { if(evt==1) { for(i=0;i<block->insz[0];i++) { *stk(gptr+j+i)=get_scicos_time(); } for(k=0;k<block->nin;k++) { for(i=0;i<block->insz[0];i++) { *stk(k*m+m+gptr+j+i)=block->inptr[k][i]; } } } else { for(k=0;k<block->nin;k++) { for(i=0;i<block->insz[0];i++) { *stk(k*m+gptr+j+i)=block->inptr[k][i]; } } } j=j+block->insz[0]; } else { if(evt==1) { for(i=0;i<m-j;i++) { *stk(gptr+j+i)=get_scicos_time(); } for(k=0;k<block->nin;k++) { for(i=0;i<m-j;i++) { *stk(k*m+m+gptr+j+i)=block->inptr[k][i]; } } for(i=m-j;i<block->insz[0];i++) { *stk(gptr+i+j-m)=get_scicos_time(); } for(k=0;k<block->nin;k++) { for(i=m-j;i<block->insz[0];i++) { *stk(k*m+m+gptr+i+j-m)=block->inptr[k][i]; } } } else { for(k=0;k<block->nin;k++) { for(i=0;i<m-j;i++) { *stk(k*m+gptr+j+i)=block->inptr[k][i]; } for(i=m-j;i<block->insz[0];i++) { *stk(k*m+gptr+i+j-m)=block->inptr[k][i]; } } } j=block->insz[0]-m+j; } block->z[0]=(double)j; } else if(flag==5) { scicos_free(work->varname[0]); scicos_free(work->varname); scicos_free(work); } }