Fonction de calcul
fr


sci_buft

Contenu du fichier


/* sci_buft Scicos scilab global variable event 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];

/* buft_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 buft_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;

 /*fprintf(stderr,"ret=%d\n",ret);*/
 /*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);
}

/* buft_checkidgvar (not used)
 * input : varname
 * output : return an error number
 *          if any
 */
int buft_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);
}

/* buft_getidgvar
 *
 * retrieve scilab variable Lstk
 * value
 *
 * input : varname
 * output : return an error number
 *          if any
 */
int buft_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 jitter period computation (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
 *                  z[1] : last value of t
 */

/*work structure of that block*/
typedef struct work_st {
  char **varname; /*name of the scilab variable*/
} work_struct;

/*prototype*/
void sci_buft(scicos_block *block,int flag)
{
 /*local variable*/
 int j;
 int m;
 int n;
 int opt;
 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 size of buffer*/
 m=block->ipar[0];
 n=1;

 /*get with jitter period computation */
 opt = block->ipar[1];

 /*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;

   /**/
   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=buft_cregvar(work->varname,m,n);

 }

 /*store only in flag 2*/
 else if(flag==2)
 {
  /**/
  j=(int)block->z[0];
  /**/
  gptr = buft_getidgvar(work->varname);
  /**/
  if (opt==1) {
    *stk(gptr+j)=get_scicos_time()-block->z[1];
  }
  else {
   *stk(gptr+j)=get_scicos_time();
  }
  /**/
  block->z[1]=get_scicos_time();
  /**/
  j++;
  /**/
  if(j==m) {
    j=0;
  }
  /**/
  block->z[0]=(double)j;
 }

 else if(flag==5) {
  scicos_free(work->varname[0]);
  scicos_free(work->varname);
  scicos_free(work);
 }

}