-
Notifications
You must be signed in to change notification settings - Fork 2
/
basic-wrapper-stream.dylan
354 lines (304 loc) · 10.6 KB
/
basic-wrapper-stream.dylan
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
module: basic-wrapper-stream
author: Dustin Voss
/**
Class: <basic-wrapper-stream>
-----------------------------
Synopsis: A <wrapper-stream> with implementations for several methods. Use this
between a wrapper stream and a non-wrapper stream affected by bug #7385.
Note: This class is only necessary because Common Dylan I/O convenience
methods do not call primitive methods on outer-stream. See bug #7385.
Several I/O methods can be written in terms of a simpler subset of methods.
This class provides implementations for the following methods that act as
described by the "Functional Developer System and I/O Reference": [bib]
- read
- read-into!
- write
- read-to
- read-through
- read-to-end
- skip-through
- read-line
- read-line-into!
- read-text
- read-text-into!
- write-line
- new-line
Those methods use the following "primitive" methods, which should be
implemented on subclasses, or left delegated to the inner stream:
- read-element
- peek
- write-element
- stream-at-end?
Other methods such as 'unread-element' or 'stream-position' are not used by
methods on this class, but should probably be implemented on subclasses
anyway.
Additionally, the following method is implemented on this class to keep it
from being delegated to the inner stream:
- close
This implementation of 'close' does nothing. Generally, one expects that closing
a wrapper stream leaves the inner stream open.
*/
define open class <basic-wrapper-stream> (<wrapper-stream>)
end class;
define method close (wrapper :: <basic-wrapper-stream>, #key, #all-keys) => ()
end method;
define method read
(wrapper :: <basic-wrapper-stream>, n :: <integer>,
#rest keys, #key on-end-of-stream = unsupplied())
=> (elements-or-eof :: <object>)
let results = make(sequence-type-for-inner-stream(wrapper), size: n);
block ()
let read-count = read-into!(wrapper.outer-stream, n, results);
if (read-count < n)
copy-sequence(results, end: read-count)
else
results
end if;
exception (type-union(<end-of-stream-error>, <incomplete-read-error>),
test: always(on-end-of-stream.supplied?))
on-end-of-stream;
end block;
end method;
define method read-into!
(wrapper :: <basic-wrapper-stream>, n :: <integer>, coll :: <mutable-sequence>,
#rest keys, #key start :: <integer> = 0, on-end-of-stream = unsupplied())
=> (count-or-eof :: <object>)
let total-read = 0;
block ()
// Would rather use replace-subsequence!, but it doesn't guarantee that
// coll would be modified.
for (i from start below min(start + n, coll.size))
coll[i] := read-element(wrapper);
total-read := total-read + 1;
end for;
exception (eos :: <end-of-stream-error>)
case
on-end-of-stream.supplied? =>
total-read := on-end-of-stream;
total-read > 0 =>
error(make(<incomplete-read-error>, stream: wrapper, count: n,
sequence: copy-sequence(coll, start: start,
end: start + total-read)));
otherwise =>
error(eos);
end case;
end block;
total-read
end method;
define method write
(wrapper :: <basic-wrapper-stream>, elements :: <sequence>,
#key start: start-pos :: <integer> = 0,
end: end-pos :: <integer> = elements.size)
=> ()
for (i from start-pos below end-pos)
write-element(wrapper, elements[i])
end for;
end method;
define method read-to
(wrapper :: <basic-wrapper-stream>, to-elem :: <object>,
#key on-end-of-stream = unsupplied(), test = \==)
=> (elements-or-eof :: <object>, found? :: <boolean>)
// We use read-through instead of coding read-to behavior directly because
// this function is exactly like read-through but drops the terminator, and
// I didn't want to screw around with EOS logic.
let (elements, found?) = read-through(wrapper.outer-stream, to-elem,
test: test, on-end-of-stream: #f);
case
elements =>
let elements = copy-sequence(elements,
end: if (found?) elements.size - 1 else elements.size end if);
values(elements, found?);
on-end-of-stream.supplied? =>
values(on-end-of-stream, #f);
otherwise =>
eos-error(wrapper);
end case;
end method;
define method read-through
(wrapper :: <basic-wrapper-stream>, to-elem :: <object>,
#key on-end-of-stream = unsupplied(), test = \==)
=> (elements-or-eof :: <object>, found? :: <boolean>)
let elements = make(<stretchy-vector>);
let elems-type = wrapper.sequence-type-for-inner-stream;
block ()
let found? = #f;
until (found?)
let elem = read-element(wrapper);
add!(elements, elem);
found? := test(elem, to-elem);
end until;
values(as(elems-type, elements), #t)
exception (eos :: <end-of-stream-error>)
case
~elements.empty? =>
values(as(elems-type, elements), #f);
on-end-of-stream.supplied? =>
values(on-end-of-stream, #f);
otherwise =>
error(eos);
end case;
end block;
end method;
define method read-to-end (wrapper :: <basic-wrapper-stream>)
=> (elements :: <sequence>)
let elements = make(<stretchy-vector>);
while (~wrapper.stream-at-end?)
add!(elements, read-element(wrapper))
end while;
as(wrapper.sequence-type-for-inner-stream, elements)
end method;
/// 'test' is called with a stream element as the first argument and 'to-elem'
/// as the second argument.
define method skip-through
(wrapper :: <basic-wrapper-stream>, to-elem :: <object>,
#key test = \==)
=> (found? :: <boolean>)
let found? = #f;
while (~stream-at-end?(wrapper) & ~found?)
found? := test(read-element(wrapper), to-elem)
end;
found?
end method;
define method read-line
(wrapper :: <basic-wrapper-stream>, #rest keys,
#key on-end-of-stream = unsupplied())
=> (string-or-eof :: <object>, newline? :: <boolean>)
// This method is a lot like read-to. We don't use read-to directly, because
// we need to know whether the terminator is '\r' or '\n' so we can read the
// next '\n' if necessary.
// Read to line ending.
let (elements, found?) = read-through(wrapper.outer-stream, "\r\n",
test: member?, on-end-of-stream: #f);
case
elements =>
// Remove LF of CRLF.
when (found? & elements.last == '\r' &
peek(wrapper, on-end-of-stream: #f) == '\n')
read-element(wrapper);
end when;
// Make into <string>.
let string-type = wrapper.string-type-for-inner-stream;
let elements = copy-sequence(elements,
end: if (found?) elements.size - 1 else elements.size end if);
values(as(string-type, elements), found?);
on-end-of-stream.supplied? =>
values(on-end-of-stream, #f);
otherwise =>
eos-error(wrapper);
end case;
end method;
define method read-line-into!
(wrapper :: <basic-wrapper-stream>, string :: <string>,
#key start :: <integer> = 0, grow? :: <boolean> = #f,
on-end-of-stream = unsupplied())
=> (string-or-eof :: <object>, newline? :: <boolean>)
let (line, found?) = read-line(wrapper.outer-stream, on-end-of-stream: #f);
case
line =>
// Grow dest string if necessary.
let size-needed = start + line.size;
if (string.size < size-needed)
if (grow?)
let new-string = make(string.object-class, size: size-needed);
string := replace-subsequence!(new-string, string, end: string.size);
else
error("String not large enough for input");
end if;
end if;
// Copy into dest string. Would rather use replace-subsequence!, but
// that doesn't guarantee that string itself is altered.
for (i from start, c in line)
string[i] := c;
end for;
values(string, found?);
on-end-of-stream.supplied? =>
values(on-end-of-stream, #f);
otherwise =>
eos-error(wrapper);
end case;
end method;
define method read-text
(wrapper :: <basic-wrapper-stream>, n :: <integer>,
#rest keys, #key on-end-of-stream = unsupplied())
=> (elements-or-eof :: <object>)
let string-type = wrapper.string-type-for-inner-stream;
let results = make(string-type, size: n);
let read-count = read-text-into!(wrapper.outer-stream, n, results,
on-end-of-stream: #f);
case
read-count =>
if (read-count < n)
copy-sequence(results, end: read-count)
else
results
end if;
on-end-of-stream.supplied? =>
on-end-of-stream;
otherwise =>
eos-error(wrapper);
end case;
end method;
define method read-text-into!
(wrapper :: <basic-wrapper-stream>, n :: <integer>, string :: <string>,
#rest keys, #key start :: <integer> = 0, on-end-of-stream = unsupplied())
=> (count-or-eof :: <object>)
let total-read = 0;
block ()
for (i from start below min(start + n, string.size))
let elem = read-element(wrapper);
if (elem == '\r')
elem := '\n';
if (peek(wrapper, on-end-of-stream: #f) == '\n')
read-element(wrapper);
end if;
end if;
string[i] := elem;
total-read := total-read + 1;
end for;
total-read;
exception (eos :: <end-of-stream-error>)
case
total-read > 0 =>
total-read;
on-end-of-stream.supplied? =>
on-end-of-stream;
otherwise =>
error(eos);
end case;
end block;
end method;
define method write-line
(wrapper :: <basic-wrapper-stream>, elements :: <string>,
#key start: start-pos = 0, end: end-pos = elements.size)
=> ()
write(wrapper.outer-stream, elements, start: start-pos, end: end-pos);
new-line(wrapper.outer-stream)
end method;
define method new-line (wrapper :: <basic-wrapper-stream>) => ()
write-element(wrapper.outer-stream, '\n');
end method;
/// Synopsis: Returns sequence type appropriate for inner stream's element type.
define method sequence-type-for-inner-stream
(wrapper :: <basic-wrapper-stream>)
=> (sequence-type :: <type>)
select (wrapper.inner-stream.stream-element-type by subtype?)
<byte-character> => <byte-string>;
<character> => <string>;
otherwise => <vector>;
end select
end method;
/// Synopsis: Returns string type appropriate for inner stream's element type.
/// Conditions: Signals error if element type isn't allowed in a string.
define method string-type-for-inner-stream
(wrapper :: <basic-wrapper-stream>)
=> (sequence-type :: <type>)
select (wrapper.inner-stream.stream-element-type by subtype?)
<byte-character> => <byte-string>;
<character> => <string>;
otherwise =>
error("Inner stream element type is not subtype of <character>.");
end select
end method;
define inline function eos-error (wrapper :: <basic-wrapper-stream>) => ()
error(make(<end-of-stream-error>, stream: wrapper))
end function;