author | Claus Gittinger <cg@exept.de> |
Thu, 17 Feb 2011 17:34:13 +0100 | |
changeset 2538 | 8e34a8db4002 |
parent 2376 | 267edc0847fe |
child 2827 | dd4e71ae9244 |
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 |
||
119 |
!LineNumberReadStream methodsFor:'initialization'! |
|
120 |
||
121 |
initialize |
|
122 |
super initialize. |
|
123 |
||
124 |
lineNumber := 1. |
|
1291 | 125 |
(inputStream isPositionable) ifTrue:[ |
2376 | 126 |
lineStartPosition := inputStream position. |
1283 | 127 |
]. |
478 | 128 |
filter := [:char | |
129 |
||
2376 | 130 |
char == Character cr ifTrue:[ |
131 |
lineNumber := lineNumber + 1. |
|
132 |
inputStream isPositionable ifTrue:[ |
|
133 |
lineStartPosition := inputStream position. |
|
134 |
]. |
|
135 |
]. |
|
136 |
char |
|
137 |
]. |
|
478 | 138 |
|
139 |
"Modified: 11.1.1997 / 16:58:42 / cg" |
|
140 |
! ! |
|
141 |
||
2084 | 142 |
!LineNumberReadStream methodsFor:'queries'! |
143 |
||
144 |
atEnd |
|
145 |
"return true, if the receiver stream is at the end" |
|
146 |
||
147 |
readAhead notNil ifTrue:[^ false]. |
|
148 |
^ inputStream atEnd |
|
149 |
! |
|
150 |
||
151 |
peekOrNil |
|
152 |
"peek ahead for the next character, or return nil" |
|
153 |
||
154 |
readAhead notNil ifTrue:[ |
|
2376 | 155 |
^ readAhead |
2084 | 156 |
]. |
157 |
^ inputStream peekOrNil |
|
158 |
! |
|
159 |
||
160 |
position:aPosition |
|
161 |
inputStream position:aPosition |
|
162 |
! ! |
|
163 |
||
478 | 164 |
!LineNumberReadStream class methodsFor:'documentation'! |
165 |
||
166 |
version |
|
2538 | 167 |
^ '$Header: /cvs/stx/stx/libbasic2/LineNumberReadStream.st,v 1.8 2011-02-17 16:34:13 cg Exp $' |
2376 | 168 |
! |
169 |
||
170 |
version_CVS |
|
2538 | 171 |
^ '$Header: /cvs/stx/stx/libbasic2/LineNumberReadStream.st,v 1.8 2011-02-17 16:34:13 cg Exp $' |
478 | 172 |
! ! |