📄 sds_assem.c
字号:
unsigned long pad_size = 0; unsigned long realloc_size = 0; unsigned long total_size = 0; unsigned long temp_size = 0; struct direc *dptr = sds_direc(sds); unsigned char pattern = 0xff; total_size = dptr[i].nelems*dptr[i].elemsz; realloc_size = total_size; if (i == 0) {/* Directory: do not copy over RECORD flags, and mark disjoint objects as unknown address */ int obcount; pointer = sds_malloc(total_size); memcpy(pointer, (char *)dptr, total_size); dptr = (struct direc *)pointer; for (obcount=0;obcount<dptr[0].nelems;obcount++) { if (dptr[obcount].structype == SDS_RECORDS) dptr[obcount].structype = SDS_NORMAL_OBJECT; if (dptr[obcount].illoca == SDS_DISJOINT_OBJECT) dptr[obcount].offst = SDS_IMPOSSIBLE_ADDRESS; } } else if (dptr[i].structype == SDS_RECORDS) { return sds_write_records(sds, fd, (sds_record_handle *)pointer,b,s,c); } if ((i != 0) && (dptr[i].illoca & SDS_REALLOC) && sds_dup_size(sds)) { temp_size = sds_dup_size(sds)[i] * dptr[i].elemsz; if (temp_size < total_size) { realloc_size = temp_size; pad_size = total_size - realloc_size; pattern = 0x00; } } if (pointer == SDS_ALLOCATE) { pad_size = total_size; realloc_size = 0; }/* If this thing is unallocated, I'll write ff bytes to indicate that there is undefined junk in it.*/ if (realloc_size != 0) { if (sds_buffered(fd,pointer,(int)realloc_size,b,s,c) != (int)realloc_size) { sds_push_error(SDS_FILE_WR,SDS_ERROR,"Writing object data"); return 0L; } } if (pad_size != 0) { if (sds_write_pattern(fd,pad_size,pattern,b,s,c) < 0) { sds_push_error(SDS_FILE_WR,SDS_ERROR,"Writing null pattern to object"); return 0L; } } if (i == 0) free(pointer); return total_size;}/*********************************************************************/sds_handlesds_load_direc(sds_handle sds,int fd, struct sds_header *h)/*********************************************************************/{ struct direc dtemp,*dptr, *dtp = &dtemp; char *list_ptr,*cptr; int ierr,ndirecs,lhsize; int o,ar; struct sds_header header; struct sds_control_p *scp = sds_control(sds);/* Read in the header ; */ if (h) /* Header has already been read in ...probably as test for conversion */ { memcpy(&header, h, sizeof(struct sds_header)); } else if ((ierr = sds_read_header(sds,fd,&header)) == 0) { scp->stream = SDS_FILE_OP; sds_push_error(ierr,SDS_ERROR,"Directory load"); return 0; } lhsize = (int)header.heap_size + (int)header.list_size; lhsize += align_delta(lhsize + BASE_OFFSET,sds_palign(SDS_DIRECTORY_STRUCTURE)); list_ptr = (char*) sds_malloc((unsigned)lhsize); if (sds_read(fd,lhsize,list_ptr) != lhsize) { sds_push_error(SDS_FILE_NOP,SDS_ERROR,"Unexpected end-of-dataset"); return 0; }/* Find size of directory */ if (sds_read(fd,sizeof(struct direc),(char *)dtp) != sizeof(struct direc)) { sds_push_error(SDS_FILE_NOP,SDS_ERROR,"Unexpected end-of-dataset"); return 0; } ndirecs = (int)dtemp.nelems;/* throw away previous directory */ if (scp->dptr != DNULL) { free((char *)scp->dptr); scp->dptr = DNULL; }/* New directory,tlist heap and header go here */ ar = ((ndirecs*sizeof(struct direc)) + (unsigned int)BASE_OFFSET + (unsigned int)header.heap_size + (unsigned int)header.list_size); ar += align_delta(ar,sds_palign(SDS_DIRECTORY_STRUCTURE)); cptr = (char*) sds_malloc(ar); scp->allofl = SDS_HEAD_ALLOC;/* Copy in header..... */ memcpy(cptr,(char *)&header,BASE_OFFSET); cptr += BASE_OFFSET;/* Copy in tlist and heap */ memcpy(cptr,list_ptr,lhsize); cptr += lhsize; dptr = (struct direc *)cptr;/* Copy in top directory..... */ memcpy(cptr,(char *)&dtemp,sizeof(struct direc)); set_sys_vars(sds,(struct direc *)cptr); cptr += sizeof(struct direc);/* Read in rest of directory..... */ if (sds_read(fd,sizeof(struct direc)*(ndirecs-1),cptr) != sizeof(struct direc)*(ndirecs-1)) { sds_push_error(SDS_FILE_NOP,SDS_ERROR,"Unexpected end-of-dataset"); return 0; }/* reclaim some space. */ free(list_ptr);/* Save offsets and mark object addresses impossible */ scp->file_offsets = (sds_handle *)malloc(dptr[0].nelems * sizeof(long)); for (o = 1; o< dptr[0].nelems; o++) { scp->file_offsets[o] = (long)dptr[(int)o].offst; dptr[(int)o].offst = SDS_IMPOSSIBLE_ADDRESS; } return sds;}/*********************************************************************/sds_handlesds_read_header(sds_handle sds,int fd,struct sds_header *header)/*********************************************************************/{ if (sds_read(fd,sizeof(struct sds_header),(char *)header) != sizeof(struct sds_header)) { sds_push_error(SDS_FILE_RD,SDS_ERROR,"Header load"); return 0L; } if (header->version != SDS_VERSION ) { sds_push_error(SDS_BAD_VERSION,SDS_WARNING,"Header load"); return 0L; } return 1L;}/*********************************************************************/intsds_open_file(name,flg)int flg;char *name;/* Here be System Dragons eg VMS's plethora of file type flags *//*********************************************************************/{ int fflag = O_RDWR,fd; char errstring[128]; errstring[0] = (char )0; if (flg != O_RDONLY) fflag |= (flg & (int)SDS_APPEND)?O_APPEND:O_CREAT | O_TRUNC; else fflag = flg;#ifdef vms if (!(fflag & O_APPEND)) { if ((fd = creat(name,0666,"ctx=nocvt","ctx = bin","mrs = 2048","rfm=udf")) < 0) { strcpy(errstring,"Cannot create file "); } } else#endif if ((fd = open(name,fflag,0666)) == -1) { strcpy(errstring,"Cannot open file "); } if (*errstring != (char)0) { strcat(errstring, name); sds_push_error(SDS_FILE_OP,SDS_ERROR,errstring); return -1; }#ifdef VXWORKS lseek(fd,0,SEEK_END); /* Go to end of file. */#endif return(fd);}/*********************************************************************/sds_handlesds_write_pattern(int fd,unsigned long size,char pattern,char *b,int s, int *c)/* Write crud to unallocated object going to eg file, although I'm not really sure why anyone would do it. Still, it's possible so one must do something rational-ish*//*********************************************************************/{#define SDS_JUNK_BUFFER 256 char buffer[SDS_JUNK_BUFFER]; int i,mod = (int)size/SDS_JUNK_BUFFER; int frac = (int)size%SDS_JUNK_BUFFER; memset(buffer,pattern,SDS_JUNK_BUFFER); if (mod > 0) for (i=0;i<mod;i++) { if (sds_buffered(fd,buffer,SDS_JUNK_BUFFER,b,s,c) < 0) { sds_push_error(SDS_FILE_WR,SDS_ERROR,"Writing spacers to file"); return 0; } } if (sds_buffered(fd,buffer,frac,b,s,c) <0) { sds_push_error(SDS_FILE_WR,SDS_ERROR,"Writing spacers to file"); return 0; } return 1;}/*********************************************************************/sds_handlesds_which(dptr)struct direc *dptr;/* Given a directory pointer , which sds does it refer to ? *//*********************************************************************/{ if (dptr != DNULL) { sds_handle i; for (i=1L;i<=sds_max();i++) if (dptr == sds_direc(i)) return(i); } return 0L;}/*********************************************************************/floatsds_version(sds)sds_handle sds;/*********************************************************************/{ struct sds_header *head = sds_head(sds); float version; short ver; if (head == HNULL) ver = SDS_VERSION; else ver = head->version; version = (float)ver; return version;}/* Additions to support SDS to file descriptors. *//***********************************************************************/sds_handlesds_write2fd(fd,sds)int fd;sds_handle sds;/***********************************************************************/{ struct direc *dptr; sds_handle sds_return = sds; char **object_pointer; if ((dptr = sds_direc(sds)) == DNULL) { sds_push_error(SDS_NO_SUCH_SDS, SDS_ERROR, "Write to file desc"); return 0L; } object_pointer = sds_saverestore(sds,dptr,SDS_SAVE_STATE); if (!to_file(sds,fd,object_pointer)) { sds_push_error(SDS_FILE_WR, SDS_ERROR, "Could not send data to fd"); sds_return = 0L; } sds_saverestore(sds,dptr,SDS_RESTORE_STATE); return(sds_return);}voidsds_settype(s,t,type, size)struct type_list *s,*t;sds_code type;int size;{ unsigned short a = 0xff00; int nb,sz = 0; t++; while (s != t) { nb = s->nelems & SDS_BITFIELD_NMASK; if (*(char *)&a) /* Big endian or..... */ { sz += nb; s->nelems = ((size - sz) << 16) + nb; } else /* ....little endian! */ { s->nelems = (sz << 16) + nb; sz += nb; } s->elemcod = type; s++; } t--; t->nelems |= SDS_LAST_BITFIELD;}void sds_fixbits(struct type_list *tl){ struct type_list *startbits; int bitson = 0, bitsize = 0; while (tl->elemcod != SDS_RETLIST) { if (tl->elemcod == SDS_BITFIELD) { if (!bitson) { bitson = 1; startbits = tl; } if ((bitson && tl[1].elemcod != SDS_BITFIELD) || (bitsize > BITS(int))) { bitson = 0; if ((bitsize + tl->nelems) <= BITS(char)) { tl->nelems += bitsize << 16; bitsize = 0; sds_settype(startbits,tl,SDS_CHAR_BITFIELD, 8); } else if ((bitsize + tl->nelems) <= BITS(short)) { tl->nelems += bitsize << 16; bitsize = 0; sds_settype(startbits,tl,SDS_SHORT_BITFIELD, 16); } else if ((bitsize + tl->nelems) <= BITS(int)) { tl->nelems += bitsize << 16; bitsize = 0; sds_settype(startbits,tl,SDS_LONG_BITFIELD, 32); } else { sds_settype(startbits,tl,SDS_LONG_BITFIELD,32); startbits = &tl[-1]; bitsize = tl[-1].nelems & SDS_BITFIELD_NMASK; tl->nelems += bitsize << 16; bitsize += tl[0].nelems & SDS_BITFIELD_NMASK; bitson = 1; } } else { tl->nelems += bitsize << 16; bitsize += tl->nelems & SDS_BITFIELD_NMASK; } } tl++; }}sds_handlesds_pmem_attach(char *mem){ sds_handle sds; struct direc *dptr; struct sds_header *h = (struct sds_header *)mem; if ((sds = next_sds()) < 0) return sds; dptr = (struct direc *)(mem + BASE_OFFSET + h->heap_size + h->list_size); set_sys_vars(sds, dptr); return sds;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -