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