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