author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 14:28:51 +0200 | |
changeset 5050 | 44fa8672d102 |
parent 3437 | 1e3c96378a00 |
child 5161 | a6ed595625e9 |
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 |
" |
|
2377 | 12 |
"{ Package: 'stx:libbasic2' }" |
988 | 13 |
|
400 | 14 |
FilteringStream subclass:#FilteringLineStream |
474 | 15 |
instanceVariableNames:'lineBuffer lineFilter' |
399 | 16 |
classVariableNames:'' |
17 |
poolDictionaries:'' |
|
474 | 18 |
category:'Streams-Misc' |
399 | 19 |
! |
20 |
||
473 | 21 |
!FilteringLineStream 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:
474
diff
changeset
|
26 |
All Rights Reserved |
473 | 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 |
||
39 |
documentation |
|
40 |
" |
|
41 |
A FilteringLineStream is much like a FilteringStream, but |
|
42 |
processes its input line-wise. |
|
43 |
||
44 |
A FilteringLineStream can be connected to some input |
|
45 |
(from which characters are read via the ReadStream protocol), |
|
46 |
and/or to some output (to which characters are written via |
|
47 |
the WriteStream protocol. |
|
48 |
||
49 |
The FilteringLineStream itself performs some filtering/processing |
|
50 |
on the elements as they arrive, optionally suppressing filtering |
|
51 |
(i.e. removing) some elements. |
|
52 |
||
53 |
||
54 |
[author:] |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
55 |
Claus Gittinger |
473 | 56 |
|
57 |
[See also:] |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
58 |
FilteringStream ReadStream WriteStream |
473 | 59 |
" |
60 |
||
61 |
||
62 |
! |
|
399 | 63 |
|
64 |
examples |
|
65 |
" |
|
474 | 66 |
pushing the contents of a stream onto another stream |
67 |
(here, the Transcript) without a need to read everyting into a |
|
68 |
buffer or to reinvent the read-loop: |
|
3437 | 69 |
[exBegin] |
474 | 70 |
|in pusher| |
71 |
||
72 |
in := 'Makefile' asFilename readStream. |
|
73 |
pusher := FilteringLineStream readingFrom:in writingTo:Transcript. |
|
74 |
pusher filterUpToEnd |
|
3437 | 75 |
[exEnd] |
76 |
||
77 |
||
78 |
collect a command's stdout and stderr, colorizing error output in red and normal output in green: |
|
79 |
[exBegin] |
|
80 |
|in out stdoutColorizer errColorizer| |
|
81 |
||
82 |
out := Transcript. |
|
83 |
in := OperatingSystem |
|
84 |
executeCommand:'bc' |
|
85 |
inputFrom: |
|
86 |
'4+5 |
|
87 |
bla bla foo |
|
88 |
4+5 |
|
89 |
bar |
|
90 |
' readStream |
|
91 |
outputTo:((FilteringLineStream writingTo:out) filter:[:line | line colorizeAllWith:Color green darkened]) |
|
92 |
errorTo:((FilteringLineStream writingTo:out) filter:[:line | line colorizeAllWith:Color red]). |
|
93 |
[exEnd] |
|
988 | 94 |
|
95 |
||
96 |
filter through an ActorStream: |
|
3437 | 97 |
[exBegin] |
988 | 98 |
|lineNr in pusher actorPerLine| |
99 |
||
100 |
lineNr := 0. |
|
101 |
||
102 |
actorPerLine := ActorStream new. |
|
103 |
actorPerLine nextPutLineBlock:[:line | lineNr := lineNr + 1. Transcript show:lineNr; showCR:line]. |
|
104 |
||
105 |
in := 'Makefile' asFilename readStream. |
|
106 |
pusher := FilteringLineStream readingFrom:in writingTo:actorPerLine. |
|
107 |
pusher filterUpToEnd |
|
3437 | 108 |
[exEnd] |
474 | 109 |
|
110 |
||
473 | 111 |
filter all comments (starting with '#') from a Makefile: |
3437 | 112 |
[exBegin] |
400 | 113 |
|in filter| |
114 |
||
115 |
in := 'Makefile' asFilename readStream. |
|
116 |
||
474 | 117 |
filter := FilteringLineStream readingFrom:in. |
400 | 118 |
filter filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]]. |
119 |
||
120 |
filter outputStream:Transcript. |
|
121 |
||
122 |
filter filterUpToEnd |
|
3437 | 123 |
[exEnd] |
400 | 124 |
|
125 |
||
473 | 126 |
the inverse - remove all comments from the Makefile: |
3437 | 127 |
[exBegin] |
473 | 128 |
|in filter| |
129 |
||
130 |
in := 'Makefile' asFilename readStream. |
|
131 |
||
474 | 132 |
filter := FilteringLineStream readingFrom:in. |
473 | 133 |
filter filter:[:line | (line startsWith:'#') ifTrue:[nil] ifFalse:[line]]. |
400 | 134 |
|
473 | 135 |
filter outputStream:Transcript. |
136 |
||
137 |
filter filterUpToEnd |
|
3437 | 138 |
[exEnd] |
473 | 139 |
|
140 |
||
141 |
feed a second filter from the first filters output; |
|
142 |
(the remains are all lines starting with '#' and ending with '-'): |
|
3437 | 143 |
[exBegin] |
399 | 144 |
|in filter1 filter2| |
145 |
||
146 |
in := 'Makefile' asFilename readStream. |
|
147 |
||
474 | 148 |
filter1 := FilteringLineStream readingFrom:in. |
399 | 149 |
filter1 filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]]. |
150 |
||
474 | 151 |
filter2 := FilteringLineStream readingFrom:filter1. |
473 | 152 |
filter2 filter:[:line | (line endsWith:'-') ifTrue:[line] ifFalse:[nil]]. |
399 | 153 |
|
154 |
filter2 outputStream:Transcript. |
|
155 |
||
474 | 156 |
filter2 filterUpToEnd |
3437 | 157 |
[exEnd] |
399 | 158 |
" |
159 |
! ! |
|
160 |
||
474 | 161 |
!FilteringLineStream methodsFor:'access - pull-reading'! |
162 |
||
163 |
filterUpToEnd |
|
164 |
"pull input from inputStream up to the end, |
|
165 |
push it filtered into the outputStream." |
|
166 |
||
167 |
[inputStream atEnd] whileFalse:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
168 |
self nextPutLine:(inputStream nextLine) |
474 | 169 |
]. |
170 |
||
171 |
"Created: 11.1.1997 / 16:09:05 / cg" |
|
172 |
! ! |
|
173 |
||
399 | 174 |
!FilteringLineStream methodsFor:'access - push-writing'! |
175 |
||
176 |
cr |
|
177 |
"finish a line, push it filtered into the outputStream" |
|
178 |
||
179 |
|output| |
|
180 |
||
474 | 181 |
lineFilter isNil ifTrue:[ |
2377 | 182 |
outputStream nextPutLine:(lineBuffer ? '') |
474 | 183 |
] ifFalse:[ |
2377 | 184 |
lineBuffer isNil ifTrue:[lineBuffer := '']. |
399 | 185 |
|
2377 | 186 |
output := lineFilter value:lineBuffer. |
187 |
output notNil ifTrue:[ |
|
188 |
outputStream nextPutLine:output. |
|
189 |
] |
|
399 | 190 |
]. |
191 |
lineBuffer := ''. |
|
192 |
||
474 | 193 |
"Modified: 11.1.1997 / 16:32:40 / cg" |
399 | 194 |
! |
195 |
||
196 |
nextPut:something |
|
197 |
"collect a line and push it when a cr arrives" |
|
198 |
||
199 |
something == Character cr ifTrue:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
200 |
self cr |
399 | 201 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
202 |
lineBuffer isNil ifTrue:[lineBuffer := '']. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
203 |
lineBuffer := lineBuffer copyWith:something. |
399 | 204 |
] |
205 |
||
206 |
"Created: 2.7.1996 / 20:54:38 / cg" |
|
207 |
"Modified: 2.7.1996 / 21:03:26 / cg" |
|
208 |
! |
|
209 |
||
210 |
nextPutAll:something |
|
211 |
"collect a line and push it when a cr arrives" |
|
212 |
||
474 | 213 |
lineBuffer isNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
214 |
lineBuffer := something |
474 | 215 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
216 |
lineBuffer := lineBuffer , something. |
474 | 217 |
] |
218 |
||
219 |
"Modified: 11.1.1997 / 16:31:28 / cg" |
|
220 |
! ! |
|
221 |
||
222 |
!FilteringLineStream methodsFor:'accessing'! |
|
399 | 223 |
|
474 | 224 |
filter |
225 |
"return the filter" |
|
226 |
||
227 |
^ lineFilter |
|
228 |
||
229 |
"Created: 11.1.1997 / 16:13:15 / cg" |
|
230 |
! |
|
231 |
||
232 |
filter:aBlock |
|
233 |
"set the filter" |
|
234 |
||
235 |
lineFilter := aBlock |
|
236 |
||
237 |
"Created: 11.1.1997 / 16:13:25 / cg" |
|
2787 | 238 |
! |
239 |
||
3437 | 240 |
isBinary |
241 |
^ (inputStream ? outputStream) isBinary |
|
242 |
! |
|
243 |
||
2787 | 244 |
size |
245 |
"mhmh - please use isEmpty; anything else is unknown" |
|
246 |
||
247 |
self error:'size of input is unknown (due to filtering)' |
|
248 |
||
249 |
"Created: / 06-08-2012 / 08:21:12 / cg" |
|
399 | 250 |
! ! |
251 |
||
473 | 252 |
!FilteringLineStream class methodsFor:'documentation'! |
399 | 253 |
|
254 |
version |
|
3437 | 255 |
^ '$Header: /cvs/stx/stx/libbasic2/FilteringLineStream.st,v 1.9 2014-11-08 10:20:41 cg Exp $' |
2377 | 256 |
! |
257 |
||
258 |
version_CVS |
|
3437 | 259 |
^ '$Header: /cvs/stx/stx/libbasic2/FilteringLineStream.st,v 1.9 2014-11-08 10:20:41 cg Exp $' |
399 | 260 |
! ! |
3437 | 261 |