BSD 4_3_Reno release
[unix-history] / usr / src / contrib / bib / src / streams.c
CommitLineData
d3ce25cd 1#ifndef lint
ca67e7b4 2static char sccsid[] = "@(#)streams.c 2.4 3/5/87";
d3ce25cd 3#endif not lint
db589048 4#
a6e63b04
GL
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.
58b36083
GL
13 %x (x in ignore) terminates line and any following non-blank lines that
14 don't begin with '%'
a6e63b04
GL
15 all words of the form %a are returned as null.
16 *p is a null terminated string (char p[maxstr]).
17*/
18getword(stream,p,ignore)
19FILE *stream;
20char *p, *ignore;
21{ char c;
22 char *oldp, *stop;
58b36083 23 long save;
a6e63b04
GL
24
25 oldp= p;
26 stop= p+maxstr-1;
27 do{ c= getc(stream);
28 } while (isspace(c) && c!='\n');
29
30 while (!isspace(c))
31 { *p= c;
32 if (p < stop) p++;
33 c= getc(stream);
34 }
35 *p= NULL;
36
37 if (oldp[0]=='%')
38 { oldp[0]= NULL;
39 if (index(ignore, oldp[1]) != NULL)
58b36083
GL
40 { do{ while (c!='\n') c=getc(stream);
41 save= ftell(stream);
42 c= getc(stream);
43 } while (c!= EOF && !isspace(c) && c!='%');
44 pos(save);
45 }
a6e63b04
GL
46 }
47}
48
49
50
51/* recsize(stream,start):
52 returns length of record beginning at start
53 (record ends at blank line or eof)
54 assumes and retains stream positioned at start
55*/
56long int recsize(stream,start)
57FILE *stream;
58long int start;
59{ char c; /* length = # of chars from start to beginning */
60 long int length; /* of current line. c in current line. */
61 int nonspaces; /* nonspaces = # of nonspaces in current line. */
62
63 nonspaces= 0;
64 c= getc(stream);
65 length= 0L;
66
67 while ( (c!='\n' || nonspaces!=0) && c!=EOF)
68 { if (c=='\n')
69 { length= ftell(stream)-start;
70 nonspaces= 0;
71 }
72 else if (!isspace(c)) nonspaces++;
73
74 c= getc(stream);
75 }
76
77 pos(start);
78 return(length);
79}
80
81
82/* nextrecord(stream,x): seeks in stream for first non-blank line
83 at or after char x in stream. seeks to eof if x is past last record.
84 x is the index of a character in the file (not eof).
85 returns position in stream. (returns EOF, if seeks to EOF)
86*/
87long int nextrecord(stream,x)
88FILE *stream;
89long int x;
90{ long int start; /* position of the beginning of the line */
91 char c; /* containing c */
92
93 pos(x);
94 start= x;
95 /* find start of first non-blank record */
58b36083 96 c= getc(stream);
a6e63b04 97 for(;;)
58b36083
GL
98 { if (c=='\n') {start= ftell(stream); c= getc(stream);}
99 else if (c=='#') while (c!='\n') c=getc(stream);
100 else if (!isspace(c)) break;
101 else c= getc(stream);
a6e63b04
GL
102 }
103
104 if (feof(stream)) { pos(start); start= EOF; }
105 else pos(start);
106 return(start);
107}
108
109/* nextline(stream,x): seeks in stream after first newline at or after
110 char x in stream. seeks to eof if x is in last line.
111 x is the index of a character in the file (not eof).
112 returns position in stream
113*/
114long int nextline(stream,x)
115FILE *stream;
116long int x;
117{ pos(x);
118 while (getc(stream)!='\n') ;
119 return(ftell(stream));
120}
121
122
123/* printline(stream): copies stream up to a newline
124*/
125printline(stream)
126FILE *stream;
127{ char c;
128 while ((c=getc(stream)) != '\n' && c!=EOF) putchar(c);
129 putchar('\n');
130}
131
132/* getline(stream,p): store in *p next chars in stream up to \n
133 advance stream past \n.
134 limit of maxstr-1 chars may be stored at p.
135*/
136getline(stream,p)
137FILE *stream;
138char *p;
139{ char *stop;
140 stop= p+maxstr-1;
141 while ( (*p= getc(stream)) != '\n' && *p!=EOF)
142 if (p<stop) p++;
143 *p= NULL;
144}
857232a7
RH
145
146/* replace string old at the head of subj by new */
147strreplace(subj, old, new)
148 char *subj, *old, *new;
149{
150 char buf[128];
151 int lg;
152 strcpy(buf, &subj[strlen(old)]);
153 strcpy(subj, new);
154 strcat(subj, buf);
155}