📄 sdc.c
字号:
/* $Header: /cvsroot/sourcenav/src/snavigator/demo/c++_demo/sds/sdc.c,v 1.1.1.1 2002/04/18 23:35:28 mdejong Exp $ *//************************************************************************** * ****** ISTK Release 1.2 ***** * * * * * * This code has been produced by numerous authors at the CERN centre for * * high energy physics, Geneve, Switzerland, at the SSC laboratory in * * Dallas, Texas, USA and at the Lawrence Berekeley Laboratory in * * California, USA. * * The latter two institutions perform work under US Government contract. * * The intent of the work is to provide useful code for people who need * * it, with an emphasis on free and collaborative exchange of ideas, * * techniques and implementations. * * Please read the disclaimer and copyright notices contained in the ISTK * * distribution and in distributed applications. * * * **************************************************************************//* Reference release Aug 10 1991 - C G Saltmarsh *//* Has the basics used at CDG & SSC 1988-1991, plus vxworks support*/#ifdef HPUX#include <ctype.h>#endif#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <math.h>#ifndef vms#include <unistd.h>#endif#if defined(vms)#include "sdsgen.h"#include "sds_externs.h"#else#include "Sds/sdsgen.h"#include "Sds/sds_externs.h"#endifstruct dshort { char one; char two; };struct dlong { char one; char two; char three; char four;};struct ddouble { char one; char two; char three; char four; char five; char six; char seven; char eight; };#if !defined(hpux) && !defined(VXWORKS) && !defined(mips)#if !defined(vms)#if !defined(__GCC_2__)#if defined(__GNUC__)#if __GNUC__ != 2extern char *memcpy();#endif#endif#endif#endif#endif/********* forward declarations ****************************************/void rswabw(char *, char *, int);void rswab(char *, char *, int);void rswabd(char *, char *, int);void sds_fix_tlist(struct sds_header *, struct type_list *, int);void sds_fix_header(struct sds_header *, int);void sds_fix_direc(struct direc *, int, int, int);int sds_copen(char *);extern char *load_sds_file(int, unsigned long);extern void r_float(char *, char *, int);extern void r_double(char *, char *, int);sds_handle sds_lofd(sds_handle,int,unsigned long,struct sds_header *);/***********************************************************************//* Load and convert from filename */sds_handlesds_load_conv(char *filename)/***********************************************************************/{ int fd; sds_handle sds; if ((fd = open(filename,O_RDONLY,0644)) < 0) { sds_push_error(SDS_FILE_OP,SDS_ERROR,"Foreign arch directory load"); return 0L; } sds = sds_read_open_fd(fd, ffsiz(fd)); sds_close_fd(fd); return sds;}/***********************************************************************//* Read from file descriptor, and convert if necessary *//* A size of 0 means unknown - but if the size IS known, one can often get performance advantage, so we will use it if possible. If buffer is non-zero, some at least of the incoming data has been loaded already. */sds_handle sds_read_open_fd(int fd, int size)/***********************************************************************/{ sds_handle orig_state; sds_handle new_sds = 0,old_sds = 0; int i,old,new; long lhsize, asize; long new_type; long del = 0; long element_end_addr; char junk[32], *j = junk; char *cptr; struct sds_header head; struct direc *dptr; struct direc *ndptr; struct sds_odesc *othing,*nthing; struct sds_control_p *scp; struct type_list *ntlist; struct sds_header *nheader,*header; old_sds = next_sds();/* Check to see if conversion is necessary */ if (!(new_sds = sds_cload_direc(old_sds,fd,&orig_state,&head))) { if (sds_last_return() == SDS_GOOD_FORMAT) /* In this case, cload_direc has loaded the header before it found that conversion is not necessary */ { if (!(new_sds = sds_lofd(old_sds, fd, size, &head))) sds_destroy(old_sds); return new_sds; } sds_push_error(SDS_NO_SUCH_SDS,SDS_ERROR,"Foreign arch directory load"); sds_destroy(old_sds); return 0L; } header = sds_head(old_sds); dptr = sds_direc(old_sds); /* Duplicate this header for the new, native arch, dataset */ /* sds_cload_direc() has done a job of loading the direcs correctly, and adjusting offsets therein. We now prepare to read actual data objects. */ lhsize = BASE_OFFSET + header->heap_size + header->list_size; lhsize += align_delta(lhsize,sds_palign(SDS_DIRECTORY_STRUCTURE)); asize = lhsize + dptr[0].nelems * sizeof(struct direc); cptr = sds_malloc(asize); memcpy(cptr,(char *)header,asize); nheader = (struct sds_header *)cptr;/* Some adaptions of this data to indicate native architecture */ nheader->magic_number = SDS_MAGIC; ndptr = (struct direc *)(cptr + lhsize); ndptr[0].offst = (sds_off_t)ndptr - (long)nheader; new_sds = next_sds(); scp = sds_control(new_sds); scp->allofl = SDS_HEAD_ALLOC; set_sys_vars(new_sds,ndptr); ntlist = sds_tlist(new_sds); ndptr = sds_direc(new_sds);/* Check if the request has been to truncate 64-bit ints or put them as doubles: if so, convert() will throw away the extra incoming high-end int fields or do the conversion, and maybe check for overflow */ if (!SDS_BIGADDR && (sds_query_truncate_b() || sds_query_float_b())) { long l; sds_code code = sds_query_truncate_b()?SDS_LONG:SDS_DOUBLE; for (l=0;l<(int)nheader->list_size/sizeof(struct type_list);l++) if (ntlist[l].elemcod == SDS_DOUBLE_LONG) ntlist[l].elemcod = code; for (l=0;l<ndptr[0].nelems;l++) if (ndptr[l].elemcod == SDS_DOUBLE_LONG) ndptr[l].elemcod = code; } fix_sizes_and_aligns(new_sds); ndptr[0].offst = SDS_NOT_ASSEMBLED; for (i=1;i<dptr[0].nelems;i++) { ndptr[i].offst = (long)SDS_ALLOCATE; ndptr[i].illoca = (char)0; dptr[i].illoca = (char)0; } new_sds = sds_ass(new_sds,"temp",SDS_PROC_MEM);/* element_end_addr is there in case there is padding before the next object. sds_resolve reports actuall addresses (although in this case they are used as offsets, as the data is not yet in process memory) */ element_end_addr = (long)dptr + dptr[0].nelems * sizeof(struct direc);/* The complete descriptions - header, tlist and directories - are now an accurate description of both input and output sds's. It remains to convert the objects, which are pointed to by the object_pointer list and described by the direc's */ for (i=1;i<ndptr[0].nelems;i++) { sds_cleanup(new_sds); sds_cleanup(old_sds); while((old = sds_resolve(old_sds,i,&othing,SDS_OBJECT)) >= 0) { new = sds_resolve(new_sds,i,&nthing,SDS_OBJECT); /* Extra pads bfore the next object? */ if ((del = (long)(othing[old].address - element_end_addr)) > 0) if (sds_read(fd,del,j) != del) { sds_discard(new_sds); sds_discard(old_sds); return 0; } new_type = convert(old_sds,fd,&othing[old], &nthing[new], (int)orig_state); if (ndptr[i].elemcod & SDS_INDLIST) ntlist[nthing[new].ind-1].elemcod = new_type; else ndptr[i].elemcod = new_type; element_end_addr = (long)othing[old].address + othing[old].nelems * othing[old].size; } new = sds_resolve(new_sds,i,&nthing,SDS_OBJECT); } sds_destroy(old_sds); return(new_sds);}/***********************************************************************/voidfix_sizes_and_aligns(sds)sds_handle sds;/***********************************************************************/{ struct direc *dptr = sds_direc(sds); struct type_list *ttptr = sds_tlist(sds); struct sds_header *head = sds_head(sds); char align; unsigned long ind,i; unsigned long ntp = (unsigned long)head->list_size/sizeof(struct type_list); if (ntp) { for (i=0;i<ntp;i++,ttptr++) { if (ttptr->elemcod & SDS_SIZE_ALIGN) { ind = (i+1) | SDS_INDLIST; ttptr->nelems = sds_tlsize(sds,ind,&align); ttptr->elemcod = SDS_SIZE_ALIGN | (long)(align & 0xff); } } } for (i=0;i<dptr[0].nelems;i++) dptr[i].elemsz = sds_element_size(sds, dptr[i].elemcod, &dptr[i].align_type);}/***********************************************************************/sds_codeconvert(sds_handle sds,int fd,struct sds_odesc *old, struct sds_odesc *new,int bs_flag)/***********************************************************************/{ long ret_type = old->elemcod; int this_read, size; char buffer[8192]; size = (int)old->size * old->nelems; while (size && (this_read = sds_read(fd,size,buffer))) { size -= this_read; switch((int)old->elemcod) {#ifdef IEEEFP case SDS_IFLOAT: if(bs_flag == SDS_SWAPPED_BYTES) rswabw(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); break; case SDS_VFLOAT: ret_type = SDS_IFLOAT;#endif#ifdef VAXFP case SDS_VFLOAT: if(bs_flag == SDS_SWAPPED_BYTES) rswabw(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); break; case SDS_IFLOAT: ret_type = SDS_VFLOAT;#endif if(bs_flag != SDS_SWAPPED_BYTES) rswabw(buffer,buffer,old->size*old->nelems); r_float(buffer,new->address,old->size*old->nelems); break;#ifdef IEEEFP case SDS_ICOMPLEX: if(bs_flag == SDS_SWAPPED_BYTES) rswabw(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); break; case SDS_VCOMPLEX: ret_type = SDS_ICOMPLEX;#endif#ifdef VAXFP case SDS_VCOMPLEX: if(bs_flag == SDS_SWAPPED_BYTES) rswabw(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); break; case SDS_ICOMPLEX: ret_type = SDS_VCOMPLEX;#endif if(bs_flag != SDS_SWAPPED_BYTES) rswabw(buffer,buffer,old->size*old->nelems); r_float(buffer,new->address,old->size*old->nelems); break;#ifdef IEEEFP case SDS_IDOUBLE: if(bs_flag == SDS_SWAPPED_BYTES) rswabd(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); break; case SDS_DVDOUBLE: case SDS_GVDOUBLE: ret_type = SDS_IDOUBLE;#endif#ifdef VAXFP case SDS_DVDOUBLE: case SDS_GVDOUBLE: if(bs_flag == SDS_SWAPPED_BYTES) rswabd(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); break; case SDS_IDOUBLE: ret_type = SDS_DVDOUBLE;#endif if(bs_flag != SDS_SWAPPED_BYTES) rswabd(buffer,new->address,old->size*old->nelems); r_double(buffer,new->address,old->size*old->nelems); break;#ifdef IEEEFP case SDS_IDOUBLE_COMPLEX: if(bs_flag == SDS_SWAPPED_BYTES) rswabd(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); break; case SDS_DVDOUBLE_COMPLEX: case SDS_GVDOUBLE_COMPLEX: ret_type = SDS_IDOUBLE;#endif#ifdef VAXFP case SDS_DVDOUBLE_COMPLEX: case SDS_GVDOUBLE_COMPLEX: if(bs_flag == SDS_SWAPPED_BYTES) rswabd(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); break; case SDS_IDOUBLE_COMPLEX: ret_type = SDS_DVDOUBLE;#endif if(bs_flag != SDS_SWAPPED_BYTES) rswabd(buffer,buffer,old->size*old->nelems); r_double(buffer,new->address,old->size*old->nelems); break; case SDS_DOUBLE_LONG: if (!SDS_BIGADDR && !sds_query_pack_b()) { if (sds_query_float_b()) { long l; if (bs_flag == SDS_SWAPPED_BYTES) rswabd(buffer,buffer,old->size*old->nelems); for (l=0;l<old->nelems;l++) *(double *)(new->address + l * 8) = (double)*(int *)(buffer + 4 + l * 8) + (double)*(int *)(buffer + l *8) * pow(2.0,32.0); ret_type = SDS_DOUBLE; } else if (sds_query_truncate_b()) { long l; if (bs_flag == SDS_SWAPPED_BYTES) rswabd(buffer,buffer,old->size*old->nelems); for (l=0;l<old->nelems;l++) { memcpy((new->address + l * 4),(buffer + 4 + l * 8), 4); if (sds_query_trap_b() && *(int *)(buffer + l * 8)) sds_bigint_trap(buffer + l * 8); } ret_type = SDS_LONG; } else { if(bs_flag == SDS_SWAPPED_BYTES) rswabd(buffer,new->address,old->size*old->nelems); else memcpy(new->address,buffer,old->size*old->nelems); } } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -