113111Srrh #ifndef lint 2*30589Sgarrison static char sccsid[] = "@(#)streams.c 2.4 03/05/87"; 313111Srrh #endif not lint 415063Sgarrison # 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. 13*30589Sgarrison %x (x in ignore) terminates line and any following non-blank lines that 14*30589Sgarrison don't begin with '%' 1512916Sgarrison all words of the form %a are returned as null. 1612916Sgarrison *p is a null terminated string (char p[maxstr]). 1712916Sgarrison */ 1812916Sgarrison getword(stream,p,ignore) 1912916Sgarrison FILE *stream; 2012916Sgarrison char *p, *ignore; 2112916Sgarrison { char c; 2212916Sgarrison char *oldp, *stop; 23*30589Sgarrison long save; 2412916Sgarrison 2512916Sgarrison oldp= p; 2612916Sgarrison stop= p+maxstr-1; 2712916Sgarrison do{ c= getc(stream); 2812916Sgarrison } while (isspace(c) && c!='\n'); 2912916Sgarrison 3012916Sgarrison while (!isspace(c)) 3112916Sgarrison { *p= c; 3212916Sgarrison if (p < stop) p++; 3312916Sgarrison c= getc(stream); 3412916Sgarrison } 3512916Sgarrison *p= NULL; 3612916Sgarrison 3712916Sgarrison if (oldp[0]=='%') 3812916Sgarrison { oldp[0]= NULL; 3912916Sgarrison if (index(ignore, oldp[1]) != NULL) 40*30589Sgarrison { do{ while (c!='\n') c=getc(stream); 41*30589Sgarrison save= ftell(stream); 42*30589Sgarrison c= getc(stream); 43*30589Sgarrison } while (c!= EOF && !isspace(c) && c!='%'); 44*30589Sgarrison pos(save); 45*30589Sgarrison } 4612916Sgarrison } 4712916Sgarrison } 4812916Sgarrison 4912916Sgarrison 5012916Sgarrison 5112916Sgarrison /* recsize(stream,start): 5212916Sgarrison returns length of record beginning at start 5312916Sgarrison (record ends at blank line or eof) 5412916Sgarrison assumes and retains stream positioned at start 5512916Sgarrison */ 5612916Sgarrison long int recsize(stream,start) 5712916Sgarrison FILE *stream; 5812916Sgarrison long int start; 5912916Sgarrison { char c; /* length = # of chars from start to beginning */ 6012916Sgarrison long int length; /* of current line. c in current line. */ 6112916Sgarrison int nonspaces; /* nonspaces = # of nonspaces in current line. */ 6212916Sgarrison 6312916Sgarrison nonspaces= 0; 6412916Sgarrison c= getc(stream); 6512916Sgarrison length= 0L; 6612916Sgarrison 6712916Sgarrison while ( (c!='\n' || nonspaces!=0) && c!=EOF) 6812916Sgarrison { if (c=='\n') 6912916Sgarrison { length= ftell(stream)-start; 7012916Sgarrison nonspaces= 0; 7112916Sgarrison } 7212916Sgarrison else if (!isspace(c)) nonspaces++; 7312916Sgarrison 7412916Sgarrison c= getc(stream); 7512916Sgarrison } 7612916Sgarrison 7712916Sgarrison pos(start); 7812916Sgarrison return(length); 7912916Sgarrison } 8012916Sgarrison 8112916Sgarrison 8212916Sgarrison /* nextrecord(stream,x): seeks in stream for first non-blank line 8312916Sgarrison at or after char x in stream. seeks to eof if x is past last record. 8412916Sgarrison x is the index of a character in the file (not eof). 8512916Sgarrison returns position in stream. (returns EOF, if seeks to EOF) 8612916Sgarrison */ 8712916Sgarrison long int nextrecord(stream,x) 8812916Sgarrison FILE *stream; 8912916Sgarrison long int x; 9012916Sgarrison { long int start; /* position of the beginning of the line */ 9112916Sgarrison char c; /* containing c */ 9212916Sgarrison 9312916Sgarrison pos(x); 9412916Sgarrison start= x; 9512916Sgarrison /* find start of first non-blank record */ 96*30589Sgarrison c= getc(stream); 9712916Sgarrison for(;;) 98*30589Sgarrison { if (c=='\n') {start= ftell(stream); c= getc(stream);} 99*30589Sgarrison else if (c=='#') while (c!='\n') c=getc(stream); 100*30589Sgarrison else if (!isspace(c)) break; 101*30589Sgarrison else c= getc(stream); 10212916Sgarrison } 10312916Sgarrison 10412916Sgarrison if (feof(stream)) { pos(start); start= EOF; } 10512916Sgarrison else pos(start); 10612916Sgarrison return(start); 10712916Sgarrison } 10812916Sgarrison 10912916Sgarrison /* nextline(stream,x): seeks in stream after first newline at or after 11012916Sgarrison char x in stream. seeks to eof if x is in last line. 11112916Sgarrison x is the index of a character in the file (not eof). 11212916Sgarrison returns position in stream 11312916Sgarrison */ 11412916Sgarrison long int nextline(stream,x) 11512916Sgarrison FILE *stream; 11612916Sgarrison long int x; 11712916Sgarrison { pos(x); 11812916Sgarrison while (getc(stream)!='\n') ; 11912916Sgarrison return(ftell(stream)); 12012916Sgarrison } 12112916Sgarrison 12212916Sgarrison 12312916Sgarrison /* printline(stream): copies stream up to a newline 12412916Sgarrison */ 12512916Sgarrison printline(stream) 12612916Sgarrison FILE *stream; 12712916Sgarrison { char c; 12812916Sgarrison while ((c=getc(stream)) != '\n' && c!=EOF) putchar(c); 12912916Sgarrison putchar('\n'); 13012916Sgarrison } 13112916Sgarrison 13212916Sgarrison /* getline(stream,p): store in *p next chars in stream up to \n 13312916Sgarrison advance stream past \n. 13412916Sgarrison limit of maxstr-1 chars may be stored at p. 13512916Sgarrison */ 13612916Sgarrison getline(stream,p) 13712916Sgarrison FILE *stream; 13812916Sgarrison char *p; 13912916Sgarrison { char *stop; 14012916Sgarrison stop= p+maxstr-1; 14112916Sgarrison while ( (*p= getc(stream)) != '\n' && *p!=EOF) 14212916Sgarrison if (p<stop) p++; 14312916Sgarrison *p= NULL; 14412916Sgarrison } 14517249Srrh 14617249Srrh /* replace string old at the head of subj by new */ 14717249Srrh strreplace(subj, old, new) 14817249Srrh char *subj, *old, *new; 14917249Srrh { 15017249Srrh char buf[128]; 15117249Srrh int lg; 15217249Srrh strcpy(buf, &subj[strlen(old)]); 15317249Srrh strcpy(subj, new); 15417249Srrh strcat(subj, buf); 15517249Srrh } 156