author | Claus Gittinger <cg@exept.de> |
Sat, 04 May 2019 12:32:23 +0200 | |
changeset 4940 | d5d7391bc875 |
parent 4199 | a3839426cbcf |
child 5160 | ba06e617dd3f |
permissions | -rw-r--r-- |
472 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
3 |
All Rights Reserved |
472 | 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 |
" |
|
2375 | 12 |
"{ Package: 'stx:libbasic2' }" |
889 | 13 |
|
3757 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
889 | 16 |
PeekableStream 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 |
2375 | 56 |
nextPut:; it will then process the element, and send it to its |
477 | 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 |
|
2375 | 66 |
Similar, but not quite the same as SelectingReadStream / CollectingReadStream. |
67 |
||
472 | 68 |
[instance variables:] |
2375 | 69 |
inputStream <Stream> the stream from which elements are read |
70 |
outputStream <Stream> the stream to which elements are written |
|
71 |
filter <Block> the filter block; |
|
72 |
unbound <Boolean> if true, the stream is unbound. |
|
472 | 73 |
|
74 |
[author:] |
|
2375 | 75 |
Claus Gittinger |
472 | 76 |
|
77 |
[See also:] |
|
2375 | 78 |
ReadStream WriteStream |
472 | 79 |
" |
80 |
! |
|
401 | 81 |
|
82 |
examples |
|
83 |
" |
|
474 | 84 |
pushing the contents of a stream onto another stream |
889 | 85 |
(here, the Transcript) without a need to read everything into a |
474 | 86 |
buffer or to reinvent the read-loop: |
479 | 87 |
(notice, a FilteringLineStream does this with less overhead, |
88 |
due to the byte-wise reading done here) |
|
889 | 89 |
[exBegin] |
474 | 90 |
|in pusher| |
91 |
||
2375 | 92 |
in := 'Make.proto' asFilename readStream. |
474 | 93 |
pusher := FilteringStream readingFrom:in writingTo:Transcript. |
94 |
pusher filterUpToEnd |
|
889 | 95 |
[exEnd] |
474 | 96 |
|
97 |
||
2375 | 98 |
filter random numbers in [0.5 .. 0.6]: |
889 | 99 |
[exBegin] |
401 | 100 |
|in filter| |
101 |
||
102 |
in := Random new. |
|
103 |
||
2375 | 104 |
filter := FilteringStream readingFrom:in. |
401 | 105 |
filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]]. |
106 |
||
107 |
20 timesRepeat:[ |
|
889 | 108 |
Transcript showCR:(filter next printString). |
401 | 109 |
] |
889 | 110 |
[exEnd] |
4078 | 111 |
compute the values times two |
112 |
[exBegin] |
|
113 |
|filter| |
|
114 |
||
115 |
filter := FilteringStream |
|
116 |
readingFrom:#(1 2 3 4 5 6 7 8 9 10) readStream |
|
117 |
writingTo:#() writeStream. |
|
118 |
filter filter:[:num | num * 2]. |
|
119 |
filter contents inspect. |
|
120 |
[exEnd] |
|
121 |
||
401 | 122 |
|
2375 | 123 |
filtering prime numbers (that's just a demo - not really useful, because we will end |
124 |
in a recursion error if many filters are stacked): |
|
889 | 125 |
[exBegin] |
401 | 126 |
|num generator primeFilter addFilter| |
127 |
||
128 |
num := 1. |
|
129 |
generator := Plug new. |
|
130 |
generator respondTo:#next |
|
889 | 131 |
with:[num := num + 1. num]. |
401 | 132 |
generator respondTo:#atEnd |
889 | 133 |
with:[false]. |
401 | 134 |
|
135 |
addFilter := [:prime | |newFilter| |
|
889 | 136 |
newFilter := FilteringStream basicNew. |
137 |
newFilter filter:[:num | (num \\ prime) == 0 ifTrue:[ |
|
138 |
nil |
|
139 |
] ifFalse:[ |
|
140 |
num |
|
141 |
] |
|
142 |
]. |
|
143 |
newFilter inputStream:primeFilter. |
|
144 |
primeFilter := newFilter |
|
401 | 145 |
]. |
146 |
||
147 |
addFilter value:2. |
|
148 |
primeFilter inputStream:generator. |
|
149 |
||
2375 | 150 |
1000 timesRepeat:[ |
889 | 151 |
|nextPrime| |
401 | 152 |
|
889 | 153 |
nextPrime := primeFilter next. |
154 |
addFilter value:nextPrime. |
|
155 |
Transcript showCR:nextPrime. |
|
401 | 156 |
] |
889 | 157 |
[exEnd] |
401 | 158 |
" |
159 |
! ! |
|
160 |
||
474 | 161 |
!FilteringStream class methodsFor:'instance creation'! |
162 |
||
163 |
new |
|
164 |
"create and return a new filteringStream. |
|
165 |
The resulting stream must be connected to some other stream, |
|
166 |
before being used" |
|
167 |
||
168 |
^ self basicNew initialize. |
|
169 |
||
170 |
"Created: 11.1.1997 / 15:31:30 / cg" |
|
171 |
"Modified: 11.1.1997 / 15:33:13 / cg" |
|
172 |
! |
|
173 |
||
477 | 174 |
on:something |
175 |
"create and return a new filteringStream, which reads from |
|
176 |
something (which must be convertable to a stream)" |
|
177 |
||
178 |
^ self readingFrom:something readStream |
|
179 |
||
180 |
"Created: 11.1.1997 / 19:19:34 / cg" |
|
181 |
! |
|
182 |
||
474 | 183 |
readingFrom:aReadStream |
184 |
"create and return a new filteringStream, which reads from |
|
185 |
another stream" |
|
186 |
||
1291 | 187 |
^ (self basicNew inputStream:aReadStream) initialize. |
474 | 188 |
|
189 |
"Created: 11.1.1997 / 15:32:15 / cg" |
|
190 |
! |
|
191 |
||
192 |
readingFrom:aReadStream writingTo:aWriteStream |
|
193 |
"create and return a new filteringStream, which reads from |
|
194 |
aReadStream and writes to aWriteStream." |
|
195 |
||
196 |
|newStream| |
|
197 |
||
1291 | 198 |
newStream := self basicNew. |
474 | 199 |
newStream inputStream:aReadStream. |
200 |
newStream outputStream:aWriteStream. |
|
1291 | 201 |
newStream initialize. |
474 | 202 |
^ newStream |
203 |
||
204 |
"Created: 11.1.1997 / 15:32:28 / cg" |
|
205 |
! |
|
206 |
||
207 |
writingTo:aWriteStream |
|
208 |
"create and return a new filteringStream, which writes to |
|
209 |
another stream" |
|
210 |
||
1291 | 211 |
^ (self basicNew outputStream:aWriteStream) initialize. |
474 | 212 |
|
213 |
"Created: 11.1.1997 / 15:32:36 / cg" |
|
214 |
! ! |
|
215 |
||
401 | 216 |
!FilteringStream methodsFor:'access - pull-reading'! |
217 |
||
1291 | 218 |
contents |
4078 | 219 |
self filterUpToEnd. |
220 |
^ outputStream contents |
|
1291 | 221 |
! |
222 |
||
401 | 223 |
filterUpToEnd |
224 |
"pull input from inputStream up to the end, |
|
474 | 225 |
push it filtered into the outputStream." |
401 | 226 |
|
227 |
[inputStream atEnd] whileFalse:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
228 |
self nextPut:(inputStream next) |
401 | 229 |
]. |
230 |
||
231 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
474 | 232 |
"Modified: 11.1.1997 / 16:08:35 / cg" |
401 | 233 |
! |
234 |
||
235 |
next |
|
236 |
"pull input from inputStream and |
|
237 |
push it filtered into the outputStream" |
|
238 |
||
239 |
|input output| |
|
240 |
||
477 | 241 |
"/ readAhead input has already been filtered |
242 |
"/ (see #atEnd) |
|
243 |
||
244 |
readAhead notNil ifTrue:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
245 |
input := readAhead. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
246 |
readAhead := nil. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
247 |
^ input |
477 | 248 |
]. |
249 |
||
401 | 250 |
[inputStream atEnd] whileFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
251 |
"/ get an element |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
252 |
input := inputStream next. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
253 |
filter isNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
254 |
^ input |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
255 |
]. |
474 | 256 |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
257 |
"/ filter it - this may return nil, to eat it |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
258 |
output := filter value:input. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
259 |
output notNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
260 |
"/ good - output it |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
261 |
^ output. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
262 |
]. |
401 | 263 |
]. |
264 |
^ nil |
|
265 |
||
266 |
"Created: 2.7.1996 / 21:09:58 / cg" |
|
477 | 267 |
"Modified: 11.1.1997 / 17:17:27 / cg" |
889 | 268 |
! |
269 |
||
270 |
peek |
|
271 |
"peek ahead for the next character" |
|
272 |
||
273 |
|input output| |
|
274 |
||
275 |
readAhead notNil ifTrue:[ |
|
2375 | 276 |
^ readAhead |
889 | 277 |
]. |
278 |
||
279 |
[inputStream atEnd] whileFalse:[ |
|
2375 | 280 |
"/ get an element |
281 |
input := inputStream peek. |
|
282 |
filter isNil ifTrue:[ |
|
283 |
^ input |
|
284 |
]. |
|
889 | 285 |
|
2375 | 286 |
"/ filter it - this may return nil, to eat it |
287 |
inputStream next. |
|
288 |
output := filter value:input. |
|
289 |
output notNil ifTrue:[ |
|
290 |
"/ good - output it |
|
291 |
readAhead := output. |
|
292 |
^ output. |
|
293 |
]. |
|
889 | 294 |
]. |
1483 | 295 |
^ self pastEndRead |
889 | 296 |
|
297 |
! |
|
298 |
||
299 |
peekOrNil |
|
300 |
"peek ahead for the next character, or return nil" |
|
301 |
||
302 |
|input output| |
|
303 |
||
304 |
readAhead notNil ifTrue:[ |
|
2375 | 305 |
^ readAhead |
889 | 306 |
]. |
307 |
||
308 |
[inputStream atEnd] whileFalse:[ |
|
2375 | 309 |
"/ get an element |
310 |
input := inputStream peek. |
|
311 |
filter isNil ifTrue:[ |
|
312 |
^ input |
|
313 |
]. |
|
889 | 314 |
|
2375 | 315 |
"/ filter it - this may return nil, to eat it |
316 |
inputStream next. |
|
317 |
output := filter value:input. |
|
318 |
output notNil ifTrue:[ |
|
319 |
"/ good - output it |
|
320 |
readAhead := output. |
|
321 |
^ output. |
|
322 |
]. |
|
889 | 323 |
]. |
324 |
^ nil |
|
401 | 325 |
! ! |
326 |
||
327 |
!FilteringStream methodsFor:'access - push-writing'! |
|
328 |
||
329 |
nextPut:something |
|
330 |
"push something through the filter" |
|
331 |
||
332 |
|output| |
|
333 |
||
472 | 334 |
"/ filter it |
474 | 335 |
filter isNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
336 |
outputStream nextPut:something |
474 | 337 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
338 |
output := filter value:something. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
339 |
output notNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
340 |
outputStream nextPut:output |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
341 |
] |
401 | 342 |
] |
343 |
||
474 | 344 |
"Modified: 11.1.1997 / 16:12:52 / cg" |
401 | 345 |
! ! |
346 |
||
347 |
!FilteringStream methodsFor:'accessing'! |
|
348 |
||
349 |
filter |
|
350 |
"return the filter" |
|
351 |
||
352 |
^ filter |
|
353 |
||
354 |
"Modified: 2.7.1996 / 21:03:36 / cg" |
|
355 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
356 |
! |
|
357 |
||
358 |
filter:something |
|
359 |
"set the filter" |
|
360 |
||
361 |
filter := something. |
|
362 |
||
363 |
"Modified: 2.7.1996 / 21:03:40 / cg" |
|
364 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
365 |
! |
|
366 |
||
367 |
inputStream |
|
368 |
"return the inputStream" |
|
369 |
||
3100 | 370 |
^ inputStream inputStream |
401 | 371 |
|
372 |
"Modified: 2.7.1996 / 21:03:43 / cg" |
|
373 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
374 |
! |
|
375 |
||
376 |
inputStream:something |
|
377 |
"set the inputStream" |
|
378 |
||
379 |
inputStream := something. |
|
380 |
||
381 |
"Modified: 2.7.1996 / 21:03:46 / cg" |
|
382 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
383 |
! |
|
384 |
||
385 |
outputStream |
|
386 |
"return the outputStream" |
|
387 |
||
388 |
^ outputStream |
|
389 |
||
390 |
"Modified: 2.7.1996 / 21:03:49 / cg" |
|
391 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
392 |
! |
|
393 |
||
394 |
outputStream:something |
|
395 |
"set the outputStream" |
|
396 |
||
397 |
outputStream := something. |
|
398 |
||
399 |
"Modified: 2.7.1996 / 21:03:52 / cg" |
|
400 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
474 | 401 |
! ! |
401 | 402 |
|
474 | 403 |
!FilteringStream methodsFor:'misc'! |
401 | 404 |
|
4199 | 405 |
clearEOF |
406 |
^ inputStream clearEOF |
|
407 |
! |
|
408 |
||
474 | 409 |
close |
410 |
"when I am closed, close my input - if any" |
|
401 | 411 |
|
474 | 412 |
inputStream notNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
413 |
inputStream close |
474 | 414 |
] |
401 | 415 |
|
474 | 416 |
"Created: 11.1.1997 / 15:27:17 / cg" |
401 | 417 |
! ! |
418 |
||
419 |
!FilteringStream methodsFor:'queries'! |
|
420 |
||
421 |
atEnd |
|
472 | 422 |
"return true, if the receiver stream is at the end" |
423 |
||
477 | 424 |
|nextElement| |
425 |
||
426 |
readAhead notNil ifTrue:[^ false]. |
|
427 |
||
428 |
filter isNil ifTrue:[ |
|
3757 | 429 |
"/ then, its easy |
430 |
^ inputStream atEnd |
|
477 | 431 |
]. |
401 | 432 |
|
2375 | 433 |
"/ with a filter, things are more complicated, |
3757 | 434 |
"/ since we cannot tell, without asking the filter... |
477 | 435 |
|
436 |
[inputStream atEnd] whileFalse:[ |
|
3757 | 437 |
nextElement := inputStream next. |
438 |
readAhead := filter value:nextElement. |
|
439 |
readAhead notNil ifTrue:[^ false]. |
|
477 | 440 |
]. |
441 |
||
442 |
^ true |
|
443 |
||
444 |
"Modified: 11.1.1997 / 17:16:45 / cg" |
|
474 | 445 |
! |
446 |
||
447 |
contentsSpecies |
|
448 |
"return the kind of collection I should return when asked |
|
449 |
for multiple elements." |
|
450 |
||
451 |
^ inputStream contentsSpecies |
|
452 |
||
453 |
"Created: 11.1.1997 / 16:23:22 / cg" |
|
955 | 454 |
! |
455 |
||
2786 | 456 |
isEmpty |
4078 | 457 |
^ inputStream isEmptyOrNil |
458 |
and:[outputStream isEmptyOrNil and:[readAhead isNil]]. |
|
2786 | 459 |
|
460 |
"Created: / 06-08-2012 / 08:09:01 / cg" |
|
461 |
! |
|
462 |
||
3101 | 463 |
isEncodedStream |
464 |
^ inputStream isEncodedStream |
|
465 |
! |
|
466 |
||
3068 | 467 |
isReadable |
468 |
^ inputStream notNil and:[inputStream isReadable] |
|
469 |
! |
|
470 |
||
471 |
isWritable |
|
3779 | 472 |
"return true, if writing is supported by the receiver." |
473 |
||
3068 | 474 |
^ outputStream notNil and:[outputStream isWritable] |
475 |
! |
|
476 |
||
955 | 477 |
position |
478 |
"return the receiver streams position" |
|
479 |
||
480 |
|rawPosition| |
|
481 |
||
482 |
rawPosition := inputStream position. |
|
483 |
readAhead notNil ifTrue:[ |
|
2375 | 484 |
rawPosition := rawPosition - 1 |
955 | 485 |
]. |
486 |
^ rawPosition |
|
1239 | 487 |
! |
488 |
||
489 |
position0Based |
|
3082 | 490 |
<resource: #obsolete> |
1239 | 491 |
"return the receiver streams position" |
492 |
||
3082 | 493 |
^ self position |
1239 | 494 |
! |
495 |
||
496 |
position1Based |
|
3082 | 497 |
<resource: #obsolete> |
1239 | 498 |
"return the receiver streams position" |
499 |
||
3082 | 500 |
^ self position + 1 |
2695 | 501 |
! |
502 |
||
503 |
readStream |
|
504 |
^ self |
|
505 |
||
506 |
"Created: / 06-12-2011 / 01:05:26 / cg" |
|
2775 | 507 |
! |
508 |
||
509 |
size |
|
4078 | 510 |
"not always correct, but probably better than 0. |
511 |
Better use #isEmpty." |
|
3067 | 512 |
|
513 |
"/ is that better? |
|
514 |
"/ self error:'size of input is unknown (due to filtering)' |
|
4078 | 515 |
^ (inputStream ? outputStream) size. |
2775 | 516 |
|
517 |
"Created: / 05-08-2012 / 18:39:00 / cg" |
|
401 | 518 |
! ! |
519 |
||
472 | 520 |
!FilteringStream class methodsFor:'documentation'! |
401 | 521 |
|
522 |
version |
|
3757 | 523 |
^ '$Header$' |
2375 | 524 |
! |
525 |
||
526 |
version_CVS |
|
3757 | 527 |
^ '$Header$' |
401 | 528 |
! ! |
3067 | 529 |