author | Claus Gittinger <cg@exept.de> |
Fri, 15 Apr 2016 13:47:09 +0200 | |
changeset 19591 | 185835d2d0e5 |
parent 19403 | 5accf347c2c1 |
child 19610 | a9a6940944a9 |
child 19885 | 7a8a85b3fa8d |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 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 |
" |
|
5393
7e2230bd4bad
moved some stuff to superclass
Claus Gittinger <cg@exept.de>
parents:
5308
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
7e2230bd4bad
moved some stuff to superclass
Claus Gittinger <cg@exept.de>
parents:
5308
diff
changeset
|
13 |
|
17559 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
57 | 16 |
PeekableStream subclass:#PositionableStream |
973 | 17 |
instanceVariableNames:'collection position readLimit writeLimit' |
8443
7bc4348c059e
fileIn stuff moved to peekableStream
Claus Gittinger <cg@exept.de>
parents:
8363
diff
changeset
|
18 |
classVariableNames:'ZeroPosition InvalidPositionErrorSignal' |
973 | 19 |
poolDictionaries:'' |
20 |
category:'Streams' |
|
1 | 21 |
! |
22 |
||
1897 | 23 |
!PositionableStream class methodsFor:'documentation'! |
88 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 28 |
All Rights Reserved |
1 | 29 |
|
88 | 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 |
! |
|
10 | 38 |
|
88 | 39 |
documentation |
40 |
" |
|
41 |
Instances of PositionableStream allow positioning the read pointer. |
|
42 |
The PositionableStream class also adds methods for source-chunk reading |
|
43 |
and writing, and for filing-in/out of source code. |
|
3249
8401c3e42165
checkin to force re-checkout.
Claus Gittinger <cg@exept.de>
parents:
3184
diff
changeset
|
44 |
|
88 | 45 |
This is an abstract class. |
1295 | 46 |
|
4405 | 47 |
Compatibility Notice: |
9434 | 48 |
In previous versions of ST/X, streams started with a 1-position (i.e. as in collections), |
49 |
while ST-80 has always been using 0-based postions for streams and 1-based positions for collections. |
|
4405 | 50 |
|
17559 | 51 |
THIS CERTAINLY IS BAD. |
52 |
||
53 |
Although this is confusing ST/X has been changed to now also uses 0-based stream positioning. |
|
9434 | 54 |
|
17559 | 55 |
[caveat:] |
56 |
Basing capabilities like readability/writability/positionability/peekability on inheritance makes |
|
57 |
the class hierarchy ugly and leads to strange and hard to teach redefinitions (aka. NonPositionableStream |
|
58 |
below PositionableStream or ExternalReadStream under WriteStream) |
|
4405 | 59 |
|
1295 | 60 |
[author:] |
9434 | 61 |
Claus Gittinger |
88 | 62 |
" |
63 |
! ! |
|
1 | 64 |
|
1897 | 65 |
!PositionableStream class methodsFor:'initialization'! |
1 | 66 |
|
10 | 67 |
initialize |
15353 | 68 |
ZeroPosition := 0. "/ changed with stx rel5.1; |
69 |
"/ rel6.2: no longer used (i.e. 0 has now ben hardcoded) |
|
70 |
"/ class variable to be eliminated. |
|
7363 | 71 |
|
8443
7bc4348c059e
fileIn stuff moved to peekableStream
Claus Gittinger <cg@exept.de>
parents:
8363
diff
changeset
|
72 |
InvalidPositionErrorSignal isNil ifTrue:[ |
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
73 |
InvalidPositionErrorSignal := PositionOutOfBoundsError. |
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
74 |
InvalidPositionErrorSignal notifierString:'position out of bounds: '. |
10 | 75 |
] |
76 |
! ! |
|
1 | 77 |
|
1897 | 78 |
!PositionableStream class methodsFor:'instance creation'! |
1 | 79 |
|
80 |
on:aCollection |
|
81 |
"return a new PositionableStream streaming on aCollection" |
|
82 |
||
83 |
^ (self basicNew) on:aCollection |
|
84 |
! |
|
85 |
||
86 |
on:aCollection from:first to:last |
|
87 |
"return a new PositionableStream streaming on aCollection |
|
88 |
from first to last" |
|
89 |
||
369 | 90 |
^ (self basicNew) on:aCollection from:first to:last |
57 | 91 |
! |
92 |
||
93 |
with:aCollection |
|
94 |
"return a new PositionableStream streaming on aCollection, |
|
95 |
the stream is positioned to the end of the collection." |
|
96 |
||
362 | 97 |
^ (self basicNew) with:aCollection |
1 | 98 |
! ! |
99 |
||
3032
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
100 |
!PositionableStream class methodsFor:'Signal constants'! |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
101 |
|
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
102 |
invalidPositionErrorSignal |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
103 |
"return the signal raised if positioning is attempted to an |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
104 |
invalid position (i.e. before the begin of the stream or after |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
105 |
the end)" |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
106 |
|
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
107 |
^ PositionOutOfBoundsError |
3032
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
108 |
! ! |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
109 |
|
1897 | 110 |
!PositionableStream class methodsFor:'constants'! |
1 | 111 |
|
4405 | 112 |
zeroPosition |
113 |
"return the number, which marks the initial position. |
|
114 |
Compatibility Notice: |
|
9434 | 115 |
In previous versions of ST/X, streams started with a 1-position (i.e. as in collections), |
116 |
while ST-80 has always been using 0-based postions for streams and 1-based positions for collections. |
|
4405 | 117 |
|
9434 | 118 |
Although this is confusing ST/X has been changed to now also use 0-based stream positioning. |
119 |
" |
|
4405 | 120 |
|
15353 | 121 |
^ 0 |
9434 | 122 |
|
123 |
"Modified: / 13-07-2006 / 20:36:54 / cg" |
|
360 | 124 |
! ! |
125 |
||
7261 | 126 |
!PositionableStream methodsFor:'Compatibility-Dolphin'! |
6413 | 127 |
|
128 |
endChunk |
|
129 |
self nextPutChunkSeparator |
|
130 |
! ! |
|
131 |
||
9284 | 132 |
!PositionableStream methodsFor:'Compatibility-ST/V'! |
133 |
||
134 |
skipTo:anElement |
|
135 |
"ST/V compatibility: |
|
136 |
skip for the element given by the argument, anElement; |
|
137 |
return nil if not found, self otherwise. |
|
138 |
On a successful match, the next read will return the element after anElement." |
|
139 |
||
140 |
^ self skipThrough:anElement |
|
141 |
||
142 |
" |
|
143 |
|s| |
|
144 |
s := ReadStream on:'12345678901234567890'. |
|
145 |
s skipTo:$5. |
|
146 |
s copyFrom:1 to:(s position). |
|
147 |
s upToEnd |
|
148 |
" |
|
149 |
! ! |
|
150 |
||
16876
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
151 |
!PositionableStream methodsFor:'Compatibility-Squeak'! |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
152 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
153 |
back |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
154 |
"Go back one element and return it." |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
155 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
156 |
self position = 0 ifTrue: [self positionError]. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
157 |
self skip: -1. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
158 |
^ self peek |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
159 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
160 |
"Created: / 03-10-2014 / 03:06:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
161 |
! |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
162 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
163 |
peekBack |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
164 |
"Return the element at the previous position, without changing position. Use indirect messages in case self is a StandardFileStream." |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
165 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
166 |
| element | |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
167 |
element := self back. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
168 |
self skip: 1. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
169 |
^ element |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
170 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
171 |
"Created: / 03-10-2014 / 03:06:26 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
172 |
! ! |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
173 |
|
1 | 174 |
!PositionableStream methodsFor:'accessing'! |
175 |
||
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
176 |
collection |
19591 | 177 |
"return the underlying collection buffer. |
178 |
Notice, that this buffer may become invalid after being retrieved, |
|
179 |
if more data is written to the stream (because a bigger buffer might be |
|
180 |
allocated). Therefore, it should be only used in special situations, |
|
181 |
where an already filed buffer needs to be backpatched later |
|
182 |
(eg. before being sent out to some external stream or socket)" |
|
183 |
||
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
184 |
^ collection |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
185 |
! |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
186 |
|
1 | 187 |
contents |
188 |
"return the entire contents of the stream" |
|
189 |
||
190 |
^ collection |
|
191 |
! |
|
192 |
||
193 |
peek |
|
194 |
"look ahead for and return the next element" |
|
195 |
||
196 |
|peekObject| |
|
197 |
||
198 |
peekObject := self next. |
|
10 | 199 |
self backStep. |
1 | 200 |
^ peekObject |
201 |
! |
|
202 |
||
2432 | 203 |
peekForAll:aCollection |
204 |
"return true and advance if the next elements are the same |
|
205 |
as aCollection. |
|
206 |
otherwise stay and let the position unchanged" |
|
207 |
||
208 |
|oldPos| |
|
209 |
||
210 |
oldPos := self position. |
|
211 |
(self next:(aCollection size)) = aCollection ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6924
diff
changeset
|
212 |
^ true |
2432 | 213 |
]. |
214 |
self position:oldPos. |
|
215 |
^ false |
|
216 |
||
217 |
"Created: 1.3.1997 / 15:11:25 / cg" |
|
218 |
! |
|
219 |
||
3905
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
220 |
readLimit |
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
221 |
"return the read-limit; thats the position at which EOF is returned" |
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
222 |
|
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
223 |
^ readLimit |
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
224 |
|
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
225 |
"Created: / 30.10.1998 / 16:47:04 / cg" |
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
226 |
! |
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
227 |
|
1 | 228 |
readLimit:aNumber |
369 | 229 |
"set the read-limit; thats the position at which EOF is returned" |
1 | 230 |
|
231 |
readLimit := aNumber |
|
369 | 232 |
! |
233 |
||
14934 | 234 |
setCollection:newCollection |
235 |
collection := newCollection |
|
236 |
! |
|
237 |
||
369 | 238 |
writeLimit:aNumber |
239 |
"set the writeLimit; thats the position after which writing is prohibited" |
|
240 |
||
241 |
writeLimit := aNumber |
|
10592 | 242 |
|
243 |
" |
|
244 |
|s| |
|
245 |
s := WriteStream on:String new. |
|
246 |
s nextPutAll:'hello world'. |
|
247 |
s contents. |
|
248 |
||
249 |
|s| |
|
250 |
s := WriteStream on:String new. |
|
251 |
s writeLimit:5. |
|
252 |
s nextPutAll:'hello world'. |
|
253 |
s contents. |
|
254 |
||
255 |
" |
|
256 |
||
257 |
"Modified: / 04-06-2007 / 17:21:55 / cg" |
|
1 | 258 |
! ! |
259 |
||
17611 | 260 |
!PositionableStream methodsFor:'misc functions'! |
261 |
||
19403 | 262 |
copyToEndInto:aWriteStream |
17611 | 263 |
"read from the receiver, and write all data up to the end to another stream. |
264 |
Return the number of bytes which have been transferred. |
|
19403 | 265 |
Redefined here to avoid intermediate buffers/garbage." |
17611 | 266 |
|
267 |
|cnt| |
|
268 |
||
269 |
collection notNil ifTrue:[ |
|
270 |
aWriteStream nextPutAll:collection startingAt:position+1 to:readLimit. |
|
271 |
cnt := readLimit - position. |
|
272 |
position := readLimit. |
|
273 |
^ cnt. |
|
274 |
]. |
|
19403 | 275 |
^ super copyToEndInto:aWriteStream. |
276 |
||
277 |
" |
|
278 |
|rs ws cnt| |
|
279 |
||
280 |
ws := #() writeStream. |
|
281 |
rs := #( 1 2 3 4 a nil true) readWriteStream. |
|
282 |
rs next. |
|
283 |
cnt := rs copyToEndInto:ws bufferSize:0. |
|
284 |
Transcript show:cnt; show:' '; showCR:ws contents. |
|
285 |
" |
|
286 |
! |
|
287 |
||
288 |
copyToEndInto:aWriteStream bufferSize:bufferSize |
|
289 |
"read from the receiver, and write all data up to the end to another stream. |
|
290 |
Return the number of bytes which have been transferred. |
|
291 |
Redefined here to avoid intermediate buffers/garbage. |
|
292 |
bufferSize does not matter here." |
|
293 |
||
294 |
collection notNil ifTrue:[ |
|
295 |
^ self copyToEndInto:aWriteStream. |
|
296 |
]. |
|
17611 | 297 |
^ super copyToEndInto:aWriteStream bufferSize:bufferSize. |
298 |
||
299 |
" |
|
300 |
|rs ws cnt| |
|
301 |
||
302 |
ws := #() writeStream. |
|
303 |
rs := #( 1 2 3 4 a nil true) readWriteStream. |
|
304 |
rs next. |
|
305 |
cnt := rs copyToEndInto:ws bufferSize:0. |
|
306 |
Transcript show:cnt; show:' '; showCR:ws contents. |
|
307 |
" |
|
308 |
! ! |
|
309 |
||
611 | 310 |
!PositionableStream methodsFor:'positioning'! |
311 |
||
312 |
backStep |
|
313 |
"move backward read position by one" |
|
314 |
||
4405 | 315 |
self position:(self position - 1) |
611 | 316 |
! |
317 |
||
16188 | 318 |
match: subCollection |
319 |
"Set the access position of the receiver to be past the next occurrence of the subCollection. |
|
320 |
Answer whether subCollection is found. |
|
321 |
No wildcards, and case does matter." |
|
322 |
||
323 |
| pattern startMatch | |
|
324 |
||
325 |
pattern := ReadStream on: subCollection. |
|
326 |
startMatch := nil. |
|
327 |
[pattern atEnd] whileFalse: [ |
|
328 |
self atEnd ifTrue: [^ false]. |
|
329 |
(self next) = (pattern next) |
|
330 |
ifTrue: [pattern position = 1 ifTrue: [startMatch := self position]] |
|
331 |
ifFalse: [ |
|
332 |
pattern position: 0. |
|
333 |
startMatch ifNotNil: [ |
|
334 |
self position: startMatch. |
|
335 |
startMatch := nil |
|
336 |
] |
|
337 |
] |
|
338 |
]. |
|
339 |
^ true |
|
340 |
||
341 |
" |
|
342 |
'abc def ghi' readStream match:'def'; upToEnd |
|
343 |
" |
|
344 |
! |
|
345 |
||
611 | 346 |
position |
15615 | 347 |
"return the read position (0-based)" |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
348 |
|
15348 | 349 |
^ position |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
350 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
351 |
|
15615 | 352 |
position0Based |
353 |
<resource: #obsolete> |
|
354 |
"return the read position 0-based" |
|
355 |
||
356 |
^ self position |
|
357 |
! |
|
358 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
359 |
position0Based:index0Based |
15615 | 360 |
<resource: #obsolete> |
4405 | 361 |
"set the read (or write) position" |
611 | 362 |
|
15615 | 363 |
self position:index0Based |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
364 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
365 |
|
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
366 |
position1Based |
15615 | 367 |
<resource: #obsolete> |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
368 |
"return the read position 1-based" |
611 | 369 |
|
15615 | 370 |
^ self position + 1 |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
371 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
372 |
|
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
373 |
position1Based:index1Based |
15615 | 374 |
<resource: #obsolete> |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
375 |
"set the read (or write) position" |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
376 |
|
15615 | 377 |
self position:(index1Based - 1) |
4405 | 378 |
|
379 |
" |
|
380 |
|s| |
|
381 |
||
382 |
s := '1234567890' readStream. |
|
383 |
s next:5. |
|
384 |
s position:1. |
|
385 |
s next:7. |
|
386 |
" |
|
387 |
||
388 |
" |
|
389 |
|s| |
|
390 |
||
391 |
s := '' writeStream. |
|
392 |
s nextPutAll:'1234567890'. |
|
393 |
s position:5. |
|
394 |
s nextPutAll:'abcdefg'. |
|
395 |
s contents |
|
396 |
" |
|
397 |
||
398 |
" |
|
399 |
|s| |
|
400 |
||
401 |
s := '' writeStream. |
|
402 |
s nextPutAll:'1234567890'. |
|
403 |
s position:0. |
|
404 |
s nextPutAll:'abcdefg'. |
|
405 |
s contents |
|
406 |
" |
|
611 | 407 |
! |
408 |
||
15615 | 409 |
position:index0Based |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
410 |
"set the read (or write) position" |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
411 |
|
15615 | 412 |
((index0Based > readLimit) or:[index0Based < 0]) ifTrue: [^ self positionError:index0Based]. |
413 |
position := index0Based |
|
6626
f7b08d6bee3f
#positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents:
6538
diff
changeset
|
414 |
! |
f7b08d6bee3f
#positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents:
6538
diff
changeset
|
415 |
|
611 | 416 |
reset |
417 |
"set the read position to the beginning of the collection" |
|
418 |
||
5308 | 419 |
self resetPosition |
420 |
||
421 |
" |
|
422 |
|s| |
|
423 |
||
424 |
s := 'hello world' readStream. |
|
425 |
Transcript showCR:(s next:5). |
|
426 |
s reset. |
|
427 |
Transcript showCR:(s next:10). |
|
428 |
" |
|
429 |
! |
|
430 |
||
431 |
resetPosition |
|
432 |
"set the read position to the beginning of the collection" |
|
433 |
||
15348 | 434 |
position := 0 |
4405 | 435 |
|
436 |
" |
|
437 |
|s| |
|
438 |
||
439 |
s := 'hello world' readStream. |
|
440 |
Transcript showCR:(s next:5). |
|
441 |
s reset. |
|
442 |
Transcript showCR:(s next:10). |
|
443 |
" |
|
611 | 444 |
! |
445 |
||
446 |
setToEnd |
|
11433 | 447 |
"set the read position to the end of the collection. |
448 |
#next will return EOF, #nextPut: will append to the stream. |
|
449 |
(same Behavior as FileStream." |
|
611 | 450 |
|
15348 | 451 |
position := readLimit |
611 | 452 |
! |
453 |
||
454 |
skip:numberToSkip |
|
455 |
"skip the next numberToSkip elements" |
|
456 |
||
5774 | 457 |
numberToSkip ~~ 0 ifTrue:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6924
diff
changeset
|
458 |
self position:(self position + numberToSkip) |
5774 | 459 |
] |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
460 |
! |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
461 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
462 |
skipThroughAll:aCollection |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
463 |
"skip for and through the sequence given by the argument, aCollection; |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
464 |
return nil if not found, self otherwise. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
465 |
On a successful match, the next read will return elements after aCollection; |
2153
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
466 |
if no match was found, the receiver will be positioned at the end. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
467 |
This is redefined here, to make use of positioning." |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
468 |
|
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
469 |
|buffer len first idx| |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
470 |
|
16186 | 471 |
self isPositionable ifFalse:[ |
472 |
"/ for non-positionable subclasses |
|
473 |
^ super skipThroughAll:aCollection |
|
474 |
]. |
|
475 |
||
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
476 |
len := aCollection size. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
477 |
first := aCollection at:1. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
478 |
[self atEnd] whileFalse:[ |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
479 |
buffer := self nextAvailable:len. |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
480 |
buffer size == len ifTrue:[ |
16209 | 481 |
buffer = aCollection ifTrue:[ |
482 |
^ self |
|
483 |
]. |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
484 |
"expect more input" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
485 |
idx := buffer indexOf:first startingAt:2. |
16209 | 486 |
idx ~~ 0 ifTrue:[ |
487 |
self skip:(idx - len - 1) |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
488 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
489 |
]. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
490 |
]. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
491 |
^ nil |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
492 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
493 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
494 |
|s| |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
495 |
s := ReadStream on:'12345678901234567890a'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
496 |
s skipThroughAll:'90ab'. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
497 |
s upToEnd |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
498 |
" |
2153
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
499 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
500 |
|s| |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
501 |
s := ReadStream on:'12345678901234567890'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
502 |
s skipThroughAll:'1234'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
503 |
s upToEnd |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
504 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
505 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
506 |
|s| |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
507 |
s := ReadStream on:'12345678901234567890'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
508 |
s skipThroughAll:'999'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
509 |
s atEnd |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
510 |
" |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
511 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
512 |
"Created: 26.6.1996 / 09:35:35 / cg" |
2154
89e96599e1e2
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2153
diff
changeset
|
513 |
"Modified: 11.1.1997 / 19:16:38 / cg" |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
514 |
! |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
515 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
516 |
skipToAll:aCollection |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
517 |
"skip for the sequence given by the argument, aCollection; |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
518 |
return nil if not found, self otherwise. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
519 |
On a successful match, the next read will return elements of aCollection." |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
520 |
|
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
521 |
|oldPos buffer len first idx| |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
522 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
523 |
oldPos := self position. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
524 |
len := aCollection size. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
525 |
first := aCollection at:1. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
526 |
[self atEnd] whileFalse:[ |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
527 |
buffer := self nextAvailable:len. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
528 |
buffer = aCollection ifTrue:[ |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
529 |
self position:(self position - len). |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
530 |
^ self |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
531 |
]. |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
532 |
buffer size == len ifTrue:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
533 |
"more input can be expected" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
534 |
idx := buffer indexOf:first startingAt:2. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
535 |
idx == 0 ifFalse:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
536 |
self position:(self position - len + idx - 1) |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
537 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
538 |
]. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
539 |
]. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
540 |
self position:oldPos. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
541 |
^ nil |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
542 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
543 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
544 |
|s| |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
545 |
s := ReadStream on:'12345678901234567890'. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
546 |
s skipToAll:'901'. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
547 |
s upToEnd |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
548 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
549 |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
550 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
551 |
|s| |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
552 |
s := ReadStream on:'1234567890'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
553 |
s skipToAll:'901'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
554 |
s upToEnd |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
555 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
556 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
557 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
558 |
|s| |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
559 |
s := 'Makefile' asFilename readStream. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
560 |
[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
561 |
(s skipToAll:'EDIT') notNil ifTrue:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
562 |
s next:100. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
563 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
564 |
] ensure:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
565 |
s close. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
566 |
] |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
567 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
568 |
|
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
569 |
"Modified: 26.6.1996 / 09:28:27 / cg" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
570 |
"Created: 26.6.1996 / 09:35:06 / cg" |
44 | 571 |
! ! |
611 | 572 |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
573 |
!PositionableStream methodsFor:'printing & storing'! |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
574 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
575 |
printOn:aStream |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
576 |
aStream nextPutAll:self className; nextPutAll:'(on:'; nextPutAll:collection classNameWithArticle; nextPut:$) |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
577 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
578 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
579 |
'' readStream printString |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
580 |
'' writeStream printString |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
581 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
582 |
! ! |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
583 |
|
611 | 584 |
!PositionableStream methodsFor:'private'! |
585 |
||
586 |
contentsSpecies |
|
587 |
"return a class of which instances will be returned, when |
|
588 |
parts of the collection are asked for. |
|
15640 | 589 |
(see upTo-kind of methods in Stream)" |
611 | 590 |
|
591 |
^ collection species |
|
592 |
! |
|
593 |
||
594 |
on:aCollection |
|
595 |
"setup for streaming on aCollection" |
|
596 |
||
597 |
collection := aCollection. |
|
598 |
readLimit := aCollection size. |
|
13379 | 599 |
readLimit == 0 ifTrue:[ |
600 |
self assert:(aCollection isCollection) |
|
601 |
]. |
|
15348 | 602 |
position := 0 |
611 | 603 |
! |
604 |
||
605 |
on:aCollection from:first to:last |
|
606 |
"setup for streaming on aCollection from first to last" |
|
607 |
||
608 |
collection := aCollection. |
|
15348 | 609 |
position := first - 1. |
611 | 610 |
readLimit := last |
611 |
! |
|
612 |
||
613 |
positionError |
|
1998 | 614 |
"{ Pragma: +optSpace }" |
615 |
||
2966 | 616 |
"report an error when positioning past the end |
617 |
or before the beginning." |
|
611 | 618 |
|
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
619 |
^ PositionOutOfBoundsError raiseRequestWith:nil |
4452
3d050f505a0a
Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents:
4416
diff
changeset
|
620 |
|
3d050f505a0a
Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents:
4416
diff
changeset
|
621 |
"Modified: / 26.7.1999 / 10:59:13 / stefan" |
611 | 622 |
! |
623 |
||
6314 | 624 |
positionError:badPostition |
625 |
"{ Pragma: +optSpace }" |
|
626 |
||
627 |
"report an error when positioning past the end |
|
628 |
or before the beginning." |
|
629 |
||
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
630 |
^ PositionOutOfBoundsError raiseRequestWith:badPostition |
6314 | 631 |
! |
632 |
||
611 | 633 |
with:aCollection |
634 |
"setup for streaming to the end of aCollection" |
|
635 |
||
636 |
collection := aCollection. |
|
11434 | 637 |
readLimit := aCollection size. |
611 | 638 |
self setToEnd |
11434 | 639 |
|
640 |
" |
|
641 |
(WriteStream with:#(1 2 3 4 5)) |
|
642 |
nextPut:6; |
|
643 |
contents |
|
644 |
" |
|
611 | 645 |
! ! |
646 |
||
16195 | 647 |
!PositionableStream methodsFor:'queries'! |
648 |
||
16209 | 649 |
endsBeforePositionWith:aSequenceableCollection |
16195 | 650 |
"answer true, if the elements in aSequenceableCollection |
16209 | 651 |
are at the current end of the stream up to position." |
16195 | 652 |
|
16209 | 653 |
|sz pos| |
16195 | 654 |
|
16209 | 655 |
sz := aSequenceableCollection size. |
656 |
pos := self position. |
|
657 |
pos < sz ifTrue:[ |
|
658 |
^ false. |
|
16195 | 659 |
]. |
16209 | 660 |
self contentsSpecies == collection class ifTrue:[ |
661 |
^ collection sameContentsFrom:pos+1-sz to:pos as:aSequenceableCollection startingAt:1. |
|
662 |
]. |
|
663 |
self position:pos-sz. |
|
664 |
^ (self next:sz) = aSequenceableCollection. |
|
16195 | 665 |
|
666 |
" |
|
16209 | 667 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'World' |
668 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello World' |
|
669 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello Worldx' |
|
670 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Bla' |
|
671 |
('' writeStream) endsBeforePositionWith:'Bla' |
|
672 |
('' writeStream) endsBeforePositionWith:'' |
|
16195 | 673 |
'' endsWith:'' |
674 |
" |
|
675 |
! ! |
|
676 |
||
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
677 |
!PositionableStream methodsFor:'reading'! |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
678 |
|
9003 | 679 |
nextAvailable:count |
17606 | 680 |
collection notNil ifTrue:[ |
681 |
|end result| |
|
9003 | 682 |
|
17606 | 683 |
end := position + count. |
684 |
end >= readLimit ifTrue:[ |
|
685 |
end := readLimit. |
|
686 |
]. |
|
687 |
||
688 |
result := collection copyFrom:position+1 to:end. |
|
689 |
position := end. |
|
690 |
^ result. |
|
9003 | 691 |
]. |
692 |
||
17606 | 693 |
^ super nextAvailable:count. |
9003 | 694 |
|
695 |
" |
|
696 |
'abc' readStream nextAvailable:1. |
|
697 |
'abc' readStream nextAvailable:2. |
|
698 |
'abc' readStream nextAvailable:3. |
|
699 |
'abc' readStream nextAvailable:4. |
|
700 |
||
701 |
'abc' readStream nextAvailable:2; nextAvailable:2. |
|
702 |
'abc' readStream nextAvailable:3; nextAvailable:3. |
|
703 |
" |
|
704 |
! |
|
705 |
||
17611 | 706 |
nextAvailable:count into:aCollection startingAt:initialIndex |
707 |
"return the next count objects from the stream. If the end is |
|
708 |
reached before, only that many objects are copyied into the |
|
709 |
collection. |
|
710 |
Returns the number of objects that have been actually read." |
|
711 |
||
712 |
| max | |
|
713 |
||
714 |
collection notNil ifTrue:[ |
|
715 |
max := (readLimit - position) min: count. |
|
716 |
aCollection |
|
717 |
replaceFrom: initialIndex |
|
718 |
to: initialIndex+max-1 |
|
719 |
with: collection |
|
720 |
startingAt: position+1. |
|
721 |
position := position + max. |
|
722 |
^ max. |
|
723 |
]. |
|
724 |
||
725 |
^ super nextAvailable:count into:aCollection startingAt:initialIndex. |
|
726 |
||
727 |
" |
|
728 |
|s n buffer| |
|
729 |
||
730 |
buffer := ByteArray new:10. |
|
731 |
||
732 |
s := ReadStream on:#[1 2 3 4 5 6 7 8 9]. |
|
733 |
s next:3. |
|
734 |
n := s nextBytes:9 into:buffer startingAt:1. |
|
735 |
Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer) |
|
736 |
" |
|
737 |
! |
|
738 |
||
739 |
nextBytes:numBytes into:aCollection startingAt:initialIndex |
|
740 |
"return the next numBytes from the stream. If the end is |
|
741 |
reached before, only that many bytes are copyied into the |
|
742 |
collection. |
|
743 |
Returns the number of bytes that have been actually read. |
|
744 |
The receiver must support reading of binary bytes. |
|
745 |
||
746 |
Notice: this method is provided here for protocol completeness |
|
747 |
with externalStreams - it is normally not used with other |
|
748 |
streams." |
|
749 |
||
750 |
|max| |
|
751 |
||
752 |
(collection isByteCollection |
|
753 |
and:[aCollection isByteCollection]) ifTrue:[ |
|
754 |
"do it the fast way" |
|
755 |
max := (readLimit - position) min: numBytes. |
|
756 |
aCollection |
|
757 |
replaceBytesFrom:initialIndex |
|
758 |
to:(initialIndex + max - 1) |
|
759 |
with:collection |
|
760 |
startingAt:position+1. |
|
761 |
position := position + max. |
|
762 |
^ max |
|
763 |
]. |
|
764 |
"do it the hard way" |
|
765 |
^ super nextBytes:numBytes into:aCollection startingAt:initialIndex |
|
766 |
||
767 |
" |
|
768 |
|s n buffer| |
|
769 |
||
770 |
buffer := ByteArray new:10. |
|
771 |
||
772 |
s := ReadStream on:#[1 2 3 4 5 6 7 8 9]. |
|
773 |
s next:3. |
|
774 |
n := s nextBytes:9 into:buffer startingAt:1. |
|
775 |
Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer) |
|
776 |
" |
|
777 |
! |
|
778 |
||
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
779 |
upToAll:aCollection |
16186 | 780 |
"read until a subcollection consisisting of the elements in aCollection is encountered. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
781 |
Return everything read excluding the elements in aCollection. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
782 |
The position is left before the collection; i.e. the next |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
783 |
read operations will return those elements. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
784 |
If no such subcollection is encountered, all elements up to the end |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
785 |
are read and returned. |
6494 | 786 |
See also #throughAll: which also reads up to some objects |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
787 |
but positions behind it and DOES include it in the returned |
3184
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
788 |
collection. |
6494 | 789 |
See also #upToAllExcluding:, which returns the same, but leaves the |
790 |
read pointer after the matched subcollection. |
|
3184
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
791 |
|
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
792 |
Note: this behavior is inconsistent with the other upTo.. methods, |
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
793 |
which position after the found item. We implement the method |
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
794 |
this way for the sake of ST80-compatibility." |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
795 |
|
17611 | 796 |
"/ in the future, this will have the Squeak semantics, which is upToAllExcluding: |
16189 | 797 |
|
798 |
^ self upToAll_positionBefore:aCollection |
|
799 |
||
800 |
" |
|
801 |
|s| |
|
802 |
s := ReadStream on:'hello world'. |
|
803 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
804 |
Transcript showCR:s atEnd. |
|
805 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
|
806 |
" |
|
807 |
" |
|
808 |
|s| |
|
809 |
s := ReadStream on:'hello world'. |
|
810 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
811 |
Transcript showCR:s atEnd. |
|
812 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
813 |
" |
|
814 |
" |
|
815 |
|s| |
|
816 |
s := ReadStream on:'hello world'. |
|
817 |
Transcript show:'<'; show:(s upToAll:'xx'); showCR:'>'. |
|
818 |
Transcript showCR:s atEnd. |
|
819 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
|
820 |
" |
|
821 |
||
822 |
"Modified: / 12.1.1998 / 22:06:42 / cg" |
|
823 |
"Created: / 12.1.1998 / 22:07:01 / cg" |
|
824 |
! |
|
825 |
||
826 |
upToAll_positionBefore:aCollection |
|
827 |
"read until a subcollection consisisting of the elements in aCollection is encountered. |
|
828 |
Return everything read excluding the elements in aCollection. |
|
829 |
The position is left before the collection; i.e. the next |
|
830 |
read operations will return those elements. |
|
831 |
If no such subcollection is encountered, all elements up to the end |
|
832 |
are read and returned. |
|
833 |
See also #throughAll: which also reads up to some objects |
|
834 |
but positions behind it and DOES include it in the returned |
|
16840 | 835 |
collection." |
16189 | 836 |
|
16209 | 837 |
|answerStream element last| |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
838 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
839 |
last := aCollection last. |
16209 | 840 |
answerStream := WriteStream on:(self contentsSpecies new:100). |
841 |
[(element := self nextOrNil) notNil] whileTrue:[ |
|
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
842 |
answerStream nextPut:element. |
16209 | 843 |
(element = last and:[answerStream endsBeforePositionWith:aCollection]) ifTrue:[ |
844 |
|backStep| |
|
845 |
backStep := aCollection size negated. |
|
846 |
self skip:backStep. |
|
847 |
answerStream skip:backStep. |
|
848 |
^ answerStream contents |
|
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
849 |
]. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
850 |
]. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
851 |
^ answerStream contents |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
852 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
853 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
854 |
|s| |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
855 |
s := ReadStream on:'hello world'. |
16209 | 856 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
6494 | 857 |
Transcript showCR:s atEnd. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
858 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
859 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
860 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
861 |
|s| |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
862 |
s := ReadStream on:'hello world'. |
16209 | 863 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
16186 | 864 |
Transcript showCR:s atEnd. |
16209 | 865 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
16186 | 866 |
" |
867 |
" |
|
868 |
|s| |
|
869 |
s := ReadStream on:'hello world'. |
|
16209 | 870 |
Transcript show:'<'; show:(s upToAll_positionBefore:'xx'); showCR:'>'. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
871 |
Transcript showCR:s atEnd. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
872 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
873 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
874 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
875 |
"Modified: / 12.1.1998 / 22:06:42 / cg" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
876 |
"Created: / 12.1.1998 / 22:07:01 / cg" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
877 |
! ! |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
878 |
|
611 | 879 |
!PositionableStream methodsFor:'testing'! |
880 |
||
881 |
atEnd |
|
882 |
"return true, if the read-position is at the end" |
|
883 |
||
15357 | 884 |
^ position >= readLimit |
611 | 885 |
! |
886 |
||
887 |
isEmpty |
|
888 |
"return true, if the contents of the stream is empty" |
|
889 |
||
890 |
^ readLimit == 0 |
|
9006 | 891 |
! |
892 |
||
10577 | 893 |
isInternalByteStream |
894 |
"return true, if the stream is an internal stream reading bytes" |
|
895 |
||
896 |
^ collection class isBytes |
|
897 |
||
898 |
"Created: / 30-05-2007 / 16:16:12 / cg" |
|
899 |
! |
|
900 |
||
9006 | 901 |
isPositionable |
902 |
"return true, if the stream supports positioning (this one is)" |
|
903 |
||
904 |
^ true |
|
611 | 905 |
! ! |
906 |
||
1897 | 907 |
!PositionableStream class methodsFor:'documentation'! |
701 | 908 |
|
909 |
version |
|
19403 | 910 |
^ '$Header$' |
13379 | 911 |
! |
912 |
||
913 |
version_CVS |
|
19403 | 914 |
^ '$Header$' |
701 | 915 |
! ! |
6777
5efddf5358b2
currentFileInDirectory query
Claus Gittinger <cg@exept.de>
parents:
6707
diff
changeset
|
916 |
|
14934 | 917 |
|
611 | 918 |
PositionableStream initialize! |