xref: /csrg-svn/contrib/bib/src/streams.c (revision 13111)
1*13111Srrh #ifndef lint
2*13111Srrh static char sccsid[] = "@(#)streams.c	1.2	06/14/83";
3*13111Srrh #endif not lint
412916Sgarrison 
512916Sgarrison # include "stdio.h"
612916Sgarrison # include "streams.h"
712916Sgarrison # include "ctype.h"
812916Sgarrison 
912916Sgarrison /*  getword(stream,p,ignore):
1012916Sgarrison         read next sequence of nonspaces on current line into *p.
1112916Sgarrison     null if no more words on current line.
1212916Sgarrison     %x (x in ignore) terminates line.
1312916Sgarrison     all words of the form %a are returned as null.
1412916Sgarrison     *p is a null terminated string (char p[maxstr]).
1512916Sgarrison */
1612916Sgarrison getword(stream,p,ignore)
1712916Sgarrison FILE *stream;
1812916Sgarrison char *p, *ignore;
1912916Sgarrison {   char c;
2012916Sgarrison     char *oldp, *stop;
2112916Sgarrison 
2212916Sgarrison     oldp= p;
2312916Sgarrison     stop= p+maxstr-1;
2412916Sgarrison     do{ c= getc(stream);
2512916Sgarrison     }   while (isspace(c) && c!='\n');
2612916Sgarrison 
2712916Sgarrison     while (!isspace(c))
2812916Sgarrison     {   *p= c;
2912916Sgarrison         if (p < stop)  p++;
3012916Sgarrison         c= getc(stream);
3112916Sgarrison     }
3212916Sgarrison     *p= NULL;
3312916Sgarrison 
3412916Sgarrison     if (oldp[0]=='%')
3512916Sgarrison     {   oldp[0]= NULL;
3612916Sgarrison         if (index(ignore, oldp[1]) != NULL)
3712916Sgarrison             while (c!='\n') c=getc(stream);
3812916Sgarrison     }
3912916Sgarrison }
4012916Sgarrison 
4112916Sgarrison 
4212916Sgarrison 
4312916Sgarrison /*  recsize(stream,start):
4412916Sgarrison     returns length of record beginning at start
4512916Sgarrison     (record ends at blank line or eof)
4612916Sgarrison     assumes and retains stream positioned at start
4712916Sgarrison */
4812916Sgarrison long int recsize(stream,start)
4912916Sgarrison FILE *stream;
5012916Sgarrison long int start;
5112916Sgarrison {   char c;                 /*  length = # of chars from start to beginning */
5212916Sgarrison     long int length;        /*  of current line.  c in current line.        */
5312916Sgarrison     int nonspaces;          /*  nonspaces = # of nonspaces in current line. */
5412916Sgarrison 
5512916Sgarrison     nonspaces= 0;
5612916Sgarrison     c= getc(stream);
5712916Sgarrison     length= 0L;
5812916Sgarrison 
5912916Sgarrison     while ( (c!='\n' || nonspaces!=0) && c!=EOF)
6012916Sgarrison     {   if      (c=='\n')
6112916Sgarrison         {   length= ftell(stream)-start;
6212916Sgarrison             nonspaces= 0;
6312916Sgarrison         }
6412916Sgarrison         else if (!isspace(c))    nonspaces++;
6512916Sgarrison 
6612916Sgarrison         c= getc(stream);
6712916Sgarrison     }
6812916Sgarrison 
6912916Sgarrison     pos(start);
7012916Sgarrison     return(length);
7112916Sgarrison }
7212916Sgarrison 
7312916Sgarrison 
7412916Sgarrison /*  nextrecord(stream,x): seeks in stream for first non-blank line
7512916Sgarrison         at or after char x in stream. seeks to eof if x is past last record.
7612916Sgarrison         x is the index of a character in the file (not eof).
7712916Sgarrison     returns position in stream.  (returns EOF, if seeks to EOF)
7812916Sgarrison */
7912916Sgarrison long int nextrecord(stream,x)
8012916Sgarrison FILE *stream;
8112916Sgarrison long int x;
8212916Sgarrison {   long int start;         /*  position of the beginning of the line  */
8312916Sgarrison     char c;                 /*      containing c                       */
8412916Sgarrison 
8512916Sgarrison     pos(x);
8612916Sgarrison     start= x;
8712916Sgarrison     /*  find start of first non-blank record        */
8812916Sgarrison         for(;;)
8912916Sgarrison         {   c= getc(stream);
9012916Sgarrison             if      (c=='\n')           start= ftell(stream);
9112916Sgarrison             else if (!isspace(c))       break;
9212916Sgarrison         }
9312916Sgarrison 
9412916Sgarrison     if (feof(stream))   { pos(start);  start= EOF;  }
9512916Sgarrison     else                pos(start);
9612916Sgarrison     return(start);
9712916Sgarrison }
9812916Sgarrison 
9912916Sgarrison /*  nextline(stream,x): seeks in stream after first newline at or after
10012916Sgarrison         char x in stream. seeks to eof if x is in last line.
10112916Sgarrison         x is the index of a character in the file (not eof).
10212916Sgarrison     returns position in stream
10312916Sgarrison */
10412916Sgarrison long int nextline(stream,x)
10512916Sgarrison FILE *stream;
10612916Sgarrison long int x;
10712916Sgarrison {   pos(x);
10812916Sgarrison     while (getc(stream)!='\n') ;
10912916Sgarrison     return(ftell(stream));
11012916Sgarrison }
11112916Sgarrison 
11212916Sgarrison 
11312916Sgarrison /*  printline(stream): copies stream up to a newline
11412916Sgarrison */
11512916Sgarrison printline(stream)
11612916Sgarrison FILE *stream;
11712916Sgarrison {   char c;
11812916Sgarrison     while ((c=getc(stream)) != '\n' && c!=EOF)  putchar(c);
11912916Sgarrison     putchar('\n');
12012916Sgarrison }
12112916Sgarrison 
12212916Sgarrison /*  getline(stream,p):  store in *p next chars in stream up to \n
12312916Sgarrison         advance stream past \n.
12412916Sgarrison     limit of  maxstr-1 chars may be stored at p.
12512916Sgarrison */
12612916Sgarrison getline(stream,p)
12712916Sgarrison FILE *stream;
12812916Sgarrison char *p;
12912916Sgarrison {   char *stop;
13012916Sgarrison     stop= p+maxstr-1;
13112916Sgarrison     while ( (*p= getc(stream)) != '\n' && *p!=EOF)
13212916Sgarrison         if (p<stop)    p++;
13312916Sgarrison     *p= NULL;
13412916Sgarrison }
135