void fputtr_internal(FILE *fp, segy *tp, cwp_Bool fixed_length) { unsigned int databytes; /* bytes from nsfirst */ int nwritten; /* bytes seen by fwrite calls */ /* search linked list for possible alternative */ if(fp != lastfp) searchlist(fp); if (infoptr == ((struct outsegyinfo *) NULL)) { /* initialize new segy output stream */ /* allocate new segy output information table */ *oldinfoptr = (struct outsegyinfo *) malloc(sizeof(struct outsegyinfo)); infoptr = *oldinfoptr; infoptr->nextinfo = (struct outsegyinfo *) NULL; /* save FILE * ptr */ infoptr->outfp = fp; infoptr->itr = 0; /* allocate XDR struct and associate FILE * ptr */ infoptr->segy_xdr = (XDR *) malloc(sizeof(XDR)); switch (infoptr->ftype = filestat(fileno(fp))) { case DIRECTORY: suerr("%s: segy output can't be a directory", __FILE__); case TTY: suerr("%s: segy output can't be tty", __FILE__); break; default: /* the rest are ok */ break; } xdrstdio_create(infoptr->segy_xdr,fp,XDR_ENCODE); /* Sanity check the segy header */ infoptr->nsfirst = tp->ns; if (infoptr->nsfirst > SU_NFLTS) suerr("%s: unable to handle %d > %d samples per trace", __FILE__, infoptr->nsfirst, SU_NFLTS); switch(tp->trid) { case CHARPACK: infoptr->bytesper = sizeof(char); break; case SHORTPACK: infoptr->bytesper = 2*sizeof(char); break; default: infoptr->bytesper = BYTES_PER_XDR_UNIT; break; } } databytes = infoptr->bytesper * (fixed_length?infoptr->nsfirst:tp->ns); if(FALSE == xdrhdrsub(infoptr->segy_xdr,tp)) suerr("%s: unable to write header on trace #%ld", __FILE__, (infoptr->itr)+1); nwritten = datawrite(infoptr, tp, fixed_length); if (nwritten != databytes) suerr("%s: on trace #%ld, tried to write %d bytes, " "wrote %d bytes", __FILE__, (infoptr->itr)+1, databytes, nwritten); ++infoptr->itr; lastfp = infoptr->outfp; }
static int fgettr_internal(FILE *fp, segy *tp, cwp_Bool fixed_length) { int nread; /* bytes seen by fread calls */ off_t curoff; if(fp != lastfp) /* search linked list for possible alternative */ searchlist(fp); if (infoptr == ((struct insegyinfo *) NULL)) { /* initialize new segy input stream */ unsigned int databytes; /* bytes from nsfirst */ /* allocate new segy input information table */ *oldinfoptr = (struct insegyinfo *) malloc(sizeof(struct insegyinfo)); infoptr = *oldinfoptr; infoptr->nextinfo = (struct insegyinfo *) NULL; /* save FILE * ptr */ infoptr->infp = fp; infoptr->itr = 0; infoptr->ntr = -1; /* allocate XDR struct and associate FILE * ptr */ infoptr->segy_xdr = (XDR *) malloc(sizeof(XDR)); switch (infoptr->ftype = filestat(fileno(fp))) { case DIRECTORY: err("%s: segy input can't be a directory", __FILE__); case TTY: err("%s: segy input can't be tty", __FILE__); break; default: /* the rest are ok */ break; } /* xdrstdio_create(infoptr->segy_xdr,fp,XDR_DECODE); */ infoptr->buf = ealloc1(sizeof(segy),sizeof(char)); xdrmem_create(infoptr->segy_xdr, infoptr->buf, sizeof(segy), XDR_DECODE); infoptr->bufstart = xdr_getpos(infoptr->segy_xdr); /* retrieve segy trace header */ nread = efread(infoptr->buf ,1 ,HDRBYTES ,infoptr->infp); if(nread != HDRBYTES || FALSE == xdrhdrsub(infoptr->segy_xdr,tp)) err("%s: bad first header", __FILE__); /* Have the header, now for the data */ infoptr->nsfirst = tp->ns; if (infoptr->nsfirst > SU_NFLTS) err("%s: unable to handle %d > %d samples per trace", __FILE__, infoptr->nsfirst, SU_NFLTS); switch(tp->trid) { case CHARPACK: infoptr->bytesper = sizeof(char); break; case SHORTPACK: infoptr->bytesper = 2*sizeof(char); break; default: infoptr->bytesper = BYTES_PER_XDR_UNIT; break; } databytes = infoptr->bytesper * tp->ns; infoptr->nsegy = databytes + HDRBYTES; nread = dataread(infoptr, tp, fixed_length); switch (nread) { case 0: err("%s: no data on first trace", __FILE__); default: if (nread != databytes) err("%s: first trace: tried to read %d bytes " "read %d bytes", __FILE__, databytes, nread); else nread += HDRBYTES; } if(infoptr->ftype == DISK) { /* compute ntr */ curoff = eftello(fp); efseeko(fp,(off_t) 0,SEEK_END); infoptr->ntr = eftello(fp)/infoptr->nsegy; efseeko(fp, curoff, SEEK_SET); /* restore location */ } } else { /* Not first entry */ xdr_setpos(infoptr->segy_xdr, infoptr->bufstart); nread = efread(infoptr->buf ,1 ,HDRBYTES ,infoptr->infp); if ( nread != HDRBYTES || FALSE == xdrhdrsub(infoptr->segy_xdr,tp)) nread=0; if(nread == HDRBYTES) nread += dataread(infoptr, tp, fixed_length); if (nread <= 0) { lastfp = infoptr->infp; return 0; } if (fixed_length && (tp->ns != infoptr->nsfirst)) err("%s: on trace #%ld, " "number of samples in header (%d) " "differs from number for first trace (%d)", __FILE__, infoptr->itr, tp->ns, infoptr->nsfirst); } ++(infoptr->itr); lastfp = infoptr->infp; return (nread); }