コード例 #1
0
ファイル: mg_net.c プロジェクト: jiezhou87/mglib
/*
  err = MG_NET_RECVVAR(socket, variable)

  Reads an IDL variable from the socket in the form written by MG_NET_SENDVAR.
  The complete variable is reconstructed. See MG_NET_SENDVAR for more details.
 */
static IDL_VPTR IDL_CDECL mg_net_recvvar(int argc, IDL_VPTR argv[], char *argk) {
  IDL_LONG i, iRet;
  IDL_LONG swab = 0;
  i_var var;
  IDL_VPTR vpTmp;
  char *pbuffer;

  i = IDL_LongScalar(argv[0]);
  if ((i < 0) || (i >= MAX_SOCKETS)) return (IDL_GettmpLong(-1));
  if (net_list[i].iState != NET_IO) return (IDL_GettmpLong(-1));
  IDL_EXCLUDE_EXPR(argv[1]);

  /* read the header */
  iRet = recv_packet(net_list[i].socket, &var,sizeof(i_var));
  if (iRet == -1) return (IDL_GettmpLong(-1));
  if (var.token == SWAPTOKEN) {
    mg_byteswap(&var, sizeof(i_var), sizeof(IDL_LONG));
    swab = 1;
  }
  if (var.token != TOKEN) return (IDL_GettmpLong(-1));

  /* allocate the variable */
  if (var.type == IDL_TYP_STRING) {
    vpTmp = IDL_StrToSTRING("");
    IDL_StrEnsureLength(&(vpTmp->value.str), var.len);
    vpTmp->value.str.slen = var.len - 1;
    pbuffer = vpTmp->value.str.s;
    memset(pbuffer, 0x20, var.len-1);
    pbuffer[var.len] = '\0';
    IDL_VarCopy(vpTmp, argv[1]);
  } else if (var.ndims != 0) {
    pbuffer = IDL_MakeTempArray(var.type, var.ndims, var.dims, IDL_BARR_INI_NOP, &vpTmp);
    IDL_VarCopy(vpTmp, argv[1]);
  } else {
    vpTmp = IDL_GettmpLong(0);
    IDL_VarCopy(vpTmp, argv[1]);
    IDL_StoreScalarZero(argv[1], var.type);
    pbuffer = &(argv[1]->value.c);
  }

  /* read the data */
  iRet = recv_packet(net_list[i].socket, pbuffer, var.len);
  if (iRet == -1) return (IDL_GettmpLong(-1));
  if (swab) {
    int	swapsize = var.len / var.nelts;
    if ((var.type == IDL_TYP_COMPLEX)
	|| (var.type == IDL_TYP_DCOMPLEX)) {
      swapsize /= 2;
    }
    mg_byteswap(pbuffer, var.len, swapsize);
  }

  return (IDL_GettmpLong(1));
}
コード例 #2
0
int32 OldCnvMapIDLRead(int argc,char *argv[]) {

  int32 *idlstnum;
  int32 *idlvcnum;
  int32 *idlmodnum;
  int32 *idlcoefnum;
  int32 *idlbndnum;


  IDL_STRING *idlptr=NULL;
  struct CnvMapData map;
  struct GridData grd;

  IDL_FILE_STAT stat;
  FILE *fp;
  int unit; 
  int s;

  unit = *( (int32 *) argv[0]);

  /* Make sure that the "STDIO" keyword was used to
   * open the file in IDL.
   */

  s=IDL_FileEnsureStatus(IDL_MSG_RET,unit,IDL_EFS_STDIO);
  if (s==FALSE) return -1;

  /* Get information about the file */

  IDL_FileStat(unit,&stat);
 
  /* Find the file pointer */

  fp=stat.fptr; 
  
  if (fp==NULL) return -1;

  fflush(fp);
 
  /* Read in the record */

  memset(&grd,0,sizeof(struct GridData));
  memset(&map,0,sizeof(struct CnvMapData));

  s=OldCnvMapFread(fp,&map,&grd);
  if (s==-1) return -1;

  /* get the output pointers */

  idlstnum=(int32 *) argv[1];
  idlvcnum=(int32 *) argv[2];
  idlmodnum=(int32 *) argv[3];
  idlcoefnum=(int32 *) argv[4];
  idlbndnum=(int32 *) argv[5];

  idlptr=(IDL_STRING *) argv[6];
  
  IDL_StrEnsureLength(idlptr,sizeof(struct GridData)+
                      grd.stnum*sizeof(struct GridSVec)+
                      grd.vcnum*sizeof(struct GridGVec)+
                      sizeof(struct CnvMapData)+
                      map.num_model*sizeof(struct GridGVec)+
                      map.num_coef*4*sizeof(double)+ 
                      map.num_bnd*2*sizeof(double));
  memcpy(idlptr->s,&grd,sizeof(struct GridData)); 
  memcpy(idlptr->s+sizeof(struct GridData),grd.sdata,
         grd.stnum*sizeof(struct GridSVec));
  memcpy(idlptr->s+sizeof(struct GridData)+grd.stnum*sizeof(struct GridSVec),
         grd.data,grd.vcnum*sizeof(struct GridGVec));
  memcpy(idlptr->s+sizeof(struct GridData)+grd.stnum*sizeof(struct GridSVec)+
          grd.vcnum*sizeof(struct GridGVec),
          &map,sizeof(struct CnvMapData));
  memcpy(idlptr->s+sizeof(struct GridData)+grd.stnum*sizeof(struct GridSVec)+
         grd.vcnum*sizeof(struct GridGVec)+sizeof(struct CnvMapData),
         map.model,map.num_model*sizeof(struct GridGVec));
  memcpy(idlptr->s+sizeof(struct GridData)+grd.stnum*sizeof(struct GridSVec)+
         grd.vcnum*sizeof(struct GridGVec)+sizeof(struct CnvMapData)+
         map.num_model*sizeof(struct GridGVec),map.coef,
         map.num_coef*4*sizeof(double));
  memcpy(idlptr->s+sizeof(struct GridData)+grd.stnum*sizeof(struct GridSVec)+
         grd.vcnum*sizeof(struct GridGVec)+sizeof(struct CnvMapData)+
         map.num_model*sizeof(struct GridGVec)+map.num_coef*4*sizeof(double),
         map.bnd_lat,map.num_bnd*sizeof(double));
  memcpy(idlptr->s+sizeof(struct GridData)+grd.stnum*sizeof(struct GridSVec)+
         grd.vcnum*sizeof(struct GridGVec)+sizeof(struct CnvMapData)+
         map.num_model*sizeof(struct GridGVec)+map.num_coef*4*sizeof(double)+
         map.num_bnd*sizeof(double),map.bnd_lon,map.num_bnd*sizeof(double));

  *idlstnum=grd.stnum;
  *idlvcnum=grd.vcnum;
  *idlmodnum=map.num_model;
  *idlcoefnum=map.num_coef;
  *idlbndnum=map.num_bnd;

  return s;
}