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