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