author | Claus Gittinger <cg@exept.de> |
Thu, 06 Sep 2007 17:31:09 +0200 | |
changeset 1894 | 8bf137acc445 |
parent 1483 | 940e5d3acb4a |
child 2085 | 234895d9f5e3 |
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 |
" |
|
12 |
||
13 |
||
889 | 14 |
"{ Package: 'stx:goodies' }" |
15 |
||
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 |
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 |
889 | 84 |
(here, the Transcript) without a need to read everything into a |
474 | 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) |
|
889 | 88 |
[exBegin] |
474 | 89 |
|in pusher| |
90 |
||
91 |
in := 'Makefile' asFilename readStream. |
|
92 |
pusher := FilteringStream readingFrom:in writingTo:Transcript. |
|
93 |
pusher filterUpToEnd |
|
889 | 94 |
[exEnd] |
474 | 95 |
|
96 |
||
401 | 97 |
filter random numbers: |
889 | 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:[ |
|
889 | 107 |
Transcript showCR:(filter next printString). |
401 | 108 |
] |
889 | 109 |
[exEnd] |
401 | 110 |
|
111 |
filtering prime numbers: |
|
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 |
||
472 | 137 |
10000 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 |
||
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 |
||
1291 | 177 |
^ (self basicNew inputStream:aReadStream) initialize. |
474 | 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 |
||
1291 | 188 |
newStream := self basicNew. |
474 | 189 |
newStream inputStream:aReadStream. |
190 |
newStream outputStream:aWriteStream. |
|
1291 | 191 |
newStream initialize. |
474 | 192 |
^ newStream |
193 |
||
194 |
"Created: 11.1.1997 / 15:32:28 / cg" |
|
195 |
! |
|
196 |
||
197 |
writingTo:aWriteStream |
|
198 |
"create and return a new filteringStream, which writes to |
|
199 |
another stream" |
|
200 |
||
1291 | 201 |
^ (self basicNew outputStream:aWriteStream) initialize. |
474 | 202 |
|
203 |
"Created: 11.1.1997 / 15:32:36 / cg" |
|
204 |
! ! |
|
205 |
||
401 | 206 |
!FilteringStream methodsFor:'access - pull-reading'! |
207 |
||
1291 | 208 |
contents |
209 |
^ inputStream contents |
|
210 |
! |
|
211 |
||
401 | 212 |
filterUpToEnd |
213 |
"pull input from inputStream up to the end, |
|
474 | 214 |
push it filtered into the outputStream." |
401 | 215 |
|
216 |
[inputStream atEnd] whileFalse:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
217 |
self nextPut:(inputStream next) |
401 | 218 |
]. |
219 |
||
220 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
474 | 221 |
"Modified: 11.1.1997 / 16:08:35 / cg" |
401 | 222 |
! |
223 |
||
224 |
next |
|
225 |
"pull input from inputStream and |
|
226 |
push it filtered into the outputStream" |
|
227 |
||
228 |
|input output| |
|
229 |
||
477 | 230 |
"/ readAhead input has already been filtered |
231 |
"/ (see #atEnd) |
|
232 |
||
233 |
readAhead notNil ifTrue:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
234 |
input := readAhead. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
235 |
readAhead := nil. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
236 |
^ input |
477 | 237 |
]. |
238 |
||
401 | 239 |
[inputStream atEnd] whileFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
240 |
"/ get an element |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
241 |
input := inputStream next. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
242 |
filter isNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
243 |
^ input |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
244 |
]. |
474 | 245 |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
246 |
"/ filter it - this may return nil, to eat it |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
247 |
output := filter value:input. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
248 |
output notNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
249 |
"/ good - output it |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
250 |
^ output. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
251 |
]. |
401 | 252 |
]. |
253 |
^ nil |
|
254 |
||
255 |
"Created: 2.7.1996 / 21:09:58 / cg" |
|
477 | 256 |
"Modified: 11.1.1997 / 17:17:27 / cg" |
889 | 257 |
! |
258 |
||
259 |
peek |
|
260 |
"peek ahead for the next character" |
|
261 |
||
262 |
|input output| |
|
263 |
||
264 |
readAhead notNil ifTrue:[ |
|
265 |
^ readAhead |
|
266 |
]. |
|
267 |
||
268 |
[inputStream atEnd] whileFalse:[ |
|
269 |
"/ get an element |
|
270 |
input := inputStream peek. |
|
271 |
filter isNil ifTrue:[ |
|
272 |
^ input |
|
273 |
]. |
|
274 |
||
275 |
"/ filter it - this may return nil, to eat it |
|
276 |
inputStream next. |
|
277 |
output := filter value:input. |
|
278 |
output notNil ifTrue:[ |
|
279 |
"/ good - output it |
|
280 |
readAhead := output. |
|
281 |
^ output. |
|
282 |
]. |
|
283 |
]. |
|
1483 | 284 |
^ self pastEndRead |
889 | 285 |
|
286 |
! |
|
287 |
||
288 |
peekOrNil |
|
289 |
"peek ahead for the next character, or return nil" |
|
290 |
||
291 |
|input output| |
|
292 |
||
293 |
readAhead notNil ifTrue:[ |
|
294 |
^ readAhead |
|
295 |
]. |
|
296 |
||
297 |
[inputStream atEnd] whileFalse:[ |
|
298 |
"/ get an element |
|
299 |
input := inputStream peek. |
|
300 |
filter isNil ifTrue:[ |
|
301 |
^ input |
|
302 |
]. |
|
303 |
||
304 |
"/ filter it - this may return nil, to eat it |
|
305 |
inputStream next. |
|
306 |
output := filter value:input. |
|
307 |
output notNil ifTrue:[ |
|
308 |
"/ good - output it |
|
309 |
readAhead := output. |
|
310 |
^ output. |
|
311 |
]. |
|
312 |
]. |
|
313 |
^ nil |
|
401 | 314 |
! ! |
315 |
||
316 |
!FilteringStream methodsFor:'access - push-writing'! |
|
317 |
||
318 |
nextPut:something |
|
319 |
"push something through the filter" |
|
320 |
||
321 |
|output| |
|
322 |
||
472 | 323 |
"/ filter it |
474 | 324 |
filter isNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
325 |
outputStream nextPut:something |
474 | 326 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
327 |
output := filter value:something. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
328 |
output notNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
329 |
outputStream nextPut:output |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
330 |
] |
401 | 331 |
] |
332 |
||
474 | 333 |
"Modified: 11.1.1997 / 16:12:52 / cg" |
401 | 334 |
! ! |
335 |
||
336 |
!FilteringStream methodsFor:'accessing'! |
|
337 |
||
338 |
filter |
|
339 |
"return the filter" |
|
340 |
||
341 |
^ filter |
|
342 |
||
343 |
"Modified: 2.7.1996 / 21:03:36 / cg" |
|
344 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
345 |
! |
|
346 |
||
347 |
filter:something |
|
348 |
"set the filter" |
|
349 |
||
350 |
filter := something. |
|
351 |
||
352 |
"Modified: 2.7.1996 / 21:03:40 / cg" |
|
353 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
354 |
! |
|
355 |
||
356 |
inputStream |
|
357 |
"return the inputStream" |
|
358 |
||
359 |
^ inputStream |
|
360 |
||
361 |
"Modified: 2.7.1996 / 21:03:43 / cg" |
|
362 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
363 |
! |
|
364 |
||
365 |
inputStream:something |
|
366 |
"set the inputStream" |
|
367 |
||
368 |
inputStream := something. |
|
369 |
||
370 |
"Modified: 2.7.1996 / 21:03:46 / cg" |
|
371 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
372 |
! |
|
373 |
||
374 |
outputStream |
|
375 |
"return the outputStream" |
|
376 |
||
377 |
^ outputStream |
|
378 |
||
379 |
"Modified: 2.7.1996 / 21:03:49 / cg" |
|
380 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
381 |
! |
|
382 |
||
383 |
outputStream:something |
|
384 |
"set the outputStream" |
|
385 |
||
386 |
outputStream := something. |
|
387 |
||
388 |
"Modified: 2.7.1996 / 21:03:52 / cg" |
|
389 |
"Created: 2.7.1996 / 21:06:42 / cg" |
|
474 | 390 |
! ! |
401 | 391 |
|
474 | 392 |
!FilteringStream methodsFor:'misc'! |
401 | 393 |
|
474 | 394 |
close |
395 |
"when I am closed, close my input - if any" |
|
401 | 396 |
|
474 | 397 |
inputStream notNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
398 |
inputStream close |
474 | 399 |
] |
401 | 400 |
|
474 | 401 |
"Created: 11.1.1997 / 15:27:17 / cg" |
401 | 402 |
! ! |
403 |
||
404 |
!FilteringStream methodsFor:'queries'! |
|
405 |
||
406 |
atEnd |
|
472 | 407 |
"return true, if the receiver stream is at the end" |
408 |
||
477 | 409 |
|nextElement| |
410 |
||
411 |
readAhead notNil ifTrue:[^ false]. |
|
412 |
||
413 |
filter isNil ifTrue:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
414 |
"/ then, its easy |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
415 |
^ inputStream atEnd |
477 | 416 |
]. |
401 | 417 |
|
477 | 418 |
"/ with a filter, things are more complicated, |
419 |
"/ since we cannot tell, without asking the filter ... |
|
420 |
||
421 |
||
422 |
[inputStream atEnd] whileFalse:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
423 |
nextElement := inputStream next. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
424 |
readAhead := filter value:nextElement. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
479
diff
changeset
|
425 |
readAhead notNil ifTrue:[^ false]. |
477 | 426 |
]. |
427 |
||
428 |
^ true |
|
429 |
||
430 |
"Modified: 11.1.1997 / 17:16:45 / cg" |
|
474 | 431 |
! |
432 |
||
433 |
contentsSpecies |
|
434 |
"return the kind of collection I should return when asked |
|
435 |
for multiple elements." |
|
436 |
||
437 |
^ inputStream contentsSpecies |
|
438 |
||
439 |
"Created: 11.1.1997 / 16:23:22 / cg" |
|
955 | 440 |
! |
441 |
||
442 |
position |
|
443 |
"return the receiver streams position" |
|
444 |
||
445 |
|rawPosition| |
|
446 |
||
447 |
rawPosition := inputStream position. |
|
448 |
readAhead notNil ifTrue:[ |
|
449 |
rawPosition := rawPosition - 1 |
|
450 |
]. |
|
451 |
^ rawPosition |
|
1239 | 452 |
! |
453 |
||
454 |
position0Based |
|
455 |
"return the receiver streams position" |
|
456 |
||
457 |
|rawPosition| |
|
458 |
||
459 |
rawPosition := inputStream position0Based. |
|
460 |
readAhead notNil ifTrue:[ |
|
461 |
rawPosition := rawPosition - 1 |
|
462 |
]. |
|
463 |
^ rawPosition |
|
464 |
! |
|
465 |
||
466 |
position1Based |
|
467 |
"return the receiver streams position" |
|
468 |
||
469 |
|rawPosition| |
|
470 |
||
471 |
rawPosition := inputStream position1Based. |
|
472 |
readAhead notNil ifTrue:[ |
|
473 |
rawPosition := rawPosition - 1 |
|
474 |
]. |
|
475 |
^ rawPosition |
|
401 | 476 |
! ! |
477 |
||
472 | 478 |
!FilteringStream class methodsFor:'documentation'! |
401 | 479 |
|
480 |
version |
|
1483 | 481 |
^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.11 2004-10-01 07:53:42 cg Exp $' |
401 | 482 |
! ! |