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