author | Stefan Vogel <sv@exept.de> |
Fri, 17 Feb 2017 11:08:34 +0100 | |
changeset 4334 | 95bb043e3339 |
parent 4201 | e29dfac90a57 |
child 4455 | 54e6571c7f8a |
permissions | -rw-r--r-- |
478 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by eXept Software AG |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
3 |
All Rights Reserved |
478 | 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 |
" |
|
2376 | 12 |
"{ Package: 'stx:libbasic2' }" |
1239 | 13 |
|
3540 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
478 | 16 |
FilteringStream subclass:#LineNumberReadStream |
1283 | 17 |
instanceVariableNames:'lineNumber lineStartPosition' |
478 | 18 |
classVariableNames:'' |
19 |
poolDictionaries:'' |
|
20 |
category:'Streams-Misc' |
|
21 |
! |
|
22 |
||
23 |
!LineNumberReadStream class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1996 by eXept Software AG |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
28 |
All Rights Reserved |
478 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
||
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
1283 | 43 |
This filter keeps track of the current line, and optionally |
2538 | 44 |
the current line's start position (if the input stream is positionable). |
1283 | 45 |
while passing text from its inputStream to its outputStream. |
478 | 46 |
|
2538 | 47 |
Can be placed in-between text processing and the text's |
478 | 48 |
input stream, and let it keep track of the lineNumber. |
49 |
||
50 |
[see also:] |
|
2538 | 51 |
ReadStream WriteStream ExternalStream FileStream |
52 |
FilteringStream FilteringLineStream |
|
478 | 53 |
|
54 |
[author:] |
|
2538 | 55 |
Claus Gittinger |
478 | 56 |
" |
57 |
! |
|
58 |
||
59 |
examples |
|
60 |
" |
|
61 |
count lines in a Makefile: |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
62 |
[exBegin] |
478 | 63 |
|in filter| |
64 |
||
65 |
in := 'Makefile' asFilename readStream. |
|
66 |
||
67 |
filter := LineNumberReadStream readingFrom:in. |
|
68 |
||
69 |
filter upToEnd. |
|
70 |
filter close. |
|
71 |
||
72 |
Transcript showCR:('Makefile has %1 lines' bindWith:(filter lineNumber - 1)). |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
73 |
[exEnd] |
478 | 74 |
|
75 |
||
76 |
||
77 |
find a specific string and output its lineNr: |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
78 |
[exBegin] |
478 | 79 |
|in filter| |
80 |
||
81 |
in := 'Makefile' asFilename readStream. |
|
82 |
||
83 |
filter := LineNumberReadStream readingFrom:in. |
|
84 |
||
85 |
filter skipThroughAll:'start of'. |
|
86 |
filter close. |
|
87 |
||
88 |
Transcript showCR:('found in line %1' bindWith:(filter lineNumber)). |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
89 |
[exEnd] |
478 | 90 |
" |
91 |
||
92 |
||
93 |
! ! |
|
94 |
||
95 |
!LineNumberReadStream methodsFor:'accessing'! |
|
96 |
||
97 |
lineNumber |
|
98 |
"return the current lineNumber" |
|
99 |
||
100 |
^ lineNumber |
|
101 |
||
102 |
"Created: 11.1.1997 / 16:57:10 / cg" |
|
103 |
"Modified: 11.1.1997 / 16:57:19 / cg" |
|
104 |
! |
|
105 |
||
106 |
lineNumber:something |
|
107 |
"set the current lineNumber" |
|
108 |
||
109 |
lineNumber := something. |
|
110 |
||
111 |
"Created: 11.1.1997 / 16:57:10 / cg" |
|
112 |
"Modified: 11.1.1997 / 16:57:22 / cg" |
|
1283 | 113 |
! |
114 |
||
115 |
lineStartPosition |
|
116 |
"return the current lines start position" |
|
117 |
||
118 |
^ lineStartPosition |
|
478 | 119 |
! ! |
120 |
||
3180 | 121 |
!LineNumberReadStream methodsFor:'converting'! |
122 |
||
123 |
asLineNumberReadStream |
|
124 |
^ self |
|
125 |
! ! |
|
126 |
||
478 | 127 |
!LineNumberReadStream methodsFor:'initialization'! |
128 |
||
129 |
initialize |
|
130 |
super initialize. |
|
131 |
||
132 |
lineNumber := 1. |
|
1291 | 133 |
(inputStream isPositionable) ifTrue:[ |
3329 | 134 |
lineStartPosition := inputStream position. |
1283 | 135 |
]. |
478 | 136 |
filter := [:char | |
3329 | 137 |
char == Character cr ifTrue:[ |
138 |
lineNumber := lineNumber + 1. |
|
139 |
lineStartPosition notNil ifTrue:[ |
|
140 |
lineStartPosition := inputStream position. |
|
141 |
]. |
|
142 |
]. |
|
143 |
char |
|
144 |
]. |
|
478 | 145 |
|
146 |
"Modified: 11.1.1997 / 16:58:42 / cg" |
|
147 |
! ! |
|
148 |
||
3329 | 149 |
!LineNumberReadStream methodsFor:'positioning'! |
150 |
||
151 |
position:aPosition |
|
152 |
"this method fails if inputStream is not positionable" |
|
153 |
||
154 |
readAhead := nil. |
|
155 |
inputStream position:aPosition. |
|
156 |
(lineStartPosition notNil and:[aPosition < lineStartPosition]) ifTrue:[ |
|
157 |
"we detect backPosition over a single line, but not over multiple lines and |
|
158 |
no forward positioning!!" |
|
159 |
lineNumber := lineNumber - 1. |
|
160 |
lineStartPosition := aPosition. |
|
161 |
]. |
|
162 |
! |
|
163 |
||
164 |
skip:numberToSkip |
|
165 |
"this method fails if inputStream is not positionable" |
|
166 |
||
167 |
readAhead := nil. |
|
168 |
inputStream skip:numberToSkip. |
|
169 |
(lineStartPosition notNil and:[numberToSkip < lineStartPosition negated]) ifTrue:[ |
|
170 |
"we detect backPosition over a single line, but not over multiple lines and |
|
171 |
no forward positioning!!" |
|
172 |
lineNumber := lineNumber - 1. |
|
173 |
lineStartPosition := lineStartPosition + numberToSkip. |
|
174 |
]. |
|
175 |
! ! |
|
176 |
||
2084 | 177 |
!LineNumberReadStream methodsFor:'queries'! |
178 |
||
179 |
atEnd |
|
180 |
"return true, if the receiver stream is at the end" |
|
181 |
||
182 |
readAhead notNil ifTrue:[^ false]. |
|
183 |
^ inputStream atEnd |
|
184 |
! |
|
185 |
||
3180 | 186 |
isLineNumberReadStream |
187 |
^ true |
|
3329 | 188 |
! ! |
189 |
||
190 |
!LineNumberReadStream methodsFor:'reading'! |
|
191 |
||
4201 | 192 |
contents |
193 |
^ inputStream contents |
|
194 |
! |
|
195 |
||
196 |
contentsAsString |
|
197 |
^ inputStream contentsAsString |
|
198 |
! |
|
199 |
||
3329 | 200 |
upToAll_positionBefore:aCollection |
3540 | 201 |
"read until a subcollection consisting of the elements in aCollection is encountered. |
3329 | 202 |
Return everything read excluding the elements in aCollection. |
203 |
The position is left before the collection; i.e. the next |
|
204 |
read operations will return those elements. |
|
205 |
If no such subcollection is encountered, all elements up to the end |
|
206 |
are read and returned. |
|
207 |
See also #throughAll: which also reads up to some objects |
|
208 |
but positions behind it and DOES include it in the returned |
|
209 |
collection. |
|
3180 | 210 |
|
3329 | 211 |
This is a copy from PositionableStream>>#upToAll_positionBefore:. |
212 |
It fails if inputStream is not positionable" |
|
213 |
||
214 |
|answerStream element last| |
|
2084 | 215 |
|
3329 | 216 |
last := aCollection last. |
217 |
answerStream := WriteStream on:(self contentsSpecies new:100). |
|
218 |
[(element := self nextOrNil) notNil] whileTrue:[ |
|
219 |
answerStream nextPut:element. |
|
220 |
(element = last and:[answerStream endsBeforePositionWith:aCollection]) ifTrue:[ |
|
221 |
|backStep| |
|
222 |
backStep := aCollection size negated. |
|
223 |
self skip:backStep. |
|
224 |
answerStream skip:backStep. |
|
225 |
^ answerStream contents |
|
226 |
]. |
|
2084 | 227 |
]. |
3329 | 228 |
^ answerStream contents |
2084 | 229 |
! ! |
230 |
||
478 | 231 |
!LineNumberReadStream class methodsFor:'documentation'! |
232 |
||
233 |
version |
|
4201 | 234 |
^ '$Header$' |
2376 | 235 |
! |
236 |
||
237 |
version_CVS |
|
4201 | 238 |
^ '$Header$' |
478 | 239 |
! ! |
3180 | 240 |