472
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1996 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
|
401
|
14 |
Stream subclass:#FilteringStream
|
|
15 |
instanceVariableNames:'inputStream outputStream filter unbound'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Streams'
|
|
19 |
!
|
|
20 |
|
472
|
21 |
!FilteringStream class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1996 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
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.
|
|
44 |
The FilteringStream itself performs some filtering/processing
|
|
45 |
on the elements as they arrive.
|
|
46 |
|
|
47 |
|
|
48 |
[instance variables:]
|
|
49 |
inputStream <Stream> the stream from which elements are read
|
|
50 |
outputStream <Stream> the stream to which elements are written
|
|
51 |
filter <Block> the filter block;
|
|
52 |
unbound <Boolean> if true, the stream is unbound.
|
|
53 |
|
|
54 |
[author:]
|
|
55 |
Claus Gittinger
|
|
56 |
|
|
57 |
[See also:]
|
|
58 |
ReadStream WriteStream
|
|
59 |
"
|
|
60 |
|
|
61 |
!
|
401
|
62 |
|
|
63 |
examples
|
|
64 |
"
|
|
65 |
filter random numbers:
|
472
|
66 |
[exBegin]
|
401
|
67 |
|in filter|
|
|
68 |
|
|
69 |
in := Random new.
|
|
70 |
|
|
71 |
filter := FilteringLineStream basicNew.
|
|
72 |
filter inputStream:in.
|
|
73 |
filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]].
|
|
74 |
|
|
75 |
20 timesRepeat:[
|
|
76 |
Transcript showCR:(filter next printString).
|
|
77 |
]
|
472
|
78 |
[exEnd]
|
401
|
79 |
|
|
80 |
filtering prime numbers:
|
472
|
81 |
[exBegin]
|
401
|
82 |
|num generator primeFilter addFilter|
|
|
83 |
|
|
84 |
num := 1.
|
|
85 |
generator := Plug new.
|
|
86 |
generator respondTo:#next
|
|
87 |
with:[num := num + 1. num].
|
|
88 |
generator respondTo:#atEnd
|
|
89 |
with:[false].
|
|
90 |
|
|
91 |
addFilter := [:prime | |newFilter|
|
|
92 |
newFilter := FilteringLineStream basicNew.
|
|
93 |
newFilter filter:[:num | (num \\ prime) == 0 ifTrue:[
|
|
94 |
nil
|
|
95 |
] ifFalse:[
|
|
96 |
num
|
|
97 |
]
|
|
98 |
].
|
|
99 |
newFilter inputStream:primeFilter.
|
|
100 |
primeFilter := newFilter
|
|
101 |
].
|
|
102 |
|
|
103 |
addFilter value:2.
|
|
104 |
primeFilter inputStream:generator.
|
|
105 |
|
472
|
106 |
10000 timesRepeat:[
|
401
|
107 |
|nextPrime|
|
|
108 |
|
|
109 |
nextPrime := primeFilter next.
|
|
110 |
addFilter value:nextPrime.
|
|
111 |
Transcript showCR:nextPrime.
|
|
112 |
]
|
472
|
113 |
[exEnd]
|
401
|
114 |
"
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
! !
|
|
119 |
|
|
120 |
!FilteringStream methodsFor:'access - pull-reading'!
|
|
121 |
|
|
122 |
filterUpToEnd
|
|
123 |
"pull input from inputStream up to the end,
|
|
124 |
push it filtered into the outputStream"
|
|
125 |
|
|
126 |
[inputStream atEnd] whileFalse:[
|
|
127 |
self nextPut:(inputStream next)
|
|
128 |
].
|
|
129 |
|
|
130 |
"Modified: 2.7.1996 / 21:02:49 / cg"
|
|
131 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
132 |
!
|
|
133 |
|
|
134 |
next
|
|
135 |
"pull input from inputStream and
|
|
136 |
push it filtered into the outputStream"
|
|
137 |
|
|
138 |
|input output|
|
|
139 |
|
|
140 |
[inputStream atEnd] whileFalse:[
|
472
|
141 |
"/ get an element
|
401
|
142 |
input := inputStream next.
|
472
|
143 |
"/ filter it - this may return nil, to eat it
|
401
|
144 |
output := filter value:input.
|
|
145 |
output notNil ifTrue:[
|
472
|
146 |
"/ good - output it
|
401
|
147 |
^ output.
|
|
148 |
].
|
|
149 |
].
|
|
150 |
^ nil
|
|
151 |
|
|
152 |
"Created: 2.7.1996 / 21:09:58 / cg"
|
472
|
153 |
"Modified: 10.1.1997 / 20:23:36 / cg"
|
401
|
154 |
! !
|
|
155 |
|
|
156 |
!FilteringStream methodsFor:'access - push-writing'!
|
|
157 |
|
|
158 |
nextPut:something
|
|
159 |
"push something through the filter"
|
|
160 |
|
|
161 |
|output|
|
|
162 |
|
472
|
163 |
"/ filter it
|
401
|
164 |
output := filter value:something.
|
|
165 |
output notNil ifTrue:[
|
|
166 |
outputStream nextPut:output
|
|
167 |
]
|
|
168 |
|
|
169 |
"Created: 2.7.1996 / 21:07:58 / cg"
|
472
|
170 |
"Modified: 10.1.1997 / 20:23:56 / cg"
|
401
|
171 |
! !
|
|
172 |
|
|
173 |
!FilteringStream methodsFor:'accessing'!
|
|
174 |
|
|
175 |
filter
|
|
176 |
"return the filter"
|
|
177 |
|
|
178 |
^ filter
|
|
179 |
|
|
180 |
"Modified: 2.7.1996 / 21:03:36 / cg"
|
|
181 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
182 |
!
|
|
183 |
|
|
184 |
filter:something
|
|
185 |
"set the filter"
|
|
186 |
|
|
187 |
filter := something.
|
|
188 |
|
|
189 |
"Modified: 2.7.1996 / 21:03:40 / cg"
|
|
190 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
191 |
!
|
|
192 |
|
|
193 |
inputStream
|
|
194 |
"return the inputStream"
|
|
195 |
|
|
196 |
^ inputStream
|
|
197 |
|
|
198 |
"Modified: 2.7.1996 / 21:03:43 / cg"
|
|
199 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
200 |
!
|
|
201 |
|
|
202 |
inputStream:something
|
|
203 |
"set the inputStream"
|
|
204 |
|
|
205 |
inputStream := something.
|
|
206 |
|
|
207 |
"Modified: 2.7.1996 / 21:03:46 / cg"
|
|
208 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
209 |
!
|
|
210 |
|
|
211 |
outputStream
|
|
212 |
"return the outputStream"
|
|
213 |
|
|
214 |
^ outputStream
|
|
215 |
|
|
216 |
"Modified: 2.7.1996 / 21:03:49 / cg"
|
|
217 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
218 |
!
|
|
219 |
|
|
220 |
outputStream:something
|
|
221 |
"set the outputStream"
|
|
222 |
|
|
223 |
outputStream := something.
|
|
224 |
|
|
225 |
"Modified: 2.7.1996 / 21:03:52 / cg"
|
|
226 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
227 |
!
|
|
228 |
|
|
229 |
unbound
|
472
|
230 |
"return the unbound flag"
|
401
|
231 |
|
|
232 |
^ unbound
|
|
233 |
|
|
234 |
"Created: 2.7.1996 / 21:25:40 / cg"
|
472
|
235 |
"Modified: 10.1.1997 / 20:21:53 / cg"
|
401
|
236 |
!
|
|
237 |
|
|
238 |
unbound:something
|
472
|
239 |
"set the unbound flag"
|
401
|
240 |
|
|
241 |
unbound := something.
|
|
242 |
|
|
243 |
"Created: 2.7.1996 / 21:25:40 / cg"
|
472
|
244 |
"Modified: 10.1.1997 / 20:21:59 / cg"
|
401
|
245 |
! !
|
|
246 |
|
|
247 |
!FilteringStream methodsFor:'queries'!
|
|
248 |
|
|
249 |
atEnd
|
472
|
250 |
"return true, if the receiver stream is at the end"
|
|
251 |
|
401
|
252 |
unbound ifTrue:[^ false].
|
|
253 |
^ inputStream atEnd
|
|
254 |
|
|
255 |
"Created: 2.7.1996 / 21:19:51 / cg"
|
472
|
256 |
"Modified: 10.1.1997 / 20:22:20 / cg"
|
401
|
257 |
! !
|
|
258 |
|
472
|
259 |
!FilteringStream class methodsFor:'documentation'!
|
401
|
260 |
|
|
261 |
version
|
472
|
262 |
^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.2 1997-01-10 20:51:24 cg Exp $'
|
401
|
263 |
! !
|