xref: /original-bsd/contrib/bib/src/streams.c (revision e59fb703)
1 #ifndef lint
2 static char sccsid[] = "@(#)streams.c	2.4	03/05/87";
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 and any following non-blank lines that
14        don't begin with '%'
15     all words of the form %a are returned as null.
16     *p is a null terminated string (char p[maxstr]).
17 */
18 getword(stream,p,ignore)
19 FILE *stream;
20 char *p, *ignore;
21 {   char c;
22     char *oldp, *stop;
23     long save;
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)
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 	}
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 */
56 long int recsize(stream,start)
57 FILE *stream;
58 long 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 */
87 long int nextrecord(stream,x)
88 FILE *stream;
89 long 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        */
96         c= getc(stream);
97         for(;;)
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);
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 */
114 long int nextline(stream,x)
115 FILE *stream;
116 long 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 */
125 printline(stream)
126 FILE *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 */
136 getline(stream,p)
137 FILE *stream;
138 char *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 }
145 
146 /* replace string old at the head of subj by new */
147 strreplace(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 }
156