5
/* auto-resizing string library, standard I/O interface
7
/* #include <vstring_vstream.h>
9
/* int vstring_get(vp, fp)
13
/* int vstring_get_nonl(vp, fp)
17
/* int vstring_get_null(vp, fp)
21
/* int vstring_get_bound(vp, fp, bound)
26
/* int vstring_get_nonl_bound(vp, fp, bound)
31
/* int vstring_get_null_bound(vp, fp, bound)
36
/* The routines in this module each read one newline or null-terminated
37
/* string from an input stream. In all cases the result is either the
38
/* last character read, typically the record terminator, or VSTREAM_EOF.
40
/* vstring_get() reads one line from the named stream, including the
41
/* terminating newline character if present.
43
/* vstring_get_nonl() reads a line from the named stream and strips
44
/* the trailing newline character.
46
/* vstring_get_null() reads a null-terminated string from the named
49
/* the vstring_get<whatever>_bound() routines read no more
50
/* than \fIbound\fR characters. Otherwise they behave like the
51
/* unbounded versions documented above.
53
/* Fatal errors: memory allocation failure.
54
/* Panic: improper string bound.
58
/* The Secure Mailer license must be distributed with this software.
61
/* IBM T.J. Watson Research
63
/* Yorktown Heights, NY 10598, USA
72
/* Application-specific. */
77
#include "vstring_vstream.h"
80
* Macro to return the last character added to a VSTRING, for consistency.
82
#define VSTRING_GET_RESULT(vp) \
83
(VSTRING_LEN(vp) > 0 ? vstring_end(vp)[-1] : VSTREAM_EOF)
85
/* vstring_get - read line from file, keep newline */
87
int vstring_get(VSTRING *vp, VSTREAM *fp)
92
while ((c = VSTREAM_GETC(fp)) != VSTREAM_EOF) {
97
VSTRING_TERMINATE(vp);
98
return (VSTRING_GET_RESULT(vp));
101
/* vstring_get_nonl - read line from file, strip newline */
103
int vstring_get_nonl(VSTRING *vp, VSTREAM *fp)
108
while ((c = VSTREAM_GETC(fp)) != VSTREAM_EOF && c != '\n')
109
VSTRING_ADDCH(vp, c);
110
VSTRING_TERMINATE(vp);
111
return (c == '\n' ? c : VSTRING_GET_RESULT(vp));
114
/* vstring_get_null - read null-terminated string from file */
116
int vstring_get_null(VSTRING *vp, VSTREAM *fp)
121
while ((c = VSTREAM_GETC(fp)) != VSTREAM_EOF && c != 0)
122
VSTRING_ADDCH(vp, c);
123
VSTRING_TERMINATE(vp);
124
return (c == 0 ? c : VSTRING_GET_RESULT(vp));
127
/* vstring_get_bound - read line from file, keep newline, up to bound */
129
int vstring_get_bound(VSTRING *vp, VSTREAM *fp, int bound)
134
msg_panic("vstring_get_bound: invalid bound %d", bound);
137
while (bound-- > 0 && (c = VSTREAM_GETC(fp)) != VSTREAM_EOF) {
138
VSTRING_ADDCH(vp, c);
142
VSTRING_TERMINATE(vp);
143
return (VSTRING_GET_RESULT(vp));
146
/* vstring_get_nonl_bound - read line from file, strip newline, up to bound */
148
int vstring_get_nonl_bound(VSTRING *vp, VSTREAM *fp, int bound)
153
msg_panic("vstring_get_nonl_bound: invalid bound %d", bound);
156
while (bound-- > 0 && (c = VSTREAM_GETC(fp)) != VSTREAM_EOF && c != '\n')
157
VSTRING_ADDCH(vp, c);
158
VSTRING_TERMINATE(vp);
159
return (c == '\n' ? c : VSTRING_GET_RESULT(vp));
162
/* vstring_get_null_bound - read null-terminated string from file */
164
int vstring_get_null_bound(VSTRING *vp, VSTREAM *fp, int bound)
169
msg_panic("vstring_get_nonl_bound: invalid bound %d", bound);
172
while (bound-- > 0 && (c = VSTREAM_GETC(fp)) != VSTREAM_EOF && c != 0)
173
VSTRING_ADDCH(vp, c);
174
VSTRING_TERMINATE(vp);
175
return (c == 0 ? c : VSTRING_GET_RESULT(vp));
181
* Proof-of-concept test program: copy the source to this module to stdout.
185
#define TEXT_VSTREAM "vstring_vstream.c"
189
VSTRING *vp = vstring_alloc(1);
192
if ((fp = vstream_fopen(TEXT_VSTREAM, O_RDONLY, 0)) == 0)
193
msg_fatal("open %s: %m", TEXT_VSTREAM);
194
while (vstring_fgets(vp, fp))
195
vstream_fprintf(VSTREAM_OUT, "%s", vstring_str(vp));
197
vstream_fflush(VSTREAM_OUT);