1 #ifndef lint 2 static char sccsid[] = "@(#)streams.c 2.5 05/27/93"; 3 #endif not lint 4 # 5 6 # include "stdio.h" 7 # include "streams.h" 8 # include "ctype.h" 9 # include "bib.h" 10 11 12 /* getword(stream,p,ignore): 13 read next sequence of nonspaces on current line into *p. 14 null if no more words on current line. 15 %x (x in ignore) terminates line and any following non-blank lines that 16 don't begin with '%' 17 all words of the form %a are returned as null. 18 *p is a null terminated string (char p[maxstr]). 19 */ 20 getword(stream,p,ignore,bolp) 21 FILE *stream; 22 char *p, *ignore; 23 int *bolp; 24 { int c; /* will always contain the last character seen */ 25 char *oldp, *stop; 26 long save; 27 int newbolp; 28 29 oldp= p; 30 stop= p+maxstr-1; 31 do{ c= getc(stream); 32 } while (isspace(c) && c!='\n'); 33 34 while (!isspace(c)) 35 { *p= c; 36 if (p < stop) p++; 37 c= getc(stream); 38 } 39 *p= NULL; 40 41 /* if line begins with %, then if following char is one to cause the 42 * line to be ignored, then skip to \n. If the following line is 43 * a continuation line, then skip it as well. 44 * small BUG in old version: ANY word that began with '%', whether 45 * at the beginning of the line or not, could cause the rest of 46 * the line to be ignored: this is not the advertised behavior. 47 * modified to ignore %x words, but they do not delete lines unless 48 * they occur at the beginning of lines. -ads 49 */ 50 if (*bolp) { 51 if (*oldp == '%') { 52 *oldp = NULL; 53 if (index(ignore, oldp[1]) != NULL) { 54 do { 55 while (c != '\n') c=getc(stream); 56 c= getc(stream); 57 } while (c != EOF && !isspace(c) && c != '%'); 58 ungetc(c, stream); 59 *bolp = true; 60 } 61 else *bolp = false; 62 } 63 } 64 else *bolp = (c == '\n' || c == EOF); 65 } 66 67 68 69 /* recsize(stream,start): 70 returns length of record beginning at start 71 (record ends at blank line or eof) 72 assumes and retains stream positioned at start 73 */ 74 long int recsize(stream,start) 75 FILE *stream; 76 long int start; 77 { char c; /* length = # of chars from start to beginning */ 78 long int length; /* of current line. c in current line. */ 79 int nonspaces; /* nonspaces = # of nonspaces in current line. */ 80 81 nonspaces= 0; 82 c= getc(stream); 83 length= 0L; 84 85 while ((c != '\n' || nonspaces != 0) && c != EOF) { 86 if (c == '\n') { 87 length= ftell(stream)-start; 88 nonspaces= 0; 89 } 90 else if (!isspace(c)) nonspaces++; 91 92 c= getc(stream); 93 } 94 95 pos(start); 96 return(length); 97 } 98 99 100 /* nextrecord(stream,x): seeks in stream for first non-blank line 101 at or after char x in stream. seeks to eof if x is past last record. 102 x is the index of a character in the file (not eof). 103 returns position in stream. (returns EOF, if seeks to EOF) 104 skips comment lines (those beginning with '#') 105 */ 106 long int nextrecord(stream,x) 107 FILE *stream; 108 long int x; 109 { long int start; /* position of the beginning of the line */ 110 char c; /* containing c */ 111 112 pos(x); 113 start= x; 114 /* find start of first non-blank record */ 115 c= getc(stream); 116 for(;;) 117 { if (c == '\n') { start= ftell(stream); c= getc(stream); } 118 else if (c == '#') { 119 /* skip any comment lines */ 120 while (c != '\n') c=getc(stream); 121 } 122 else if (isspace(c)) c= getc(stream); 123 else break; 124 } 125 126 if (feof(stream)) { pos(start); start= EOF; } 127 else pos(start); 128 return(start); 129 } 130 131 /* nextline(stream,x): seeks in stream after first newline at or after 132 char x in stream. seeks to eof if x is in last line. 133 x is the index of a character in the file (not eof). 134 returns position in stream 135 */ 136 long int nextline(stream,x) 137 FILE *stream; 138 long int x; 139 { pos(x); 140 while (getc(stream)!='\n') ; 141 return(ftell(stream)); 142 } 143 144 145 /* printline(stream): copies stream up to a newline 146 */ 147 printline(stream) 148 FILE *stream; 149 { char c; 150 while ((c=getc(stream)) != '\n' && c!=EOF) putchar(c); 151 putchar('\n'); 152 } 153 154 /* getline(stream,p): store in *p next chars in stream up to \n 155 advance stream past \n. 156 limit of maxstr-1 chars may be stored at p. 157 */ 158 getline(stream,p) 159 FILE *stream; 160 char *p; 161 { char *stop; 162 stop= p+maxstr-1; 163 while ( (*p= getc(stream)) != '\n' && *p!=EOF) 164 if (p<stop) p++; 165 *p= NULL; 166 } 167 168 /* replace string old at the head of subj by new */ 169 strreplace(subj, old, new) 170 char *subj, *old, *new; 171 { 172 char buf[128]; 173 int lg; 174 strcpy(buf, &subj[strlen(old)]); 175 strcpy(subj, new); 176 strcat(subj, buf); 177 } 178