author | sr |
Thu, 18 Oct 2007 12:16:14 +0200 | |
changeset 1901 | 5e6771205713 |
parent 988 | e7e70b439226 |
child 2377 | 2c8db8ee1b58 |
permissions | -rw-r--r-- |
473 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
3 |
All Rights Reserved |
473 | 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 |
||
14 |
||
988 | 15 |
"{ Package: 'stx:goodies' }" |
16 |
||
400 | 17 |
FilteringStream subclass:#FilteringLineStream |
474 | 18 |
instanceVariableNames:'lineBuffer lineFilter' |
399 | 19 |
classVariableNames:'' |
20 |
poolDictionaries:'' |
|
474 | 21 |
category:'Streams-Misc' |
399 | 22 |
! |
23 |
||
473 | 24 |
!FilteringLineStream class methodsFor:'documentation'! |
25 |
||
26 |
copyright |
|
27 |
" |
|
28 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
29 |
All Rights Reserved |
473 | 30 |
|
31 |
This software is furnished under a license and may be used |
|
32 |
only in accordance with the terms of that license and with the |
|
33 |
inclusion of the above copyright notice. This software may not |
|
34 |
be provided or otherwise made available to, or used by, any |
|
35 |
other person. No title to or ownership of the software is |
|
36 |
hereby transferred. |
|
37 |
" |
|
38 |
||
39 |
||
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
A FilteringLineStream is much like a FilteringStream, but |
|
45 |
processes its input line-wise. |
|
46 |
||
47 |
A FilteringLineStream can be connected to some input |
|
48 |
(from which characters are read via the ReadStream protocol), |
|
49 |
and/or to some output (to which characters are written via |
|
50 |
the WriteStream protocol. |
|
51 |
||
52 |
The FilteringLineStream itself performs some filtering/processing |
|
53 |
on the elements as they arrive, optionally suppressing filtering |
|
54 |
(i.e. removing) some elements. |
|
55 |
||
56 |
||
57 |
[author:] |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
58 |
Claus Gittinger |
473 | 59 |
|
60 |
[See also:] |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
61 |
FilteringStream ReadStream WriteStream |
473 | 62 |
" |
63 |
||
64 |
||
65 |
! |
|
399 | 66 |
|
67 |
examples |
|
68 |
" |
|
474 | 69 |
pushing the contents of a stream onto another stream |
70 |
(here, the Transcript) without a need to read everyting into a |
|
71 |
buffer or to reinvent the read-loop: |
|
988 | 72 |
[exBegin] |
474 | 73 |
|in pusher| |
74 |
||
75 |
in := 'Makefile' asFilename readStream. |
|
76 |
pusher := FilteringLineStream readingFrom:in writingTo:Transcript. |
|
77 |
pusher filterUpToEnd |
|
988 | 78 |
[exEnd] |
79 |
||
80 |
||
81 |
filter through an ActorStream: |
|
82 |
[exBegin] |
|
83 |
|lineNr in pusher actorPerLine| |
|
84 |
||
85 |
lineNr := 0. |
|
86 |
||
87 |
actorPerLine := ActorStream new. |
|
88 |
actorPerLine nextPutLineBlock:[:line | lineNr := lineNr + 1. Transcript show:lineNr; showCR:line]. |
|
89 |
||
90 |
in := 'Makefile' asFilename readStream. |
|
91 |
pusher := FilteringLineStream readingFrom:in writingTo:actorPerLine. |
|
92 |
pusher filterUpToEnd |
|
93 |
[exEnd] |
|
474 | 94 |
|
95 |
||
473 | 96 |
filter all comments (starting with '#') from a Makefile: |
988 | 97 |
[exBegin] |
400 | 98 |
|in filter| |
99 |
||
100 |
in := 'Makefile' asFilename readStream. |
|
101 |
||
474 | 102 |
filter := FilteringLineStream readingFrom:in. |
400 | 103 |
filter filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]]. |
104 |
||
105 |
filter outputStream:Transcript. |
|
106 |
||
107 |
filter filterUpToEnd |
|
988 | 108 |
[exEnd] |
400 | 109 |
|
110 |
||
473 | 111 |
the inverse - remove all comments from the Makefile: |
988 | 112 |
[exBegin] |
473 | 113 |
|in filter| |
114 |
||
115 |
in := 'Makefile' asFilename readStream. |
|
116 |
||
474 | 117 |
filter := FilteringLineStream readingFrom:in. |
473 | 118 |
filter filter:[:line | (line startsWith:'#') ifTrue:[nil] ifFalse:[line]]. |
400 | 119 |
|
473 | 120 |
filter outputStream:Transcript. |
121 |
||
122 |
filter filterUpToEnd |
|
988 | 123 |
[exEnd] |
473 | 124 |
|
125 |
||
126 |
feed a second filter from the first filters output; |
|
127 |
(the remains are all lines starting with '#' and ending with '-'): |
|
988 | 128 |
[exBegin] |
399 | 129 |
|in filter1 filter2| |
130 |
||
131 |
in := 'Makefile' asFilename readStream. |
|
132 |
||
474 | 133 |
filter1 := FilteringLineStream readingFrom:in. |
399 | 134 |
filter1 filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]]. |
135 |
||
474 | 136 |
filter2 := FilteringLineStream readingFrom:filter1. |
473 | 137 |
filter2 filter:[:line | (line endsWith:'-') ifTrue:[line] ifFalse:[nil]]. |
399 | 138 |
|
139 |
filter2 outputStream:Transcript. |
|
140 |
||
474 | 141 |
filter2 filterUpToEnd |
988 | 142 |
[exEnd] |
399 | 143 |
" |
144 |
! ! |
|
145 |
||
474 | 146 |
!FilteringLineStream methodsFor:'access - pull-reading'! |
147 |
||
148 |
filterUpToEnd |
|
149 |
"pull input from inputStream up to the end, |
|
150 |
push it filtered into the outputStream." |
|
151 |
||
152 |
[inputStream atEnd] whileFalse:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
153 |
self nextPutLine:(inputStream nextLine) |
474 | 154 |
]. |
155 |
||
156 |
"Created: 11.1.1997 / 16:09:05 / cg" |
|
157 |
! ! |
|
158 |
||
399 | 159 |
!FilteringLineStream methodsFor:'access - push-writing'! |
160 |
||
161 |
cr |
|
162 |
"finish a line, push it filtered into the outputStream" |
|
163 |
||
164 |
|output| |
|
165 |
||
474 | 166 |
lineFilter isNil ifTrue:[ |
988 | 167 |
outputStream nextPutLine:(lineBuffer ? '') |
474 | 168 |
] ifFalse:[ |
988 | 169 |
lineBuffer isNil ifTrue:[lineBuffer := '']. |
399 | 170 |
|
988 | 171 |
output := lineFilter value:lineBuffer. |
172 |
output notNil ifTrue:[ |
|
173 |
outputStream nextPutLine:output. |
|
174 |
] |
|
399 | 175 |
]. |
176 |
lineBuffer := ''. |
|
177 |
||
474 | 178 |
"Modified: 11.1.1997 / 16:32:40 / cg" |
399 | 179 |
! |
180 |
||
181 |
nextPut:something |
|
182 |
"collect a line and push it when a cr arrives" |
|
183 |
||
184 |
something == Character cr ifTrue:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
185 |
self cr |
399 | 186 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
187 |
lineBuffer isNil ifTrue:[lineBuffer := '']. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
188 |
lineBuffer := lineBuffer copyWith:something. |
399 | 189 |
] |
190 |
||
191 |
"Created: 2.7.1996 / 20:54:38 / cg" |
|
192 |
"Modified: 2.7.1996 / 21:03:26 / cg" |
|
193 |
! |
|
194 |
||
195 |
nextPutAll:something |
|
196 |
"collect a line and push it when a cr arrives" |
|
197 |
||
474 | 198 |
lineBuffer isNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
199 |
lineBuffer := something |
474 | 200 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
201 |
lineBuffer := lineBuffer , something. |
474 | 202 |
] |
203 |
||
204 |
"Modified: 11.1.1997 / 16:31:28 / cg" |
|
205 |
! ! |
|
206 |
||
207 |
!FilteringLineStream methodsFor:'accessing'! |
|
399 | 208 |
|
474 | 209 |
filter |
210 |
"return the filter" |
|
211 |
||
212 |
^ lineFilter |
|
213 |
||
214 |
"Created: 11.1.1997 / 16:13:15 / cg" |
|
215 |
! |
|
216 |
||
217 |
filter:aBlock |
|
218 |
"set the filter" |
|
219 |
||
220 |
lineFilter := aBlock |
|
221 |
||
222 |
"Created: 11.1.1997 / 16:13:25 / cg" |
|
399 | 223 |
! ! |
224 |
||
473 | 225 |
!FilteringLineStream class methodsFor:'documentation'! |
399 | 226 |
|
227 |
version |
|
988 | 228 |
^ '$Header: /cvs/stx/stx/libbasic2/FilteringLineStream.st,v 1.6 2001-11-08 15:34:27 md Exp $' |
399 | 229 |
! ! |