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