author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 17:01:18 +0200 | |
changeset 3918 | c565bdb0c8c4 |
parent 3779 | b202ba593f41 |
child 4078 | b7a4464057c6 |
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] |
401 | 111 |
|
2375 | 112 |
filtering prime numbers (that's just a demo - not really useful, because we will end |
113 |
in a recursion error if many filters are stacked): |
|
889 | 114 |
[exBegin] |
401 | 115 |
|num generator primeFilter addFilter| |
116 |
||
117 |
num := 1. |
|
118 |
generator := Plug new. |
|
119 |
generator respondTo:#next |
|
889 | 120 |
with:[num := num + 1. num]. |
401 | 121 |
generator respondTo:#atEnd |
889 | 122 |
with:[false]. |
401 | 123 |
|
124 |
addFilter := [:prime | |newFilter| |
|
889 | 125 |
newFilter := FilteringStream basicNew. |
126 |
newFilter filter:[:num | (num \\ prime) == 0 ifTrue:[ |
|
127 |
nil |
|
128 |
] ifFalse:[ |
|
129 |
num |
|
130 |
] |
|
131 |
]. |
|
132 |
newFilter inputStream:primeFilter. |
|
133 |
primeFilter := newFilter |
|
401 | 134 |
]. |
135 |
||
136 |
addFilter value:2. |
|
137 |
primeFilter inputStream:generator. |
|
138 |
||
2375 | 139 |
1000 timesRepeat:[ |
889 | 140 |
|nextPrime| |
401 | 141 |
|
889 | 142 |
nextPrime := primeFilter next. |
143 |
addFilter value:nextPrime. |
|
144 |
Transcript showCR:nextPrime. |
|
401 | 145 |
] |
889 | 146 |
[exEnd] |
401 | 147 |
" |
148 |
! ! |
|
149 |
||
474 | 150 |
!FilteringStream class methodsFor:'instance creation'! |
151 |
||
152 |
new |
|
153 |
"create and return a new filteringStream. |
|
154 |
The resulting stream must be connected to some other stream, |
|
155 |
before being used" |
|
156 |
||
157 |
^ self basicNew initialize. |
|
158 |
||
159 |
"Created: 11.1.1997 / 15:31:30 / cg" |
|
160 |
"Modified: 11.1.1997 / 15:33:13 / cg" |
|
161 |
! |
|
162 |
||
477 | 163 |
on:something |
164 |
"create and return a new filteringStream, which reads from |
|
165 |
something (which must be convertable to a stream)" |
|
166 |
||
167 |
^ self readingFrom:something readStream |
|
168 |
||
169 |
"Created: 11.1.1997 / 19:19:34 / cg" |
|
170 |
! |
|
171 |
||
474 | 172 |
readingFrom:aReadStream |
173 |
"create and return a new filteringStream, which reads from |
|
174 |
another stream" |
|
175 |
||
1291 | 176 |
^ (self basicNew inputStream:aReadStream) initialize. |
474 | 177 |
|
178 |
"Created: 11.1.1997 / 15:32:15 / cg" |
|
179 |
! |
|
180 |
||
181 |
readingFrom:aReadStream writingTo:aWriteStream |
|
182 |
"create and return a new filteringStream, which reads from |
|
183 |
aReadStream and writes to aWriteStream." |
|
184 |
||
185 |
|newStream| |
|
186 |
||
1291 | 187 |
newStream := self basicNew. |
474 | 188 |
newStream inputStream:aReadStream. |
189 |
newStream outputStream:aWriteStream. |
|
1291 | 190 |
newStream initialize. |
474 | 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 |
||
1291 | 200 |
^ (self basicNew outputStream:aWriteStream) initialize. |
474 | 201 |
|
202 |
"Created: 11.1.1997 / 15:32:36 / cg" |
|
203 |
! ! |
|
204 |
||
401 | 205 |
!FilteringStream methodsFor:'access - pull-reading'! |
206 |
||
1291 | 207 |
contents |
208 |
^ inputStream contents |
|
209 |
! |
|
210 |
||
401 | 211 |
filterUpToEnd |
212 |
"pull input from inputStream up to the end, |
|
474 | 213 |
push it filtered into the outputStream." |
401 | 214 |
|
215 |
[inputStream atEnd] whileFalse:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
216 |
self nextPut:(inputStream next) |
401 | 217 |
]. |
218 |
||
219 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
474 | 220 |
"Modified: 11.1.1997 / 16:08:35 / cg" |
401 | 221 |
! |
222 |
||
223 |
next |
|
224 |
"pull input from inputStream and |
|
225 |
push it filtered into the outputStream" |
|
226 |
||
227 |
|input output| |
|
228 |
||
477 | 229 |
"/ readAhead input has already been filtered |
230 |
"/ (see #atEnd) |
|
231 |
||
232 |
readAhead notNil ifTrue:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
233 |
input := readAhead. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
234 |
readAhead := nil. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
235 |
^ input |
477 | 236 |
]. |
237 |
||
401 | 238 |
[inputStream atEnd] whileFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
239 |
"/ get an element |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
240 |
input := inputStream next. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
241 |
filter isNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
242 |
^ input |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
243 |
]. |
474 | 244 |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
245 |
"/ filter it - this may return nil, to eat it |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
246 |
output := filter value:input. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
247 |
output notNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
248 |
"/ good - output it |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
249 |
^ output. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
250 |
]. |
401 | 251 |
]. |
252 |
^ nil |
|
253 |
||
254 |
"Created: 2.7.1996 / 21:09:58 / cg" |
|
477 | 255 |
"Modified: 11.1.1997 / 17:17:27 / cg" |
889 | 256 |
! |
257 |
||
258 |
peek |
|
259 |
"peek ahead for the next character" |
|
260 |
||
261 |
|input output| |
|
262 |
||
263 |
readAhead notNil ifTrue:[ |
|
2375 | 264 |
^ readAhead |
889 | 265 |
]. |
266 |
||
267 |
[inputStream atEnd] whileFalse:[ |
|
2375 | 268 |
"/ get an element |
269 |
input := inputStream peek. |
|
270 |
filter isNil ifTrue:[ |
|
271 |
^ input |
|
272 |
]. |
|
889 | 273 |
|
2375 | 274 |
"/ filter it - this may return nil, to eat it |
275 |
inputStream next. |
|
276 |
output := filter value:input. |
|
277 |
output notNil ifTrue:[ |
|
278 |
"/ good - output it |
|
279 |
readAhead := output. |
|
280 |
^ output. |
|
281 |
]. |
|
889 | 282 |
]. |
1483 | 283 |
^ self pastEndRead |
889 | 284 |
|
285 |
! |
|
286 |
||
287 |
peekOrNil |
|
288 |
"peek ahead for the next character, or return nil" |
|
289 |
||
290 |
|input output| |
|
291 |
||
292 |
readAhead notNil ifTrue:[ |
|
2375 | 293 |
^ readAhead |
889 | 294 |
]. |
295 |
||
296 |
[inputStream atEnd] whileFalse:[ |
|
2375 | 297 |
"/ get an element |
298 |
input := inputStream peek. |
|
299 |
filter isNil ifTrue:[ |
|
300 |
^ input |
|
301 |
]. |
|
889 | 302 |
|
2375 | 303 |
"/ filter it - this may return nil, to eat it |
304 |
inputStream next. |
|
305 |
output := filter value:input. |
|
306 |
output notNil ifTrue:[ |
|
307 |
"/ good - output it |
|
308 |
readAhead := output. |
|
309 |
^ output. |
|
310 |
]. |
|
889 | 311 |
]. |
312 |
^ nil |
|
401 | 313 |
! ! |
314 |
||
315 |
!FilteringStream methodsFor:'access - push-writing'! |
|
316 |
||
317 |
nextPut:something |
|
318 |
"push something through the filter" |
|
319 |
||
320 |
|output| |
|
321 |
||
472 | 322 |
"/ filter it |
474 | 323 |
filter isNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
324 |
outputStream nextPut:something |
474 | 325 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
326 |
output := filter value:something. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
327 |
output notNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
328 |
outputStream nextPut:output |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
329 |
] |
401 | 330 |
] |
331 |
||
474 | 332 |
"Modified: 11.1.1997 / 16:12:52 / cg" |
401 | 333 |
! ! |
334 |
||
335 |
!FilteringStream methodsFor:'accessing'! |
|
336 |
||
337 |
filter |
|
338 |
"return the filter" |
|
339 |
||
340 |
^ filter |
|
341 |
||
342 |
"Modified: 2.7.1996 / 21:03:36 / cg" |
|
343 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
344 |
! |
|
345 |
||
346 |
filter:something |
|
347 |
"set the filter" |
|
348 |
||
349 |
filter := something. |
|
350 |
||
351 |
"Modified: 2.7.1996 / 21:03:40 / cg" |
|
352 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
353 |
! |
|
354 |
||
355 |
inputStream |
|
356 |
"return the inputStream" |
|
357 |
||
3100 | 358 |
^ inputStream inputStream |
401 | 359 |
|
360 |
"Modified: 2.7.1996 / 21:03:43 / cg" |
|
361 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
362 |
! |
|
363 |
||
364 |
inputStream:something |
|
365 |
"set the inputStream" |
|
366 |
||
367 |
inputStream := something. |
|
368 |
||
369 |
"Modified: 2.7.1996 / 21:03:46 / cg" |
|
370 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
371 |
! |
|
372 |
||
373 |
outputStream |
|
374 |
"return the outputStream" |
|
375 |
||
376 |
^ outputStream |
|
377 |
||
378 |
"Modified: 2.7.1996 / 21:03:49 / cg" |
|
379 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
380 |
! |
|
381 |
||
382 |
outputStream:something |
|
383 |
"set the outputStream" |
|
384 |
||
385 |
outputStream := something. |
|
386 |
||
387 |
"Modified: 2.7.1996 / 21:03:52 / cg" |
|
388 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
474 | 389 |
! ! |
401 | 390 |
|
474 | 391 |
!FilteringStream methodsFor:'misc'! |
401 | 392 |
|
474 | 393 |
close |
394 |
"when I am closed, close my input - if any" |
|
401 | 395 |
|
474 | 396 |
inputStream notNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
397 |
inputStream close |
474 | 398 |
] |
401 | 399 |
|
474 | 400 |
"Created: 11.1.1997 / 15:27:17 / cg" |
401 | 401 |
! ! |
402 |
||
403 |
!FilteringStream methodsFor:'queries'! |
|
404 |
||
405 |
atEnd |
|
472 | 406 |
"return true, if the receiver stream is at the end" |
407 |
||
477 | 408 |
|nextElement| |
409 |
||
410 |
readAhead notNil ifTrue:[^ false]. |
|
411 |
||
412 |
filter isNil ifTrue:[ |
|
3757 | 413 |
"/ then, its easy |
414 |
^ inputStream atEnd |
|
477 | 415 |
]. |
401 | 416 |
|
2375 | 417 |
"/ with a filter, things are more complicated, |
3757 | 418 |
"/ since we cannot tell, without asking the filter... |
477 | 419 |
|
420 |
[inputStream atEnd] whileFalse:[ |
|
3757 | 421 |
nextElement := inputStream next. |
422 |
readAhead := filter value:nextElement. |
|
423 |
readAhead notNil ifTrue:[^ false]. |
|
477 | 424 |
]. |
425 |
||
426 |
^ true |
|
427 |
||
428 |
"Modified: 11.1.1997 / 17:16:45 / cg" |
|
474 | 429 |
! |
430 |
||
431 |
contentsSpecies |
|
432 |
"return the kind of collection I should return when asked |
|
433 |
for multiple elements." |
|
434 |
||
435 |
^ inputStream contentsSpecies |
|
436 |
||
437 |
"Created: 11.1.1997 / 16:23:22 / cg" |
|
955 | 438 |
! |
439 |
||
2786 | 440 |
isEmpty |
441 |
^ inputStream isEmpty |
|
442 |
||
443 |
"Created: / 06-08-2012 / 08:09:01 / cg" |
|
444 |
! |
|
445 |
||
3101 | 446 |
isEncodedStream |
447 |
^ inputStream isEncodedStream |
|
448 |
! |
|
449 |
||
3068 | 450 |
isReadable |
451 |
^ inputStream notNil and:[inputStream isReadable] |
|
452 |
! |
|
453 |
||
454 |
isWritable |
|
3779 | 455 |
"return true, if writing is supported by the receiver." |
456 |
||
3068 | 457 |
^ outputStream notNil and:[outputStream isWritable] |
458 |
! |
|
459 |
||
955 | 460 |
position |
461 |
"return the receiver streams position" |
|
462 |
||
463 |
|rawPosition| |
|
464 |
||
465 |
rawPosition := inputStream position. |
|
466 |
readAhead notNil ifTrue:[ |
|
2375 | 467 |
rawPosition := rawPosition - 1 |
955 | 468 |
]. |
469 |
^ rawPosition |
|
1239 | 470 |
! |
471 |
||
472 |
position0Based |
|
3082 | 473 |
<resource: #obsolete> |
1239 | 474 |
"return the receiver streams position" |
475 |
||
3082 | 476 |
^ self position |
1239 | 477 |
! |
478 |
||
479 |
position1Based |
|
3082 | 480 |
<resource: #obsolete> |
1239 | 481 |
"return the receiver streams position" |
482 |
||
3082 | 483 |
^ self position + 1 |
2695 | 484 |
! |
485 |
||
486 |
readStream |
|
487 |
^ self |
|
488 |
||
489 |
"Created: / 06-12-2011 / 01:05:26 / cg" |
|
2775 | 490 |
! |
491 |
||
492 |
size |
|
3067 | 493 |
"not always correct, but probably better than 0" |
494 |
||
495 |
"/ is that better? |
|
496 |
"/ self error:'size of input is unknown (due to filtering)' |
|
2788 | 497 |
^ inputStream size |
2775 | 498 |
|
499 |
"Created: / 05-08-2012 / 18:39:00 / cg" |
|
401 | 500 |
! ! |
501 |
||
472 | 502 |
!FilteringStream class methodsFor:'documentation'! |
401 | 503 |
|
504 |
version |
|
3757 | 505 |
^ '$Header$' |
2375 | 506 |
! |
507 |
||
508 |
version_CVS |
|
3757 | 509 |
^ '$Header$' |
401 | 510 |
! ! |
3067 | 511 |