コード例 #1
0
ファイル: dmap.c プロジェクト: SDFrance/RSTLite
char *DataMapEncodeBuffer(struct DataMap *ptr,int *size) {
  int c,x,m,n;
  char **tmp;
  void *tbuf;
  int tsze;
  struct DataMapScalar *s=NULL;
  struct DataMapArray *a=NULL;
  char *buf=NULL;
  int off=0;
  int sze=0;

  sze=DataMapSize(ptr);

  if (size !=NULL) *size=sze;
  buf=malloc(sze);
  if (buf==NULL) return NULL;

  ConvertFromInt(DATACODE,buf+off);
  off+=sizeof(int32);
  ConvertFromInt(sze,buf+off);
  off+=sizeof(int32);
  ConvertFromInt(ptr->snum,buf+off);
  off+=sizeof(int32);
  ConvertFromInt(ptr->anum,buf+off);
  off+=sizeof(int32);
  
  for (c=0;c<ptr->snum;c++) {
    s=ptr->scl[c];
    n=0;
    while (s->name[n] !=0) n++;
    memcpy(buf+off,s->name,n+1);
    off+=n+1;
    buf[off]=s->type;
    off++;
    switch (s->type) {
    case DATACHAR:
      buf[off]=s->data.cptr[0];
      off++;
      break;
    case DATASHORT:
      ConvertFromShort(*(s->data.sptr),buf+off);
      off+=sizeof(int16);
      break;
    case DATAINT:
      ConvertFromInt(*(s->data.iptr),buf+off);
      off+=sizeof(int32);
      break;
    case DATALONG:
      ConvertFromLong(*(s->data.lptr),buf+off);
      off+=sizeof(int64);
      break;

    case DATAUCHAR:
      buf[off]=s->data.ucptr[0];
      off++;
      break;
    case DATAUSHORT:
      ConvertFromUShort(*(s->data.usptr),buf+off);
      off+=sizeof(uint16);
      break;
    case DATAUINT:
      ConvertFromUInt(*(s->data.uiptr),buf+off);
      off+=sizeof(uint32);
      break;
    case DATAULONG:
      ConvertFromULong(*(s->data.ulptr),buf+off);
      off+=sizeof(uint64);
      break;

    case DATAFLOAT:
      ConvertFromFloat(*(s->data.fptr),buf+off);
      off+=sizeof(float);
      break;
    case DATADOUBLE:
      ConvertFromDouble(*(s->data.dptr),buf+off);
      off+=sizeof(double);
      break;
    case DATASTRING:
      tmp=(char **) s->data.vptr;
      if (*tmp !=NULL) {
        n=0;
        while((*tmp)[n] !=0) n++;
        memcpy(buf+off,*tmp,n+1);
        off+=n+1;
      } else {
        buf[off]=0;
        off+=1;
      }
      break;
    default:
      if (*s->data.mptr !=NULL) {
        tbuf=DataMapEncodeBuffer(*s->data.mptr,&tsze);
        ConvertFromInt(tsze,buf+off);
        off+=sizeof(int32);
        memcpy(buf+off,tbuf,tsze);
        off+=tsze;
        free(tbuf);
      } else {
        tsze=0;
        ConvertFromInt(tsze,buf+off);
        off+=sizeof(int32);
      }
      break;
    }
  }
 
  for (c=0;c<ptr->anum;c++) {
    a=ptr->arr[c];
    n=0;
    while (a->name[n] !=0) n++;
    memcpy(buf+off,a->name,n+1);
    off+=n+1;
    buf[off]=a->type;
    off++;
    ConvertFromInt(a->dim,buf+off);
    off+=sizeof(int32);
    for (x=0;x<a->dim;x++)  {
      ConvertFromInt(a->rng[x],buf+off);
      off+=sizeof(int32);
    }
    n=1;
    for (x=0;x<a->dim;x++) n=a->rng[x]*n;
    switch (a->type) {
    case DATACHAR:
      memcpy(buf+off,a->data.cptr,n);
      off+=n;
      break;
    case DATASHORT:
      for (x=0;x<n;x++) {
        ConvertFromShort(a->data.sptr[x],buf+off);
        off+=sizeof(int16);
      }
      break;
    case DATAINT:
      for (x=0;x<n;x++) {
        ConvertFromInt(a->data.iptr[x],buf+off);
        off+=sizeof(int32);
      }
      break;
    case DATALONG:
      for (x=0;x<n;x++) {
        ConvertFromLong(a->data.lptr[x],buf+off);
        off+=sizeof(int64);
      }
      break;

    case DATAUCHAR:
      memcpy(buf+off,a->data.cptr,n);
      off+=n;
      break;
    case DATAUSHORT:
      for (x=0;x<n;x++) {
        ConvertFromUShort(a->data.usptr[x],buf+off);
        off+=sizeof(uint16);
      }
      break;
    case DATAUINT:
      for (x=0;x<n;x++) {
        ConvertFromUInt(a->data.uiptr[x],buf+off);
        off+=sizeof(uint32);
      }
      break;
    case DATAULONG:
      for (x=0;x<n;x++) {
        ConvertFromULong(a->data.ulptr[x],buf+off);
        off+=sizeof(uint64);
      }
      break;

    case DATAFLOAT:
      for (x=0;x<n;x++) {
        ConvertFromFloat(a->data.fptr[x],buf+off);
        off+=sizeof(float);
      }
      break;
    case DATADOUBLE:
      for (x=0;x<n;x++) {
        ConvertFromDouble(a->data.dptr[x],buf+off);
        off+=sizeof(double);;
      }
      break;
    case DATASTRING:
      tmp=(char **) a->data.vptr;
      for (x=0;x<n;x++) {
        if (tmp[x] !=NULL) {
          m=0;
          while( (tmp[x])[m] !=0) m++;
          memcpy(buf+off,tmp[x],m+1);
          off+=m+1;
	} else {
          buf[off]=0;
          off++;
	}
      }
      break;
    default:
      for (x=0;x<n;x++) {
        if (a->data.mptr[x] !=0) {
          tbuf=DataMapEncodeBuffer(a->data.mptr[x],&tsze);
          ConvertFromInt(tsze,buf+off);
          off+=sizeof(int32);
          memcpy(buf+off,tbuf,tsze);
          off+=tsze;
          free(tbuf);
	} else {
          tsze=0;
          ConvertFromInt(tsze,buf+off);
          off+=sizeof(int32);
	}
      }
    }    
  }
  return buf;
}
コード例 #2
0
unsigned char *CSDMakeBuffer(struct CSDdata *ptr,int *sze) {

  unsigned char *buf=NULL;
  int size,off;
  int loop,block=0,rng_num=0;
  int nrng,nbyte;
  unsigned char gsct[(MAX_RANGE+7)/8];
  unsigned char dflg[(MAX_RANGE+7)/8];
  unsigned char bm,chn; 

  unsigned char store[3*MAX_RANGE];
  memset(gsct,0,(MAX_RANGE+7)/8);
  memset(dflg,0,(MAX_RANGE+7)/8);
  memset(store,0,MAX_RANGE*3);

  nrng=ptr->nrang;
  nbyte=(nrng+7)/8;
  bm=ptr->bmnum & 0xff; 
  chn=ptr->channel & 0xff;

  for (loop=0;loop<nrng;loop++) {
    if (ptr->store[MAX_RANGE+loop] !=0) set_bit(gsct,loop);
    if (ptr->store[loop] !=0) {
       set_bit(dflg,loop);
       rng_num++;
    }
  }
 
  if (rng_num>0) {
    int index=0; 
    for (loop=0;loop<nrng;loop++) {
      if (read_bit(dflg,loop) !=0) {
        block=0;
        if ((ptr->stored & 0x01) !=0) { 
          store[index]=ptr->store[2*MAX_RANGE+loop];
          block+=rng_num;
        }
        if ((ptr->stored & 0x02) !=0) {
          store[index+block]=ptr->store[3*MAX_RANGE+loop];
          block+=rng_num;
        }
        if ((ptr->stored & 0x04) !=0) 
          store[index+block]=ptr->store[4*MAX_RANGE+loop]; 
        index++; 
      } 
    }
  }
  block=0;
  if ((ptr->stored & 0x01) !=0) block+=rng_num;
  if ((ptr->stored & 0x02) !=0) block+=rng_num;
  if ((ptr->stored & 0x04) !=0) block+=rng_num;
    
  size=sizeof(double)+2*nbyte+3+block+sizeof(int16)*10+2*sizeof(int32);

  buf=malloc(size);
  off=0;
  ConvertFromDouble(ptr->time,buf+off);
  off+=8;  
  ConvertFromShort(ptr->scan,buf+off);
  off+=2;
  ConvertFromShort(ptr->cp,buf+off);
  off+=2;
  ConvertFromShort(ptr->intt.sc,buf+off);
  off+=2;
  ConvertFromInt(ptr->intt.us,buf+off);
  off+=4;
  ConvertFromShort(ptr->frang,buf+off);
  off+=2;
  ConvertFromShort(ptr->rsep,buf+off);
  off+=2;
  ConvertFromShort(ptr->rxrise,buf+off);
  off+=2;
  ConvertFromShort(ptr->tfreq,buf+off);
  off+=2;
  ConvertFromInt(ptr->noise,buf+off);
  off+=4;
  ConvertFromShort(ptr->atten,buf+off);
  off+=2;
  ConvertFromShort(ptr->nave,buf+off);
  off+=2;
  ConvertFromShort(ptr->nrang,buf+off);
  off+=2;
  memcpy(buf+off,gsct,nbyte);
  off+=nbyte;
  memcpy(buf+off,dflg,nbyte);
  off+=nbyte;
  buf[off]=ptr->stored;
  off++;
  buf[off]=chn;
  off++;
  buf[off]=bm;
  off++;

  if (block !=0) memcpy(buf+off,store,block);
  if (sze !=NULL) *sze=size;
  return buf;
}