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