summaryrefslogtreecommitdiffstats
path: root/src/util/vstring_vstream.c
blob: 451cc501784e9ebb46a045035b046939e299bcf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*++
/* NAME
/*	vstring_vstream 3
/* SUMMARY
/*	auto-resizing string library, standard I/O interface
/* SYNOPSIS
/*	#include <vstring_vstream.h>
/*
/*	int	vstring_get_flags(vp, fp, flags)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	int	flags
/*
/*	int	vstring_get_flags_nonl(vp, fp, flags)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	int	flags
/*
/*	int	vstring_get_flags_null(vp, fp, flags)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	int	flags
/*
/*	int	vstring_get_flags_bound(vp, fp, flags, bound)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	ssize_t	bound;
/*	int	flags
/*
/*	int	vstring_get_flags_nonl_bound(vp, fp, flags, bound)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	ssize_t	bound;
/*	int	flags
/*
/*	int	vstring_get_flags_null_bound(vp, fp, flags, bound)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	ssize_t	bound;
/*	int	flags
/* CONVENIENCE API
/*	int	vstring_get(vp, fp)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*
/*	int	vstring_get_nonl(vp, fp)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*
/*	int	vstring_get_null(vp, fp)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*
/*	int	vstring_get_bound(vp, fp, bound)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	ssize_t	bound;
/*
/*	int	vstring_get_nonl_bound(vp, fp, bound)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	ssize_t	bound;
/*
/*	int	vstring_get_null_bound(vp, fp, bound)
/*	VSTRING	*vp;
/*	VSTREAM	*fp;
/*	ssize_t	bound;
/* DESCRIPTION
/*	The routines in this module each read one newline or null-terminated
/*	string from an input stream. In all cases the result is either the
/*	last character read, typically the record terminator, or VSTREAM_EOF.
/*	The flags argument is VSTRING_GET_FLAG_NONE (default) or
/*	VSTRING_GET_FLAG_APPEND (append instead of overwrite).
/*
/*	vstring_get_flags() reads one line from the named stream, including the
/*	terminating newline character if present.
/*
/*	vstring_get_flags_nonl() reads a line from the named stream and strips
/*	the trailing newline character.
/*
/*	vstring_get_flags_null() reads a null-terminated string from the named
/*	stream.
/*
/*	the vstring_get_flags<whatever>_bound() routines read no more
/*	than \fIbound\fR characters.  Otherwise they behave like the
/*	unbounded versions documented above.
/*
/*	The functions without _flags in their name accept the same
/*	arguments except flags. These functions use the default
/*	flags value.
/* DIAGNOSTICS
/*	Fatal errors: memory allocation failure.
/*	Panic: improper string bound.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System library. */

#include "sys_defs.h"
#include <stdio.h>
#include <string.h>

/* Application-specific. */

#include "msg.h"
#include "vstring.h"
#include "vstream.h"
#include "vstring_vstream.h"

 /*
  * Macro to return the last character added to a VSTRING, for consistency.
  */
#define VSTRING_GET_RESULT(vp, baselen) \
    (VSTRING_LEN(vp) > (base_len) ? vstring_end(vp)[-1] : VSTREAM_EOF)

/* vstring_get_flags - read line from file, keep newline */

int     vstring_get_flags(VSTRING *vp, VSTREAM *fp, int flags)
{
    int     c;
    ssize_t base_len;

    if ((flags & VSTRING_GET_FLAG_APPEND) == 0)
	VSTRING_RESET(vp);
    base_len = VSTRING_LEN(vp);
    while ((c = VSTREAM_GETC(fp)) != VSTREAM_EOF) {
	VSTRING_ADDCH(vp, c);
	if (c == '\n')
	    break;
    }
    VSTRING_TERMINATE(vp);
    return (VSTRING_GET_RESULT(vp, baselen));
}

/* vstring_get_flags_nonl - read line from file, strip newline */

int     vstring_get_flags_nonl(VSTRING *vp, VSTREAM *fp, int flags)
{
    int     c;
    ssize_t base_len;

    if ((flags & VSTRING_GET_FLAG_APPEND) == 0)
	VSTRING_RESET(vp);
    base_len = VSTRING_LEN(vp);
    while ((c = VSTREAM_GETC(fp)) != VSTREAM_EOF && c != '\n')
	VSTRING_ADDCH(vp, c);
    VSTRING_TERMINATE(vp);
    return (c == '\n' ? c : VSTRING_GET_RESULT(vp, baselen));
}

/* vstring_get_flags_null - read null-terminated string from file */

int     vstring_get_flags_null(VSTRING *vp, VSTREAM *fp, int flags)
{
    int     c;
    ssize_t base_len;

    if ((flags & VSTRING_GET_FLAG_APPEND) == 0)
	VSTRING_RESET(vp);
    base_len = VSTRING_LEN(vp);
    while ((c = VSTREAM_GETC(fp)) != VSTREAM_EOF && c != 0)
	VSTRING_ADDCH(vp, c);
    VSTRING_TERMINATE(vp);
    return (c == 0 ? c : VSTRING_GET_RESULT(vp, baselen));
}

/* vstring_get_flags_bound - read line from file, keep newline, up to bound */

int     vstring_get_flags_bound(VSTRING *vp, VSTREAM *fp, int flags,
				        ssize_t bound)
{
    int     c;
    ssize_t base_len;

    if (bound <= 0)
	msg_panic("vstring_get_bound: invalid bound %ld", (long) bound);

    if ((flags & VSTRING_GET_FLAG_APPEND) == 0)
	VSTRING_RESET(vp);
    base_len = VSTRING_LEN(vp);
    while (bound-- > 0 && (c = VSTREAM_GETC(fp)) != VSTREAM_EOF) {
	VSTRING_ADDCH(vp, c);
	if (c == '\n')
	    break;
    }
    VSTRING_TERMINATE(vp);
    return (VSTRING_GET_RESULT(vp, baselen));
}

/* vstring_get_flags_nonl_bound - read line from file, strip newline, up to bound */

int     vstring_get_flags_nonl_bound(VSTRING *vp, VSTREAM *fp, int flags,
				             ssize_t bound)
{
    int     c;
    ssize_t base_len;

    if (bound <= 0)
	msg_panic("vstring_get_nonl_bound: invalid bound %ld", (long) bound);

    if ((flags & VSTRING_GET_FLAG_APPEND) == 0)
	VSTRING_RESET(vp);
    base_len = VSTRING_LEN(vp);
    while (bound-- > 0 && (c = VSTREAM_GETC(fp)) != VSTREAM_EOF && c != '\n')
	VSTRING_ADDCH(vp, c);
    VSTRING_TERMINATE(vp);
    return (c == '\n' ? c : VSTRING_GET_RESULT(vp, baselen));
}

/* vstring_get_flags_null_bound - read null-terminated string from file */

int     vstring_get_flags_null_bound(VSTRING *vp, VSTREAM *fp, int flags,
				             ssize_t bound)
{
    int     c;
    ssize_t base_len;

    if (bound <= 0)
	msg_panic("vstring_get_null_bound: invalid bound %ld", (long) bound);

    if ((flags & VSTRING_GET_FLAG_APPEND) == 0)
	VSTRING_RESET(vp);
    base_len = VSTRING_LEN(vp);
    while (bound-- > 0 && (c = VSTREAM_GETC(fp)) != VSTREAM_EOF && c != 0)
	VSTRING_ADDCH(vp, c);
    VSTRING_TERMINATE(vp);
    return (c == 0 ? c : VSTRING_GET_RESULT(vp, baselen));
}

#ifdef TEST

 /*
  * Proof-of-concept test program: copy the source to this module to stdout.
  */
#include <fcntl.h>

#define TEXT_VSTREAM    "vstring_vstream.c"

int     main(void)
{
    VSTRING *vp = vstring_alloc(1);
    VSTREAM *fp;

    if ((fp = vstream_fopen(TEXT_VSTREAM, O_RDONLY, 0)) == 0)
	msg_fatal("open %s: %m", TEXT_VSTREAM);
    while (vstring_fgets(vp, fp))
	vstream_fprintf(VSTREAM_OUT, "%s", vstring_str(vp));
    vstream_fclose(fp);
    vstream_fflush(VSTREAM_OUT);
    vstring_free(vp);
    return (0);
}

#endif