author | Claus Gittinger <cg@exept.de> |
Thu, 02 Mar 2000 15:15:13 +0100 | |
changeset 885 | c31412b26306 |
parent 474 | 61cb199537b5 |
child 988 | e7e70b439226 |
permissions | -rw-r--r-- |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
1 |
"{ Package: 'stx:goodies' }" |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
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 |
" |
|
14 |
||
15 |
||
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: |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
72 |
[exBegin] |
474 | 73 |
|in pusher| |
74 |
||
75 |
in := 'Makefile' asFilename readStream. |
|
76 |
pusher := FilteringLineStream readingFrom:in writingTo:Transcript. |
|
77 |
pusher filterUpToEnd |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
78 |
[exEnd] |
474 | 79 |
|
80 |
||
473 | 81 |
filter all comments (starting with '#') from a Makefile: |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
82 |
[exBegin] |
400 | 83 |
|in filter| |
84 |
||
85 |
in := 'Makefile' asFilename readStream. |
|
86 |
||
474 | 87 |
filter := FilteringLineStream readingFrom:in. |
400 | 88 |
filter filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]]. |
89 |
||
90 |
filter outputStream:Transcript. |
|
91 |
||
92 |
filter filterUpToEnd |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
93 |
[exEnd] |
400 | 94 |
|
95 |
||
473 | 96 |
the inverse - remove all comments from the Makefile: |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
97 |
[exBegin] |
473 | 98 |
|in filter| |
99 |
||
100 |
in := 'Makefile' asFilename readStream. |
|
101 |
||
474 | 102 |
filter := FilteringLineStream readingFrom:in. |
473 | 103 |
filter filter:[:line | (line startsWith:'#') ifTrue:[nil] ifFalse:[line]]. |
400 | 104 |
|
473 | 105 |
filter outputStream:Transcript. |
106 |
||
107 |
filter filterUpToEnd |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
108 |
[exEnd] |
473 | 109 |
|
110 |
||
111 |
feed a second filter from the first filters output; |
|
112 |
(the remains are all lines starting with '#' and ending with '-'): |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
113 |
[exBegin] |
399 | 114 |
|in filter1 filter2| |
115 |
||
116 |
in := 'Makefile' asFilename readStream. |
|
117 |
||
474 | 118 |
filter1 := FilteringLineStream readingFrom:in. |
399 | 119 |
filter1 filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]]. |
120 |
||
474 | 121 |
filter2 := FilteringLineStream readingFrom:filter1. |
473 | 122 |
filter2 filter:[:line | (line endsWith:'-') ifTrue:[line] ifFalse:[nil]]. |
399 | 123 |
|
124 |
filter2 outputStream:Transcript. |
|
125 |
||
474 | 126 |
filter2 filterUpToEnd |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
127 |
[exEnd] |
399 | 128 |
" |
129 |
||
130 |
! ! |
|
131 |
||
474 | 132 |
!FilteringLineStream methodsFor:'access - pull-reading'! |
133 |
||
134 |
filterUpToEnd |
|
135 |
"pull input from inputStream up to the end, |
|
136 |
push it filtered into the outputStream." |
|
137 |
||
138 |
[inputStream atEnd] whileFalse:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
139 |
self nextPutLine:(inputStream nextLine) |
474 | 140 |
]. |
141 |
||
142 |
"Created: 11.1.1997 / 16:09:05 / cg" |
|
143 |
! ! |
|
144 |
||
399 | 145 |
!FilteringLineStream methodsFor:'access - push-writing'! |
146 |
||
147 |
cr |
|
148 |
"finish a line, push it filtered into the outputStream" |
|
149 |
||
150 |
|output| |
|
151 |
||
474 | 152 |
lineFilter isNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
153 |
lineBuffer notNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
154 |
outputStream nextPutAll:lineBuffer |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
155 |
]. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
156 |
outputStream cr |
474 | 157 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
158 |
lineBuffer isNil ifTrue:[lineBuffer := '']. |
399 | 159 |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
160 |
output := lineFilter value:lineBuffer. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
161 |
output notNil ifTrue:[ |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
162 |
outputStream nextPutAll:output. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
163 |
outputStream cr. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
164 |
] |
399 | 165 |
]. |
166 |
lineBuffer := ''. |
|
167 |
||
474 | 168 |
"Modified: 11.1.1997 / 16:32:40 / cg" |
399 | 169 |
! |
170 |
||
171 |
nextPut:something |
|
172 |
"collect a line and push it when a cr arrives" |
|
173 |
||
174 |
something == Character cr ifTrue:[ |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
175 |
self cr |
399 | 176 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
177 |
lineBuffer isNil ifTrue:[lineBuffer := '']. |
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
178 |
lineBuffer := lineBuffer copyWith:something. |
399 | 179 |
] |
180 |
||
181 |
"Created: 2.7.1996 / 20:54:38 / cg" |
|
182 |
"Modified: 2.7.1996 / 21:03:26 / cg" |
|
183 |
! |
|
184 |
||
185 |
nextPutAll:something |
|
186 |
"collect a line and push it when a cr arrives" |
|
187 |
||
474 | 188 |
lineBuffer isNil ifTrue:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
189 |
lineBuffer := something |
474 | 190 |
] ifFalse:[ |
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
191 |
lineBuffer := lineBuffer , something. |
474 | 192 |
] |
193 |
||
194 |
"Modified: 11.1.1997 / 16:31:28 / cg" |
|
195 |
! ! |
|
196 |
||
197 |
!FilteringLineStream methodsFor:'accessing'! |
|
399 | 198 |
|
474 | 199 |
filter |
200 |
"return the filter" |
|
201 |
||
202 |
^ lineFilter |
|
203 |
||
204 |
"Created: 11.1.1997 / 16:13:15 / cg" |
|
205 |
! |
|
206 |
||
207 |
filter:aBlock |
|
208 |
"set the filter" |
|
209 |
||
210 |
lineFilter := aBlock |
|
211 |
||
212 |
"Created: 11.1.1997 / 16:13:25 / cg" |
|
399 | 213 |
! ! |
214 |
||
473 | 215 |
!FilteringLineStream class methodsFor:'documentation'! |
399 | 216 |
|
217 |
version |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
474
diff
changeset
|
218 |
^ '$Header: /cvs/stx/stx/libbasic2/FilteringLineStream.st,v 1.5 2000-03-02 14:15:07 cg Exp $' |
399 | 219 |
! ! |