xref: /csrg-svn/contrib/bib/src/streams.c (revision 60507)
113111Srrh #ifndef lint
2*60507Sbostic static char sccsid[] = "@(#)streams.c	2.5	05/27/93";
313111Srrh #endif not lint
415063Sgarrison #
512916Sgarrison 
612916Sgarrison # include "stdio.h"
712916Sgarrison # include "streams.h"
812916Sgarrison # include "ctype.h"
9*60507Sbostic # include "bib.h"
1012916Sgarrison 
11*60507Sbostic 
1212916Sgarrison /*  getword(stream,p,ignore):
1312916Sgarrison         read next sequence of nonspaces on current line into *p.
1412916Sgarrison     null if no more words on current line.
1530589Sgarrison     %x (x in ignore) terminates line and any following non-blank lines that
1630589Sgarrison        don't begin with '%'
1712916Sgarrison     all words of the form %a are returned as null.
1812916Sgarrison     *p is a null terminated string (char p[maxstr]).
1912916Sgarrison */
getword(stream,p,ignore,bolp)20*60507Sbostic getword(stream,p,ignore,bolp)
2112916Sgarrison FILE *stream;
2212916Sgarrison char *p, *ignore;
23*60507Sbostic int *bolp;
24*60507Sbostic {   int c; /* will always contain the last character seen */
2512916Sgarrison     char *oldp, *stop;
2630589Sgarrison     long save;
27*60507Sbostic     int newbolp;
2812916Sgarrison 
2912916Sgarrison     oldp= p;
3012916Sgarrison     stop= p+maxstr-1;
3112916Sgarrison     do{ c= getc(stream);
3212916Sgarrison     }   while (isspace(c) && c!='\n');
3312916Sgarrison 
3412916Sgarrison     while (!isspace(c))
3512916Sgarrison     {   *p= c;
3612916Sgarrison         if (p < stop)  p++;
3712916Sgarrison         c= getc(stream);
3812916Sgarrison     }
3912916Sgarrison     *p= NULL;
4012916Sgarrison 
41*60507Sbostic     /* if line begins with %, then if following char is one to cause the
42*60507Sbostic      * line to be ignored, then skip to \n.  If the following line is
43*60507Sbostic      * a continuation line, then skip it as well.
44*60507Sbostic      * small BUG in old version: ANY word that began with '%', whether
45*60507Sbostic      * at the beginning of the line or not, could cause the rest of
46*60507Sbostic      * the line to be ignored: this is not the advertised behavior.
47*60507Sbostic      * modified to ignore %x words, but they do not delete lines unless
48*60507Sbostic      * they occur at the beginning of lines.  -ads
49*60507Sbostic      */
50*60507Sbostic    if (*bolp) {
51*60507Sbostic       if (*oldp == '%') {
52*60507Sbostic 	 *oldp = NULL;
53*60507Sbostic 	 if (index(ignore, oldp[1]) != NULL) {
54*60507Sbostic 	    do {
55*60507Sbostic 	       while (c != '\n') c=getc(stream);
56*60507Sbostic 	       c= getc(stream);
57*60507Sbostic 	       }   while (c != EOF && !isspace(c) && c != '%');
58*60507Sbostic 	    ungetc(c, stream);
59*60507Sbostic 	    *bolp = true;
60*60507Sbostic 	    }
61*60507Sbostic 	 else *bolp = false;
62*60507Sbostic 	 }
63*60507Sbostic       }
64*60507Sbostic    else *bolp = (c == '\n' || c == EOF);
6512916Sgarrison }
6612916Sgarrison 
6712916Sgarrison 
6812916Sgarrison 
6912916Sgarrison /*  recsize(stream,start):
7012916Sgarrison     returns length of record beginning at start
7112916Sgarrison     (record ends at blank line or eof)
7212916Sgarrison     assumes and retains stream positioned at start
7312916Sgarrison */
recsize(stream,start)7412916Sgarrison long int recsize(stream,start)
7512916Sgarrison FILE *stream;
7612916Sgarrison long int start;
7712916Sgarrison {   char c;                 /*  length = # of chars from start to beginning */
7812916Sgarrison     long int length;        /*  of current line.  c in current line.        */
7912916Sgarrison     int nonspaces;          /*  nonspaces = # of nonspaces in current line. */
8012916Sgarrison 
8112916Sgarrison     nonspaces= 0;
8212916Sgarrison     c= getc(stream);
8312916Sgarrison     length= 0L;
8412916Sgarrison 
85*60507Sbostic     while ((c != '\n' || nonspaces != 0) && c != EOF) {
86*60507Sbostic       if (c == '\n') {
87*60507Sbostic 	 length= ftell(stream)-start;
88*60507Sbostic          nonspaces= 0;
89*60507Sbostic 	 }
90*60507Sbostic       else if (!isspace(c)) nonspaces++;
9112916Sgarrison 
92*60507Sbostic       c= getc(stream);
93*60507Sbostic       }
9412916Sgarrison 
9512916Sgarrison     pos(start);
9612916Sgarrison     return(length);
9712916Sgarrison }
9812916Sgarrison 
9912916Sgarrison 
10012916Sgarrison /*  nextrecord(stream,x): seeks in stream for first non-blank line
10112916Sgarrison         at or after char x in stream. seeks to eof if x is past last record.
10212916Sgarrison         x is the index of a character in the file (not eof).
10312916Sgarrison     returns position in stream.  (returns EOF, if seeks to EOF)
104*60507Sbostic     skips comment lines (those beginning with '#')
10512916Sgarrison */
nextrecord(stream,x)10612916Sgarrison long int nextrecord(stream,x)
10712916Sgarrison FILE *stream;
10812916Sgarrison long int x;
10912916Sgarrison {   long int start;         /*  position of the beginning of the line  */
11012916Sgarrison     char c;                 /*      containing c                       */
11112916Sgarrison 
11212916Sgarrison     pos(x);
11312916Sgarrison     start= x;
11412916Sgarrison     /*  find start of first non-blank record        */
11530589Sgarrison         c= getc(stream);
11612916Sgarrison         for(;;)
117*60507Sbostic         {   if (c == '\n') { start= ftell(stream); c= getc(stream); }
118*60507Sbostic 	    else if (c == '#') {
119*60507Sbostic 	       /* skip any comment lines */
120*60507Sbostic 	       while (c != '\n') c=getc(stream);
121*60507Sbostic 	       }
122*60507Sbostic             else if (isspace(c)) c= getc(stream);
123*60507Sbostic 	    else                 break;
12412916Sgarrison         }
12512916Sgarrison 
12612916Sgarrison     if (feof(stream))   { pos(start);  start= EOF;  }
12712916Sgarrison     else                pos(start);
12812916Sgarrison     return(start);
12912916Sgarrison }
13012916Sgarrison 
13112916Sgarrison /*  nextline(stream,x): seeks in stream after first newline at or after
13212916Sgarrison         char x in stream. seeks to eof if x is in last line.
13312916Sgarrison         x is the index of a character in the file (not eof).
13412916Sgarrison     returns position in stream
13512916Sgarrison */
nextline(stream,x)13612916Sgarrison long int nextline(stream,x)
13712916Sgarrison FILE *stream;
13812916Sgarrison long int x;
13912916Sgarrison {   pos(x);
14012916Sgarrison     while (getc(stream)!='\n') ;
14112916Sgarrison     return(ftell(stream));
14212916Sgarrison }
14312916Sgarrison 
14412916Sgarrison 
14512916Sgarrison /*  printline(stream): copies stream up to a newline
14612916Sgarrison */
printline(stream)14712916Sgarrison printline(stream)
14812916Sgarrison FILE *stream;
14912916Sgarrison {   char c;
15012916Sgarrison     while ((c=getc(stream)) != '\n' && c!=EOF)  putchar(c);
15112916Sgarrison     putchar('\n');
15212916Sgarrison }
15312916Sgarrison 
15412916Sgarrison /*  getline(stream,p):  store in *p next chars in stream up to \n
15512916Sgarrison         advance stream past \n.
15612916Sgarrison     limit of  maxstr-1 chars may be stored at p.
15712916Sgarrison */
getline(stream,p)15812916Sgarrison getline(stream,p)
15912916Sgarrison FILE *stream;
16012916Sgarrison char *p;
16112916Sgarrison {   char *stop;
16212916Sgarrison     stop= p+maxstr-1;
16312916Sgarrison     while ( (*p= getc(stream)) != '\n' && *p!=EOF)
16412916Sgarrison         if (p<stop)    p++;
16512916Sgarrison     *p= NULL;
16612916Sgarrison }
16717249Srrh 
16817249Srrh /* replace string old at the head of subj by new */
strreplace(subj,old,new)16917249Srrh strreplace(subj, old, new)
17017249Srrh 	char *subj, *old, *new;
17117249Srrh {
17217249Srrh 	char buf[128];
17317249Srrh 	int lg;
17417249Srrh 	strcpy(buf, &subj[strlen(old)]);
17517249Srrh 	strcpy(subj, new);
17617249Srrh 	strcat(subj, buf);
17717249Srrh }
178