1 #ifndef lint 2 static char sccsid[] = "@(#)streams.c 2.2 09/23/83"; 3 #endif not lint 4 # 5 6 # include "stdio.h" 7 # include "streams.h" 8 # include "ctype.h" 9 10 /* getword(stream,p,ignore): 11 read next sequence of nonspaces on current line into *p. 12 null if no more words on current line. 13 %x (x in ignore) terminates line. 14 all words of the form %a are returned as null. 15 *p is a null terminated string (char p[maxstr]). 16 */ 17 getword(stream,p,ignore) 18 FILE *stream; 19 char *p, *ignore; 20 { char c; 21 char *oldp, *stop; 22 23 oldp= p; 24 stop= p+maxstr-1; 25 do{ c= getc(stream); 26 } while (isspace(c) && c!='\n'); 27 28 while (!isspace(c)) 29 { *p= c; 30 if (p < stop) p++; 31 c= getc(stream); 32 } 33 *p= NULL; 34 35 if (oldp[0]=='%') 36 { oldp[0]= NULL; 37 if (index(ignore, oldp[1]) != NULL) 38 while (c!='\n') c=getc(stream); 39 } 40 } 41 42 43 44 /* recsize(stream,start): 45 returns length of record beginning at start 46 (record ends at blank line or eof) 47 assumes and retains stream positioned at start 48 */ 49 long int recsize(stream,start) 50 FILE *stream; 51 long int start; 52 { char c; /* length = # of chars from start to beginning */ 53 long int length; /* of current line. c in current line. */ 54 int nonspaces; /* nonspaces = # of nonspaces in current line. */ 55 56 nonspaces= 0; 57 c= getc(stream); 58 length= 0L; 59 60 while ( (c!='\n' || nonspaces!=0) && c!=EOF) 61 { if (c=='\n') 62 { length= ftell(stream)-start; 63 nonspaces= 0; 64 } 65 else if (!isspace(c)) nonspaces++; 66 67 c= getc(stream); 68 } 69 70 pos(start); 71 return(length); 72 } 73 74 75 /* nextrecord(stream,x): seeks in stream for first non-blank line 76 at or after char x in stream. seeks to eof if x is past last record. 77 x is the index of a character in the file (not eof). 78 returns position in stream. (returns EOF, if seeks to EOF) 79 */ 80 long int nextrecord(stream,x) 81 FILE *stream; 82 long int x; 83 { long int start; /* position of the beginning of the line */ 84 char c; /* containing c */ 85 86 pos(x); 87 start= x; 88 /* find start of first non-blank record */ 89 for(;;) 90 { c= getc(stream); 91 if (c=='\n') start= ftell(stream); 92 else if (!isspace(c)) break; 93 } 94 95 if (feof(stream)) { pos(start); start= EOF; } 96 else pos(start); 97 return(start); 98 } 99 100 /* nextline(stream,x): seeks in stream after first newline at or after 101 char x in stream. seeks to eof if x is in last line. 102 x is the index of a character in the file (not eof). 103 returns position in stream 104 */ 105 long int nextline(stream,x) 106 FILE *stream; 107 long int x; 108 { pos(x); 109 while (getc(stream)!='\n') ; 110 return(ftell(stream)); 111 } 112 113 114 /* printline(stream): copies stream up to a newline 115 */ 116 printline(stream) 117 FILE *stream; 118 { char c; 119 while ((c=getc(stream)) != '\n' && c!=EOF) putchar(c); 120 putchar('\n'); 121 } 122 123 /* getline(stream,p): store in *p next chars in stream up to \n 124 advance stream past \n. 125 limit of maxstr-1 chars may be stored at p. 126 */ 127 getline(stream,p) 128 FILE *stream; 129 char *p; 130 { char *stop; 131 stop= p+maxstr-1; 132 while ( (*p= getc(stream)) != '\n' && *p!=EOF) 133 if (p<stop) p++; 134 *p= NULL; 135 } 136