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