author | Claus Gittinger <cg@exept.de> |
Thu, 20 Feb 2014 08:03:30 +0100 | |
changeset 3180 | 7c7ce3f9483e |
parent 2827 | dd4e71ae9244 |
child 3329 | 170f49ba6c9a |
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 |
|
478 | 14 |
FilteringStream subclass:#LineNumberReadStream |
1283 | 15 |
instanceVariableNames:'lineNumber lineStartPosition' |
478 | 16 |
classVariableNames:'' |
17 |
poolDictionaries:'' |
|
18 |
category:'Streams-Misc' |
|
19 |
! |
|
20 |
||
21 |
!LineNumberReadStream class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1996 by eXept Software AG |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
26 |
All Rights Reserved |
478 | 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 |
" |
|
1283 | 41 |
This filter keeps track of the current line, and optionally |
2538 | 42 |
the current line's start position (if the input stream is positionable). |
1283 | 43 |
while passing text from its inputStream to its outputStream. |
478 | 44 |
|
2538 | 45 |
Can be placed in-between text processing and the text's |
478 | 46 |
input stream, and let it keep track of the lineNumber. |
47 |
||
48 |
[see also:] |
|
2538 | 49 |
ReadStream WriteStream ExternalStream FileStream |
50 |
FilteringStream FilteringLineStream |
|
478 | 51 |
|
52 |
[author:] |
|
2538 | 53 |
Claus Gittinger |
478 | 54 |
" |
55 |
! |
|
56 |
||
57 |
examples |
|
58 |
" |
|
59 |
count lines in a Makefile: |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
60 |
[exBegin] |
478 | 61 |
|in filter| |
62 |
||
63 |
in := 'Makefile' asFilename readStream. |
|
64 |
||
65 |
filter := LineNumberReadStream readingFrom:in. |
|
66 |
||
67 |
filter upToEnd. |
|
68 |
filter close. |
|
69 |
||
70 |
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
|
71 |
[exEnd] |
478 | 72 |
|
73 |
||
74 |
||
75 |
find a specific string and output its lineNr: |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
76 |
[exBegin] |
478 | 77 |
|in filter| |
78 |
||
79 |
in := 'Makefile' asFilename readStream. |
|
80 |
||
81 |
filter := LineNumberReadStream readingFrom:in. |
|
82 |
||
83 |
filter skipThroughAll:'start of'. |
|
84 |
filter close. |
|
85 |
||
86 |
Transcript showCR:('found in line %1' bindWith:(filter lineNumber)). |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
87 |
[exEnd] |
478 | 88 |
" |
89 |
||
90 |
||
91 |
! ! |
|
92 |
||
93 |
!LineNumberReadStream methodsFor:'accessing'! |
|
94 |
||
95 |
lineNumber |
|
96 |
"return the current lineNumber" |
|
97 |
||
98 |
^ lineNumber |
|
99 |
||
100 |
"Created: 11.1.1997 / 16:57:10 / cg" |
|
101 |
"Modified: 11.1.1997 / 16:57:19 / cg" |
|
102 |
! |
|
103 |
||
104 |
lineNumber:something |
|
105 |
"set the current lineNumber" |
|
106 |
||
107 |
lineNumber := something. |
|
108 |
||
109 |
"Created: 11.1.1997 / 16:57:10 / cg" |
|
110 |
"Modified: 11.1.1997 / 16:57:22 / cg" |
|
1283 | 111 |
! |
112 |
||
113 |
lineStartPosition |
|
114 |
"return the current lines start position" |
|
115 |
||
116 |
^ lineStartPosition |
|
478 | 117 |
! ! |
118 |
||
3180 | 119 |
!LineNumberReadStream methodsFor:'converting'! |
120 |
||
121 |
asLineNumberReadStream |
|
122 |
^ self |
|
123 |
! ! |
|
124 |
||
478 | 125 |
!LineNumberReadStream methodsFor:'initialization'! |
126 |
||
127 |
initialize |
|
128 |
super initialize. |
|
129 |
||
130 |
lineNumber := 1. |
|
1291 | 131 |
(inputStream isPositionable) ifTrue:[ |
2376 | 132 |
lineStartPosition := inputStream position. |
1283 | 133 |
]. |
478 | 134 |
filter := [:char | |
135 |
||
2376 | 136 |
char == Character cr ifTrue:[ |
137 |
lineNumber := lineNumber + 1. |
|
138 |
inputStream isPositionable ifTrue:[ |
|
139 |
lineStartPosition := inputStream position. |
|
140 |
]. |
|
141 |
]. |
|
142 |
char |
|
143 |
]. |
|
478 | 144 |
|
145 |
"Modified: 11.1.1997 / 16:58:42 / cg" |
|
146 |
! ! |
|
147 |
||
2084 | 148 |
!LineNumberReadStream methodsFor:'queries'! |
149 |
||
150 |
atEnd |
|
151 |
"return true, if the receiver stream is at the end" |
|
152 |
||
153 |
readAhead notNil ifTrue:[^ false]. |
|
154 |
^ inputStream atEnd |
|
155 |
! |
|
156 |
||
3180 | 157 |
isLineNumberReadStream |
158 |
^ true |
|
159 |
! |
|
160 |
||
2084 | 161 |
peekOrNil |
162 |
"peek ahead for the next character, or return nil" |
|
163 |
||
164 |
readAhead notNil ifTrue:[ |
|
2376 | 165 |
^ readAhead |
2084 | 166 |
]. |
167 |
^ inputStream peekOrNil |
|
168 |
! |
|
169 |
||
170 |
position:aPosition |
|
2827 | 171 |
readAhead := nil. |
2084 | 172 |
inputStream position:aPosition |
2827 | 173 |
! |
174 |
||
175 |
skip:numberToSkip |
|
176 |
readAhead := nil. |
|
177 |
inputStream skip:numberToSkip |
|
2084 | 178 |
! ! |
179 |
||
478 | 180 |
!LineNumberReadStream class methodsFor:'documentation'! |
181 |
||
182 |
version |
|
3180 | 183 |
^ '$Header: /cvs/stx/stx/libbasic2/LineNumberReadStream.st,v 1.10 2014-02-20 07:03:30 cg Exp $' |
2376 | 184 |
! |
185 |
||
186 |
version_CVS |
|
3180 | 187 |
^ '$Header: /cvs/stx/stx/libbasic2/LineNumberReadStream.st,v 1.10 2014-02-20 07:03:30 cg Exp $' |
478 | 188 |
! ! |
3180 | 189 |