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