472
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1996 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
|
401
|
14 |
Stream subclass:#FilteringStream
|
477
|
15 |
instanceVariableNames:'inputStream outputStream filter readAhead'
|
401
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
474
|
18 |
category:'Streams-Misc'
|
401
|
19 |
!
|
|
20 |
|
472
|
21 |
!FilteringStream class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1996 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
|
|
36 |
!
|
|
37 |
|
|
38 |
documentation
|
|
39 |
"
|
|
40 |
A FilteringStream can be connected to some input
|
|
41 |
(from which elements are read via the ReadStream protocol),
|
|
42 |
and/or to some output (to which elements are written via
|
|
43 |
the WriteStream protocol.
|
477
|
44 |
|
|
45 |
The FilteringStream itself performs filtering/processing
|
|
46 |
on the elements as they arrive, optionally suppressing
|
|
47 |
elements.
|
|
48 |
|
|
49 |
A FilteringStream can be operated in pull-mode, by asking
|
|
50 |
it for the next element; it will then ask its inputStream for
|
|
51 |
and element, process it and return it.
|
472
|
52 |
|
477
|
53 |
Or, in pushMode, by having someone else writing elements via
|
|
54 |
nextPut:; it will then process the element, and send it to its
|
|
55 |
output stream.
|
|
56 |
|
|
57 |
Mixing modes does not make sense, since if pulled, data will not
|
|
58 |
be written to the outputStream (unless the puller does it).
|
|
59 |
|
|
60 |
The connected streams need not be real streams; anything which
|
|
61 |
responds to the basic Stream protocol can be connected
|
|
62 |
(a Transcript, a RandomNumber generator or even a Plug will do as well).
|
472
|
63 |
|
|
64 |
[instance variables:]
|
|
65 |
inputStream <Stream> the stream from which elements are read
|
|
66 |
outputStream <Stream> the stream to which elements are written
|
|
67 |
filter <Block> the filter block;
|
|
68 |
unbound <Boolean> if true, the stream is unbound.
|
|
69 |
|
|
70 |
[author:]
|
|
71 |
Claus Gittinger
|
|
72 |
|
|
73 |
[See also:]
|
|
74 |
ReadStream WriteStream
|
|
75 |
"
|
|
76 |
|
|
77 |
!
|
401
|
78 |
|
|
79 |
examples
|
|
80 |
"
|
474
|
81 |
pushing the contents of a stream onto another stream
|
|
82 |
(here, the Transcript) without a need to read everyting into a
|
|
83 |
buffer or to reinvent the read-loop:
|
479
|
84 |
(notice, a FilteringLineStream does this with less overhead,
|
|
85 |
due to the byte-wise reading done here)
|
474
|
86 |
[exBegin]
|
|
87 |
|in pusher|
|
|
88 |
|
|
89 |
in := 'Makefile' asFilename readStream.
|
|
90 |
pusher := FilteringStream readingFrom:in writingTo:Transcript.
|
|
91 |
pusher filterUpToEnd
|
|
92 |
[exEnd]
|
|
93 |
|
|
94 |
|
401
|
95 |
filter random numbers:
|
472
|
96 |
[exBegin]
|
401
|
97 |
|in filter|
|
|
98 |
|
|
99 |
in := Random new.
|
|
100 |
|
474
|
101 |
filter := FilteringLineStream readingFrom:in.
|
401
|
102 |
filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]].
|
|
103 |
|
|
104 |
20 timesRepeat:[
|
|
105 |
Transcript showCR:(filter next printString).
|
|
106 |
]
|
472
|
107 |
[exEnd]
|
401
|
108 |
|
|
109 |
filtering prime numbers:
|
472
|
110 |
[exBegin]
|
401
|
111 |
|num generator primeFilter addFilter|
|
|
112 |
|
|
113 |
num := 1.
|
|
114 |
generator := Plug new.
|
|
115 |
generator respondTo:#next
|
|
116 |
with:[num := num + 1. num].
|
|
117 |
generator respondTo:#atEnd
|
|
118 |
with:[false].
|
|
119 |
|
|
120 |
addFilter := [:prime | |newFilter|
|
|
121 |
newFilter := FilteringLineStream basicNew.
|
|
122 |
newFilter filter:[:num | (num \\ prime) == 0 ifTrue:[
|
|
123 |
nil
|
|
124 |
] ifFalse:[
|
|
125 |
num
|
|
126 |
]
|
|
127 |
].
|
|
128 |
newFilter inputStream:primeFilter.
|
|
129 |
primeFilter := newFilter
|
|
130 |
].
|
|
131 |
|
|
132 |
addFilter value:2.
|
|
133 |
primeFilter inputStream:generator.
|
|
134 |
|
472
|
135 |
10000 timesRepeat:[
|
401
|
136 |
|nextPrime|
|
|
137 |
|
|
138 |
nextPrime := primeFilter next.
|
|
139 |
addFilter value:nextPrime.
|
|
140 |
Transcript showCR:nextPrime.
|
|
141 |
]
|
472
|
142 |
[exEnd]
|
401
|
143 |
"
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
! !
|
|
148 |
|
474
|
149 |
!FilteringStream class methodsFor:'instance creation'!
|
|
150 |
|
|
151 |
new
|
|
152 |
"create and return a new filteringStream.
|
|
153 |
The resulting stream must be connected to some other stream,
|
|
154 |
before being used"
|
|
155 |
|
|
156 |
^ self basicNew initialize.
|
|
157 |
|
|
158 |
"Created: 11.1.1997 / 15:31:30 / cg"
|
|
159 |
"Modified: 11.1.1997 / 15:33:13 / cg"
|
|
160 |
!
|
|
161 |
|
477
|
162 |
on:something
|
|
163 |
"create and return a new filteringStream, which reads from
|
|
164 |
something (which must be convertable to a stream)"
|
|
165 |
|
|
166 |
^ self readingFrom:something readStream
|
|
167 |
|
|
168 |
"Created: 11.1.1997 / 19:19:34 / cg"
|
|
169 |
!
|
|
170 |
|
474
|
171 |
readingFrom:aReadStream
|
|
172 |
"create and return a new filteringStream, which reads from
|
|
173 |
another stream"
|
|
174 |
|
|
175 |
^ self basicNew initialize inputStream:aReadStream.
|
|
176 |
|
|
177 |
"Created: 11.1.1997 / 15:32:15 / cg"
|
|
178 |
!
|
|
179 |
|
|
180 |
readingFrom:aReadStream writingTo:aWriteStream
|
|
181 |
"create and return a new filteringStream, which reads from
|
|
182 |
aReadStream and writes to aWriteStream."
|
|
183 |
|
|
184 |
|newStream|
|
|
185 |
|
|
186 |
newStream := self basicNew initialize.
|
|
187 |
newStream inputStream:aReadStream.
|
|
188 |
newStream outputStream:aWriteStream.
|
|
189 |
^ newStream
|
|
190 |
|
|
191 |
"Created: 11.1.1997 / 15:32:28 / cg"
|
|
192 |
!
|
|
193 |
|
|
194 |
writingTo:aWriteStream
|
|
195 |
"create and return a new filteringStream, which writes to
|
|
196 |
another stream"
|
|
197 |
|
|
198 |
^ self basicNew initialize outputStream:aWriteStream.
|
|
199 |
|
|
200 |
"Created: 11.1.1997 / 15:32:36 / cg"
|
|
201 |
! !
|
|
202 |
|
401
|
203 |
!FilteringStream methodsFor:'access - pull-reading'!
|
|
204 |
|
|
205 |
filterUpToEnd
|
|
206 |
"pull input from inputStream up to the end,
|
474
|
207 |
push it filtered into the outputStream."
|
401
|
208 |
|
|
209 |
[inputStream atEnd] whileFalse:[
|
|
210 |
self nextPut:(inputStream next)
|
|
211 |
].
|
|
212 |
|
|
213 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
474
|
214 |
"Modified: 11.1.1997 / 16:08:35 / cg"
|
401
|
215 |
!
|
|
216 |
|
|
217 |
next
|
|
218 |
"pull input from inputStream and
|
|
219 |
push it filtered into the outputStream"
|
|
220 |
|
|
221 |
|input output|
|
|
222 |
|
477
|
223 |
"/ readAhead input has already been filtered
|
|
224 |
"/ (see #atEnd)
|
|
225 |
|
|
226 |
readAhead notNil ifTrue:[
|
|
227 |
input := readAhead.
|
|
228 |
readAhead := nil.
|
|
229 |
^ input
|
|
230 |
].
|
|
231 |
|
401
|
232 |
[inputStream atEnd] whileFalse:[
|
472
|
233 |
"/ get an element
|
401
|
234 |
input := inputStream next.
|
474
|
235 |
filter isNil ifTrue:[
|
|
236 |
^ input
|
|
237 |
].
|
|
238 |
|
472
|
239 |
"/ filter it - this may return nil, to eat it
|
401
|
240 |
output := filter value:input.
|
|
241 |
output notNil ifTrue:[
|
472
|
242 |
"/ good - output it
|
401
|
243 |
^ output.
|
|
244 |
].
|
|
245 |
].
|
|
246 |
^ nil
|
|
247 |
|
|
248 |
"Created: 2.7.1996 / 21:09:58 / cg"
|
477
|
249 |
"Modified: 11.1.1997 / 17:17:27 / cg"
|
401
|
250 |
! !
|
|
251 |
|
|
252 |
!FilteringStream methodsFor:'access - push-writing'!
|
|
253 |
|
|
254 |
nextPut:something
|
|
255 |
"push something through the filter"
|
|
256 |
|
|
257 |
|output|
|
|
258 |
|
472
|
259 |
"/ filter it
|
474
|
260 |
filter isNil ifTrue:[
|
|
261 |
outputStream nextPut:something
|
|
262 |
] ifFalse:[
|
|
263 |
output := filter value:something.
|
|
264 |
output notNil ifTrue:[
|
|
265 |
outputStream nextPut:output
|
|
266 |
]
|
401
|
267 |
]
|
|
268 |
|
474
|
269 |
"Modified: 11.1.1997 / 16:12:52 / cg"
|
401
|
270 |
! !
|
|
271 |
|
|
272 |
!FilteringStream methodsFor:'accessing'!
|
|
273 |
|
|
274 |
filter
|
|
275 |
"return the filter"
|
|
276 |
|
|
277 |
^ filter
|
|
278 |
|
|
279 |
"Modified: 2.7.1996 / 21:03:36 / cg"
|
|
280 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
281 |
!
|
|
282 |
|
|
283 |
filter:something
|
|
284 |
"set the filter"
|
|
285 |
|
|
286 |
filter := something.
|
|
287 |
|
|
288 |
"Modified: 2.7.1996 / 21:03:40 / cg"
|
|
289 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
290 |
!
|
|
291 |
|
|
292 |
inputStream
|
|
293 |
"return the inputStream"
|
|
294 |
|
|
295 |
^ inputStream
|
|
296 |
|
|
297 |
"Modified: 2.7.1996 / 21:03:43 / cg"
|
|
298 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
299 |
!
|
|
300 |
|
|
301 |
inputStream:something
|
|
302 |
"set the inputStream"
|
|
303 |
|
|
304 |
inputStream := something.
|
|
305 |
|
|
306 |
"Modified: 2.7.1996 / 21:03:46 / cg"
|
|
307 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
308 |
!
|
|
309 |
|
|
310 |
outputStream
|
|
311 |
"return the outputStream"
|
|
312 |
|
|
313 |
^ outputStream
|
|
314 |
|
|
315 |
"Modified: 2.7.1996 / 21:03:49 / cg"
|
|
316 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
317 |
!
|
|
318 |
|
|
319 |
outputStream:something
|
|
320 |
"set the outputStream"
|
|
321 |
|
|
322 |
outputStream := something.
|
|
323 |
|
|
324 |
"Modified: 2.7.1996 / 21:03:52 / cg"
|
|
325 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
474
|
326 |
! !
|
401
|
327 |
|
474
|
328 |
!FilteringStream methodsFor:'misc'!
|
401
|
329 |
|
474
|
330 |
close
|
|
331 |
"when I am closed, close my input - if any"
|
401
|
332 |
|
474
|
333 |
inputStream notNil ifTrue:[
|
|
334 |
inputStream close
|
|
335 |
]
|
401
|
336 |
|
474
|
337 |
"Created: 11.1.1997 / 15:27:17 / cg"
|
401
|
338 |
! !
|
|
339 |
|
|
340 |
!FilteringStream methodsFor:'queries'!
|
|
341 |
|
|
342 |
atEnd
|
472
|
343 |
"return true, if the receiver stream is at the end"
|
|
344 |
|
477
|
345 |
|nextElement|
|
|
346 |
|
|
347 |
readAhead notNil ifTrue:[^ false].
|
|
348 |
|
|
349 |
filter isNil ifTrue:[
|
|
350 |
"/ then, its easy
|
|
351 |
^ inputStream atEnd
|
|
352 |
].
|
401
|
353 |
|
477
|
354 |
"/ with a filter, things are more complicated,
|
|
355 |
"/ since we cannot tell, without asking the filter ...
|
|
356 |
|
|
357 |
|
|
358 |
[inputStream atEnd] whileFalse:[
|
|
359 |
nextElement := inputStream next.
|
|
360 |
readAhead := filter value:nextElement.
|
|
361 |
readAhead notNil ifTrue:[^ false].
|
|
362 |
].
|
|
363 |
|
|
364 |
^ true
|
|
365 |
|
|
366 |
"Modified: 11.1.1997 / 17:16:45 / cg"
|
474
|
367 |
!
|
|
368 |
|
|
369 |
contentsSpecies
|
|
370 |
"return the kind of collection I should return when asked
|
|
371 |
for multiple elements."
|
|
372 |
|
|
373 |
^ inputStream contentsSpecies
|
|
374 |
|
|
375 |
"Created: 11.1.1997 / 16:23:22 / cg"
|
401
|
376 |
! !
|
|
377 |
|
472
|
378 |
!FilteringStream class methodsFor:'documentation'!
|
401
|
379 |
|
|
380 |
version
|
479
|
381 |
^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.5 1997-01-11 18:55:14 cg Exp $'
|
401
|
382 |
! !
|