📄 fgettr.c
字号:
if(fp != lastfp) 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; infoptr->infp = fp; /* save FILE * ptr */ infoptr->itr = 0; infoptr->ntr = -1; 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__); default: /* all others are ok */ break; }/*--------------------------------------------------------------------*\ Check for the presence of a line header and set a flag if one is found. The decision of what to do will be delayed until the call to fputtr(). This allows us to accept data w/ or w/o a line header. Reginald H. Beardsley rhb@acm.org\*--------------------------------------------------------------------*/ /* Attempt to get a text header */ nread=efread(buf ,1 ,HDRBYTES ,infoptr->infp); switch( nread ){ case 0: return 0; /* no traces; trap in mains */ default: if (nread < HDRBYTES ){ return 0; }else if( isascii_txt( buf ,HDRBYTES ) || isebcdic_txt( buf ,HDRBYTES ) ){ in_line_hdr = 1; memcpy( su_text_hdr ,buf ,HDRBYTES ); nread += efread(&(su_text_hdr[HDRBYTES]) ,1 ,3200-HDRBYTES ,infoptr->infp); }else{ in_line_hdr=0; memcpy( tp ,buf ,HDRBYTES ); } } if( in_line_hdr ){ /* Get the binary header */ nread = efread(&su_binary_hdr, 1, sizeof(bhed), infoptr->infp); switch( nread ){ case 0: return 0; /* no traces; trap in mains */ default: if (nread != sizeof(su_binary_hdr)){ err("%s:%d bad binary header" , __FILE__ ,__LINE__ ); } } /* Get the first trace header */ nread = efread(tp, 1, HDRBYTES, infoptr->infp); switch( nread ){ case 0: return 0; /* no traces; trap in mains */ default: if (nread != HDRBYTES){ 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 = sizeof(float); break; } databytes = infoptr->bytesper * tp->ns; infoptr->nsegy = HDRBYTES + databytes; /* Inconvenient to bump nread here; do it in the switch */ nread = dataread(tp, infoptr, fixed_length); switch (nread) { case 0: err("%s: no data on first trace", __FILE__); default: if (nread != databytes){ err("%s: first trace: " "read only %d bytes of %u", __FILE__, nread, databytes); }else{ nread += HDRBYTES; } } if (infoptr->ftype == DISK) { /* compute ntr */ efseeko(fp, (off_t) 0LL,SEEK_END); if( in_line_hdr ){ infoptr->ntr = (eftello(fp)-3600)/infoptr->nsegy; efseeko(fp, (off_t) 3600+infoptr->nsegy,SEEK_SET); }else{ infoptr->ntr = eftello(fp)/infoptr->nsegy; efseeko(fp, (off_t) infoptr->nsegy ,SEEK_SET); } } }else{ /* not first trace *//*--------------------------------------------------------------------*\ A number of programs seek on the input file using either fseek(3c) or rewind(3c) and then expect to read trace data. As a consequence we need to check and offset the filepointer if needed.\*--------------------------------------------------------------------*/ if( in_line_hdr && ftello( infoptr->infp ) == 0 ){ fseeko( infoptr->infp ,(off_t)3600L ,SEEK_SET ); } nread = (int) efread(tp, 1, HDRBYTES, infoptr->infp); switch( nread ){ case 0: lastfp = infoptr->infp; return 0; /* finished */ default: if (nread != HDRBYTES){ err("%s: on trace #%ld read %d bytes expected %d bytes", __FILE__,(infoptr->itr)+1,nread,HDRBYTES); } } nread += dataread(tp, infoptr, fixed_length); 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)+1, tp->ns, infoptr->nsfirst); } } ++(infoptr->itr); lastfp = infoptr->infp; return (nread);}int fgettr(FILE *fp, segy *tp){ return(fgettr_internal(fp,tp,cwp_true));}int fvgettr(FILE *fp, segy *tp){ return(fgettr_internal(fp,tp,cwp_false));}int fgettra(FILE *fp, segy *tp, int itr){ int nread; if(lastfp != fp) searchlist(fp); /* search for match */ if(infoptr == (struct insegyinfo *) NULL) { /* get first trace */ if(0 >= fgettr(fp, tp)) return(0); /* error return */ switch(infoptr->ftype) { case TTY: warn("stdin not redirected"); break; case DISK: /* correct */ break; default: err("%s: input must be disk file",__FILE__); } efseeko(fp,(off_t) 0LL,SEEK_END); if( in_line_hdr ){ infoptr->ntr = (off_t)((eftello(fp)-3600)/infoptr->nsegy); efseeko(fp, (off_t) 3600+infoptr->nsegy,SEEK_SET); }else{ infoptr->ntr = (off_t)(eftello(fp)/infoptr->nsegy); efseeko(fp, (off_t) infoptr->nsegy,SEEK_SET); } } /* end first entry initialization */ /* Check on requested trace number */ if(itr >= infoptr->ntr) err("%s: trying to read off end of file",__FILE__); /* Position file pointer at start of requested trace */ if( in_line_hdr ){ efseeko(fp, (off_t) 3600+itr*infoptr->nsegy,SEEK_SET); }else{ efseeko(fp, (off_t) itr*infoptr->nsegy,SEEK_SET); } nread=fgettr(fp, tp); /* let fgettr do the work */ if(nread != infoptr->nsegy) err("%s: read %d bytes in trace of %d bytes", __FILE__,nread,infoptr->nsegy); if(tp->ns != infoptr->nsfirst) warn("%s: header ns field = %d differs from first trace = %d", __FILE__,tp->ns,infoptr->nsfirst); return(infoptr->ntr);}#endif /* end of XDR choice */ /*====================================================================*\ These functions determine the presence of a SEGY text header based on the character set statistics of the first 3200 bytes in the file. Reginald H. Beardsley rhb@acm.org\*====================================================================*/static unsigned char asciitext[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; static unsigned char ebcdictext[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1, 1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1, 0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0, 0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0 }; int isascii_txt( unsigned char* a ,int n){ int ascii = 0; int ebcdic = 0; int i; for (i=0; i<n; i++){ ascii += asciitext[a[i]]; ebcdic += ebcdictext[a[i]]; } if( ascii > ebcdic && ascii > (int)(n*0.9) ){ return(1); }else{ return(0); }} int isebcdic_txt( unsigned char* a ,int n ){ int ascii = 0; int ebcdic = 0; int i; for (i=0; i<n; i++){ ascii += asciitext[a[i]]; ebcdic += ebcdictext[a[i]]; } if( ascii < ebcdic && ebcdic > (int)(n*0.9) ){ return(1); }else{ return(0); }}#else#include <stdio.h>#include "su.h"#include "segy.h"char *sdoc[] = {" "," tgettr <stdin >stdout "," "," Test harness for gettr.c "," Changes cdp to abs(cdp) "," Contrast the following results: "," suplane offset=-100 | sugethw offset "," suplane offset=-100 | tgettr | sugethw offset "," ",NULL};segy tr;main(int argc, char **argv){ initargs(argc, argv); requestdoc(1); while (gettr(&tr)) { tr.offset = abs(tr.offset); puttr(&tr); } return EXIT_SUCCESS;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -