113111Srrh #ifndef lint 2*15063Sgarrison static char sccsid[] = "@(#)streams.c 2.2 09/23/83"; 313111Srrh #endif not lint 4*15063Sgarrison # 512916Sgarrison 612916Sgarrison # include "stdio.h" 712916Sgarrison # include "streams.h" 812916Sgarrison # include "ctype.h" 912916Sgarrison 1012916Sgarrison /* getword(stream,p,ignore): 1112916Sgarrison read next sequence of nonspaces on current line into *p. 1212916Sgarrison null if no more words on current line. 1312916Sgarrison %x (x in ignore) terminates line. 1412916Sgarrison all words of the form %a are returned as null. 1512916Sgarrison *p is a null terminated string (char p[maxstr]). 1612916Sgarrison */ 1712916Sgarrison getword(stream,p,ignore) 1812916Sgarrison FILE *stream; 1912916Sgarrison char *p, *ignore; 2012916Sgarrison { char c; 2112916Sgarrison char *oldp, *stop; 2212916Sgarrison 2312916Sgarrison oldp= p; 2412916Sgarrison stop= p+maxstr-1; 2512916Sgarrison do{ c= getc(stream); 2612916Sgarrison } while (isspace(c) && c!='\n'); 2712916Sgarrison 2812916Sgarrison while (!isspace(c)) 2912916Sgarrison { *p= c; 3012916Sgarrison if (p < stop) p++; 3112916Sgarrison c= getc(stream); 3212916Sgarrison } 3312916Sgarrison *p= NULL; 3412916Sgarrison 3512916Sgarrison if (oldp[0]=='%') 3612916Sgarrison { oldp[0]= NULL; 3712916Sgarrison if (index(ignore, oldp[1]) != NULL) 3812916Sgarrison while (c!='\n') c=getc(stream); 3912916Sgarrison } 4012916Sgarrison } 4112916Sgarrison 4212916Sgarrison 4312916Sgarrison 4412916Sgarrison /* recsize(stream,start): 4512916Sgarrison returns length of record beginning at start 4612916Sgarrison (record ends at blank line or eof) 4712916Sgarrison assumes and retains stream positioned at start 4812916Sgarrison */ 4912916Sgarrison long int recsize(stream,start) 5012916Sgarrison FILE *stream; 5112916Sgarrison long int start; 5212916Sgarrison { char c; /* length = # of chars from start to beginning */ 5312916Sgarrison long int length; /* of current line. c in current line. */ 5412916Sgarrison int nonspaces; /* nonspaces = # of nonspaces in current line. */ 5512916Sgarrison 5612916Sgarrison nonspaces= 0; 5712916Sgarrison c= getc(stream); 5812916Sgarrison length= 0L; 5912916Sgarrison 6012916Sgarrison while ( (c!='\n' || nonspaces!=0) && c!=EOF) 6112916Sgarrison { if (c=='\n') 6212916Sgarrison { length= ftell(stream)-start; 6312916Sgarrison nonspaces= 0; 6412916Sgarrison } 6512916Sgarrison else if (!isspace(c)) nonspaces++; 6612916Sgarrison 6712916Sgarrison c= getc(stream); 6812916Sgarrison } 6912916Sgarrison 7012916Sgarrison pos(start); 7112916Sgarrison return(length); 7212916Sgarrison } 7312916Sgarrison 7412916Sgarrison 7512916Sgarrison /* nextrecord(stream,x): seeks in stream for first non-blank line 7612916Sgarrison at or after char x in stream. seeks to eof if x is past last record. 7712916Sgarrison x is the index of a character in the file (not eof). 7812916Sgarrison returns position in stream. (returns EOF, if seeks to EOF) 7912916Sgarrison */ 8012916Sgarrison long int nextrecord(stream,x) 8112916Sgarrison FILE *stream; 8212916Sgarrison long int x; 8312916Sgarrison { long int start; /* position of the beginning of the line */ 8412916Sgarrison char c; /* containing c */ 8512916Sgarrison 8612916Sgarrison pos(x); 8712916Sgarrison start= x; 8812916Sgarrison /* find start of first non-blank record */ 8912916Sgarrison for(;;) 9012916Sgarrison { c= getc(stream); 9112916Sgarrison if (c=='\n') start= ftell(stream); 9212916Sgarrison else if (!isspace(c)) break; 9312916Sgarrison } 9412916Sgarrison 9512916Sgarrison if (feof(stream)) { pos(start); start= EOF; } 9612916Sgarrison else pos(start); 9712916Sgarrison return(start); 9812916Sgarrison } 9912916Sgarrison 10012916Sgarrison /* nextline(stream,x): seeks in stream after first newline at or after 10112916Sgarrison char x in stream. seeks to eof if x is in last line. 10212916Sgarrison x is the index of a character in the file (not eof). 10312916Sgarrison returns position in stream 10412916Sgarrison */ 10512916Sgarrison long int nextline(stream,x) 10612916Sgarrison FILE *stream; 10712916Sgarrison long int x; 10812916Sgarrison { pos(x); 10912916Sgarrison while (getc(stream)!='\n') ; 11012916Sgarrison return(ftell(stream)); 11112916Sgarrison } 11212916Sgarrison 11312916Sgarrison 11412916Sgarrison /* printline(stream): copies stream up to a newline 11512916Sgarrison */ 11612916Sgarrison printline(stream) 11712916Sgarrison FILE *stream; 11812916Sgarrison { char c; 11912916Sgarrison while ((c=getc(stream)) != '\n' && c!=EOF) putchar(c); 12012916Sgarrison putchar('\n'); 12112916Sgarrison } 12212916Sgarrison 12312916Sgarrison /* getline(stream,p): store in *p next chars in stream up to \n 12412916Sgarrison advance stream past \n. 12512916Sgarrison limit of maxstr-1 chars may be stored at p. 12612916Sgarrison */ 12712916Sgarrison getline(stream,p) 12812916Sgarrison FILE *stream; 12912916Sgarrison char *p; 13012916Sgarrison { char *stop; 13112916Sgarrison stop= p+maxstr-1; 13212916Sgarrison while ( (*p= getc(stream)) != '\n' && *p!=EOF) 13312916Sgarrison if (p<stop) p++; 13412916Sgarrison *p= NULL; 13512916Sgarrison } 136