3973
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
0
|
3 |
"
|
4
|
4 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
48
|
5 |
All Rights Reserved
|
0
|
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 |
"
|
3973
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
0
|
17 |
|
55
|
18 |
StringCollection subclass:#FileText
|
254
|
19 |
instanceVariableNames:'myStream lastLineKnown lastLineOfFile cachedLines cacheLineNr'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Text'
|
0
|
23 |
!
|
|
24 |
|
28
|
25 |
!FileText class methodsFor:'documentation'!
|
0
|
26 |
|
31
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
48
|
30 |
All Rights Reserved
|
31
|
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 |
|
28
|
41 |
documentation
|
|
42 |
"
|
263
|
43 |
FileText represents the contents of a text-file and allows
|
|
44 |
transparent access, via #at:, as if the lineStrings were in
|
|
45 |
memory - although, only a small portion of the file is actually
|
|
46 |
present in a cache.
|
|
47 |
|
|
48 |
Only the offsets of the text-lines are stored in an internal array
|
|
49 |
to save memory space. The #at: method fetches the line from the file.
|
|
50 |
Individual textlines may be replaced by strings (via #at:put:).
|
|
51 |
The underlying file is NOT updated in this case.
|
48
|
52 |
|
28
|
53 |
Care should be taken, if the underlying file is rewritten -
|
|
54 |
you have to manually update/flush the pointers.
|
|
55 |
Never rewrite the file using the data from a FileText.
|
263
|
56 |
If you keep a file's contents in a FileText object and want to
|
48
|
57 |
rewrite that file, you MUST write to a temporary file first.
|
|
58 |
Otherwise, you will clobber the contents.
|
|
59 |
|
263
|
60 |
This is an EXPERIMENTAL class, use at your own risk.
|
|
61 |
(If at all, use fileText for huge readonly texts only.)
|
254
|
62 |
|
|
63 |
[author:]
|
|
64 |
Claus Gittinger
|
28
|
65 |
"
|
|
66 |
! !
|
0
|
67 |
|
|
68 |
!FileText class methodsFor:'instance creation'!
|
|
69 |
|
|
70 |
of:aStream
|
|
71 |
"return a new FileText object for the stream aStream"
|
|
72 |
|
|
73 |
^ (self new:1) of:aStream
|
|
74 |
!
|
|
75 |
|
|
76 |
ofFile:aFileName
|
|
77 |
"return a new FileText object for the named file"
|
|
78 |
|
|
79 |
|aStream|
|
|
80 |
|
|
81 |
aStream := FileStream readonlyFileNamed:aFileName.
|
|
82 |
aStream isNil ifTrue:[^ nil].
|
|
83 |
^ (self new:1) of:aStream
|
|
84 |
! !
|
|
85 |
|
|
86 |
!FileText methodsFor:'accessing'!
|
|
87 |
|
|
88 |
at:index
|
266
|
89 |
"return the files line at index, as a string"
|
|
90 |
|
0
|
91 |
|entry oldPosition|
|
|
92 |
|
|
93 |
(index > lastLineKnown) ifTrue:[
|
266
|
94 |
self scanUpToLine:index.
|
|
95 |
(lastLineOfFile notNil) ifTrue:[
|
|
96 |
(index > lastLineOfFile) ifTrue:[
|
|
97 |
^ self subscriptBoundsError
|
|
98 |
]
|
|
99 |
]
|
0
|
100 |
].
|
|
101 |
|
|
102 |
entry := super at:index.
|
3973
|
103 |
entry isSingleByteString ifTrue:[^ entry].
|
0
|
104 |
|
|
105 |
cachedLines isNil ifTrue:[
|
266
|
106 |
cachedLines := Array new:50.
|
|
107 |
cacheLineNr := -9999
|
0
|
108 |
].
|
|
109 |
((index < cacheLineNr)
|
|
110 |
or:[index >= (cacheLineNr + cachedLines size)]) ifTrue:[
|
266
|
111 |
oldPosition := myStream position.
|
|
112 |
myStream position:entry.
|
|
113 |
1 to:(cachedLines size) do:[:cacheIndex|
|
|
114 |
cachedLines at:cacheIndex put:(myStream nextLine)
|
|
115 |
].
|
|
116 |
myStream position:oldPosition.
|
|
117 |
cacheLineNr := index
|
0
|
118 |
].
|
|
119 |
|
|
120 |
^ cachedLines at:(index - cacheLineNr + 1)
|
266
|
121 |
|
|
122 |
"Modified: 27.4.1996 / 13:32:29 / cg"
|
0
|
123 |
!
|
124
|
124 |
|
|
125 |
of:aStream
|
266
|
126 |
"setup the receiver for lines from aStream"
|
|
127 |
|
124
|
128 |
myStream := aStream.
|
|
129 |
lastLineOfFile := nil.
|
|
130 |
lastLineKnown := 0.
|
|
131 |
cachedLines := nil
|
266
|
132 |
|
|
133 |
"Modified: 27.4.1996 / 13:33:26 / cg"
|
124
|
134 |
!
|
|
135 |
|
0
|
136 |
size
|
|
137 |
"return the number of text-lines - have to scan file the first time"
|
|
138 |
|
|
139 |
(lastLineOfFile isNil) ifTrue:[
|
48
|
140 |
self scanUpToEnd
|
0
|
141 |
].
|
|
142 |
^ lastLineOfFile
|
|
143 |
! !
|
|
144 |
|
|
145 |
!FileText methodsFor:'enumerating'!
|
|
146 |
|
|
147 |
do:aBlock
|
266
|
148 |
"evaluate aBlock for all lines"
|
|
149 |
|
0
|
150 |
self from:1 to:(self size) do:aBlock
|
266
|
151 |
|
|
152 |
"Modified: 27.4.1996 / 13:33:51 / cg"
|
0
|
153 |
!
|
|
154 |
|
|
155 |
from:index1 to:index2 do:aBlock
|
266
|
156 |
"evaluate aBlock for all lines from index1 to index2.
|
|
157 |
Must be redefined back since elements are indices into file,
|
|
158 |
not the elements themselfes"
|
0
|
159 |
|
|
160 |
|index "{ Class: SmallInteger }"
|
|
161 |
stop "{ Class: SmallInteger }" |
|
|
162 |
|
|
163 |
index := index1.
|
|
164 |
stop := index2.
|
|
165 |
[index <= stop] whileTrue:[
|
266
|
166 |
aBlock value:(self at:index).
|
|
167 |
index := index + 1
|
0
|
168 |
]
|
|
169 |
|
266
|
170 |
"Modified: 27.4.1996 / 13:34:16 / cg"
|
0
|
171 |
! !
|
|
172 |
|
|
173 |
!FileText methodsFor:'private'!
|
|
174 |
|
|
175 |
scanUpToEnd
|
|
176 |
"scan myStream up to the end of file"
|
|
177 |
|
|
178 |
(lastLineOfFile notNil) ifTrue:[^ self].
|
|
179 |
[true] whileTrue:[
|
48
|
180 |
lastLineKnown := lastLineKnown + 1.
|
|
181 |
(super size < lastLineKnown) ifTrue:[
|
|
182 |
super grow:(super size * 2 + 1)
|
|
183 |
].
|
|
184 |
super at:lastLineKnown put:(myStream position).
|
|
185 |
myStream skipLine isNil ifTrue:[
|
|
186 |
lastLineOfFile := lastLineKnown.
|
|
187 |
^ self
|
|
188 |
]
|
0
|
189 |
]
|
|
190 |
!
|
|
191 |
|
|
192 |
scanUpToLine:index
|
|
193 |
"scan myStream up to line index and save line-start-positions"
|
|
194 |
|
|
195 |
(lastLineOfFile notNil) ifTrue:[
|
48
|
196 |
(index > lastLineOfFile) ifTrue:[^ self]
|
0
|
197 |
].
|
|
198 |
[lastLineKnown <= index] whileTrue:[
|
48
|
199 |
lastLineKnown := lastLineKnown + 1.
|
|
200 |
(super size < lastLineKnown) ifTrue:[
|
|
201 |
super grow:(super size * 2 + 1)
|
|
202 |
].
|
|
203 |
super at:lastLineKnown put:(myStream position).
|
|
204 |
myStream skipLine isNil ifTrue:[
|
|
205 |
lastLineOfFile := lastLineKnown.
|
|
206 |
^ self
|
|
207 |
]
|
0
|
208 |
]
|
|
209 |
! !
|
124
|
210 |
|
131
|
211 |
!FileText class methodsFor:'documentation'!
|
|
212 |
|
|
213 |
version
|
3973
|
214 |
^ '$Header$'
|
131
|
215 |
! !
|
3973
|
216 |
|