author | Stefan Vogel <sv@exept.de> |
Thu, 12 Mar 2015 18:14:00 +0100 | |
changeset 17606 | b30bac248329 |
parent 17559 | 010577abda05 |
child 17611 | a3d9a43a6197 |
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 |
||
611 | 256 |
!PositionableStream methodsFor:'positioning'! |
257 |
||
258 |
backStep |
|
259 |
"move backward read position by one" |
|
260 |
||
4405 | 261 |
self position:(self position - 1) |
611 | 262 |
! |
263 |
||
16188 | 264 |
match: subCollection |
265 |
"Set the access position of the receiver to be past the next occurrence of the subCollection. |
|
266 |
Answer whether subCollection is found. |
|
267 |
No wildcards, and case does matter." |
|
268 |
||
269 |
| pattern startMatch | |
|
270 |
||
271 |
pattern := ReadStream on: subCollection. |
|
272 |
startMatch := nil. |
|
273 |
[pattern atEnd] whileFalse: [ |
|
274 |
self atEnd ifTrue: [^ false]. |
|
275 |
(self next) = (pattern next) |
|
276 |
ifTrue: [pattern position = 1 ifTrue: [startMatch := self position]] |
|
277 |
ifFalse: [ |
|
278 |
pattern position: 0. |
|
279 |
startMatch ifNotNil: [ |
|
280 |
self position: startMatch. |
|
281 |
startMatch := nil |
|
282 |
] |
|
283 |
] |
|
284 |
]. |
|
285 |
^ true |
|
286 |
||
287 |
" |
|
288 |
'abc def ghi' readStream match:'def'; upToEnd |
|
289 |
" |
|
290 |
! |
|
291 |
||
611 | 292 |
position |
15615 | 293 |
"return the read position (0-based)" |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
294 |
|
15348 | 295 |
^ position |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
296 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
297 |
|
15615 | 298 |
position0Based |
299 |
<resource: #obsolete> |
|
300 |
"return the read position 0-based" |
|
301 |
||
302 |
^ self position |
|
303 |
! |
|
304 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
305 |
position0Based:index0Based |
15615 | 306 |
<resource: #obsolete> |
4405 | 307 |
"set the read (or write) position" |
611 | 308 |
|
15615 | 309 |
self position:index0Based |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
310 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
311 |
|
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
312 |
position1Based |
15615 | 313 |
<resource: #obsolete> |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
314 |
"return the read position 1-based" |
611 | 315 |
|
15615 | 316 |
^ self position + 1 |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
317 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
318 |
|
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
319 |
position1Based:index1Based |
15615 | 320 |
<resource: #obsolete> |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
321 |
"set the read (or write) position" |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
322 |
|
15615 | 323 |
self position:(index1Based - 1) |
4405 | 324 |
|
325 |
" |
|
326 |
|s| |
|
327 |
||
328 |
s := '1234567890' readStream. |
|
329 |
s next:5. |
|
330 |
s position:1. |
|
331 |
s next:7. |
|
332 |
" |
|
333 |
||
334 |
" |
|
335 |
|s| |
|
336 |
||
337 |
s := '' writeStream. |
|
338 |
s nextPutAll:'1234567890'. |
|
339 |
s position:5. |
|
340 |
s nextPutAll:'abcdefg'. |
|
341 |
s contents |
|
342 |
" |
|
343 |
||
344 |
" |
|
345 |
|s| |
|
346 |
||
347 |
s := '' writeStream. |
|
348 |
s nextPutAll:'1234567890'. |
|
349 |
s position:0. |
|
350 |
s nextPutAll:'abcdefg'. |
|
351 |
s contents |
|
352 |
" |
|
611 | 353 |
! |
354 |
||
15615 | 355 |
position:index0Based |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
356 |
"set the read (or write) position" |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
357 |
|
15615 | 358 |
((index0Based > readLimit) or:[index0Based < 0]) ifTrue: [^ self positionError:index0Based]. |
359 |
position := index0Based |
|
6626
f7b08d6bee3f
#positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents:
6538
diff
changeset
|
360 |
! |
f7b08d6bee3f
#positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents:
6538
diff
changeset
|
361 |
|
611 | 362 |
reset |
363 |
"set the read position to the beginning of the collection" |
|
364 |
||
5308 | 365 |
self resetPosition |
366 |
||
367 |
" |
|
368 |
|s| |
|
369 |
||
370 |
s := 'hello world' readStream. |
|
371 |
Transcript showCR:(s next:5). |
|
372 |
s reset. |
|
373 |
Transcript showCR:(s next:10). |
|
374 |
" |
|
375 |
! |
|
376 |
||
377 |
resetPosition |
|
378 |
"set the read position to the beginning of the collection" |
|
379 |
||
15348 | 380 |
position := 0 |
4405 | 381 |
|
382 |
" |
|
383 |
|s| |
|
384 |
||
385 |
s := 'hello world' readStream. |
|
386 |
Transcript showCR:(s next:5). |
|
387 |
s reset. |
|
388 |
Transcript showCR:(s next:10). |
|
389 |
" |
|
611 | 390 |
! |
391 |
||
392 |
setToEnd |
|
11433 | 393 |
"set the read position to the end of the collection. |
394 |
#next will return EOF, #nextPut: will append to the stream. |
|
395 |
(same Behavior as FileStream." |
|
611 | 396 |
|
15348 | 397 |
position := readLimit |
611 | 398 |
! |
399 |
||
400 |
skip:numberToSkip |
|
401 |
"skip the next numberToSkip elements" |
|
402 |
||
5774 | 403 |
numberToSkip ~~ 0 ifTrue:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6924
diff
changeset
|
404 |
self position:(self position + numberToSkip) |
5774 | 405 |
] |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
406 |
! |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
407 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
408 |
skipThroughAll:aCollection |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
409 |
"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
|
410 |
return nil if not found, self otherwise. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
411 |
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
|
412 |
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
|
413 |
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
|
414 |
|
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
415 |
|buffer len first idx| |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
416 |
|
16186 | 417 |
self isPositionable ifFalse:[ |
418 |
"/ for non-positionable subclasses |
|
419 |
^ super skipThroughAll:aCollection |
|
420 |
]. |
|
421 |
||
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
422 |
len := aCollection size. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
423 |
first := aCollection at:1. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
424 |
[self atEnd] whileFalse:[ |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
425 |
buffer := self nextAvailable:len. |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
426 |
buffer size == len ifTrue:[ |
16209 | 427 |
buffer = aCollection ifTrue:[ |
428 |
^ self |
|
429 |
]. |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
430 |
"expect more input" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
431 |
idx := buffer indexOf:first startingAt:2. |
16209 | 432 |
idx ~~ 0 ifTrue:[ |
433 |
self skip:(idx - len - 1) |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
434 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
435 |
]. |
1489
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 |
^ nil |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
438 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
439 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
440 |
|s| |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
441 |
s := ReadStream on:'12345678901234567890a'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
442 |
s skipThroughAll:'90ab'. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
443 |
s upToEnd |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
444 |
" |
2153
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
445 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
446 |
|s| |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
447 |
s := ReadStream on:'12345678901234567890'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
448 |
s skipThroughAll:'1234'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
449 |
s upToEnd |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
450 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
451 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
452 |
|s| |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
453 |
s := ReadStream on:'12345678901234567890'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
454 |
s skipThroughAll:'999'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
455 |
s atEnd |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
456 |
" |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
457 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
458 |
"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
|
459 |
"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
|
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 |
skipToAll:aCollection |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
463 |
"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
|
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 of aCollection." |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
466 |
|
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
467 |
|oldPos buffer len first idx| |
1489
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 |
oldPos := self position. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
470 |
len := aCollection size. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
471 |
first := aCollection at:1. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
472 |
[self atEnd] whileFalse:[ |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
473 |
buffer := self nextAvailable:len. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
474 |
buffer = aCollection ifTrue:[ |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
475 |
self position:(self position - len). |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
476 |
^ self |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
477 |
]. |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
478 |
buffer size == len ifTrue:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
479 |
"more input can be expected" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
480 |
idx := buffer indexOf:first startingAt:2. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
481 |
idx == 0 ifFalse:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
482 |
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
|
483 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
484 |
]. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
485 |
]. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
486 |
self position:oldPos. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
487 |
^ nil |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
488 |
|
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 |
|s| |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
491 |
s := ReadStream on:'12345678901234567890'. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
492 |
s skipToAll:'901'. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
493 |
s upToEnd |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
494 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
495 |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
496 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
497 |
|s| |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
498 |
s := ReadStream on:'1234567890'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
499 |
s skipToAll:'901'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
500 |
s upToEnd |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
501 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
502 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
503 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
504 |
|s| |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
505 |
s := 'Makefile' asFilename readStream. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
506 |
[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
507 |
(s skipToAll:'EDIT') notNil ifTrue:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
508 |
s next:100. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
509 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
510 |
] ensure:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
511 |
s close. |
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 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
514 |
|
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
515 |
"Modified: 26.6.1996 / 09:28:27 / cg" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
516 |
"Created: 26.6.1996 / 09:35:06 / cg" |
44 | 517 |
! ! |
611 | 518 |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
519 |
!PositionableStream methodsFor:'printing & storing'! |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
520 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
521 |
printOn:aStream |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
522 |
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
|
523 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
524 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
525 |
'' readStream printString |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
526 |
'' writeStream printString |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
527 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
528 |
! ! |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
529 |
|
611 | 530 |
!PositionableStream methodsFor:'private'! |
531 |
||
532 |
contentsSpecies |
|
533 |
"return a class of which instances will be returned, when |
|
534 |
parts of the collection are asked for. |
|
15640 | 535 |
(see upTo-kind of methods in Stream)" |
611 | 536 |
|
537 |
^ collection species |
|
538 |
! |
|
539 |
||
540 |
on:aCollection |
|
541 |
"setup for streaming on aCollection" |
|
542 |
||
543 |
collection := aCollection. |
|
544 |
readLimit := aCollection size. |
|
13379 | 545 |
readLimit == 0 ifTrue:[ |
546 |
self assert:(aCollection isCollection) |
|
547 |
]. |
|
15348 | 548 |
position := 0 |
611 | 549 |
! |
550 |
||
551 |
on:aCollection from:first to:last |
|
552 |
"setup for streaming on aCollection from first to last" |
|
553 |
||
554 |
collection := aCollection. |
|
15348 | 555 |
position := first - 1. |
611 | 556 |
readLimit := last |
557 |
! |
|
558 |
||
559 |
positionError |
|
1998 | 560 |
"{ Pragma: +optSpace }" |
561 |
||
2966 | 562 |
"report an error when positioning past the end |
563 |
or before the beginning." |
|
611 | 564 |
|
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
565 |
^ PositionOutOfBoundsError raiseRequestWith:nil |
4452
3d050f505a0a
Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents:
4416
diff
changeset
|
566 |
|
3d050f505a0a
Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents:
4416
diff
changeset
|
567 |
"Modified: / 26.7.1999 / 10:59:13 / stefan" |
611 | 568 |
! |
569 |
||
6314 | 570 |
positionError:badPostition |
571 |
"{ Pragma: +optSpace }" |
|
572 |
||
573 |
"report an error when positioning past the end |
|
574 |
or before the beginning." |
|
575 |
||
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
576 |
^ PositionOutOfBoundsError raiseRequestWith:badPostition |
6314 | 577 |
! |
578 |
||
611 | 579 |
with:aCollection |
580 |
"setup for streaming to the end of aCollection" |
|
581 |
||
582 |
collection := aCollection. |
|
11434 | 583 |
readLimit := aCollection size. |
611 | 584 |
self setToEnd |
11434 | 585 |
|
586 |
" |
|
587 |
(WriteStream with:#(1 2 3 4 5)) |
|
588 |
nextPut:6; |
|
589 |
contents |
|
590 |
" |
|
611 | 591 |
! ! |
592 |
||
16195 | 593 |
!PositionableStream methodsFor:'queries'! |
594 |
||
16209 | 595 |
endsBeforePositionWith:aSequenceableCollection |
16195 | 596 |
"answer true, if the elements in aSequenceableCollection |
16209 | 597 |
are at the current end of the stream up to position." |
16195 | 598 |
|
16209 | 599 |
|sz pos| |
16195 | 600 |
|
16209 | 601 |
sz := aSequenceableCollection size. |
602 |
pos := self position. |
|
603 |
pos < sz ifTrue:[ |
|
604 |
^ false. |
|
16195 | 605 |
]. |
16209 | 606 |
self contentsSpecies == collection class ifTrue:[ |
607 |
^ collection sameContentsFrom:pos+1-sz to:pos as:aSequenceableCollection startingAt:1. |
|
608 |
]. |
|
609 |
self position:pos-sz. |
|
610 |
^ (self next:sz) = aSequenceableCollection. |
|
16195 | 611 |
|
612 |
" |
|
16209 | 613 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'World' |
614 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello World' |
|
615 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello Worldx' |
|
616 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Bla' |
|
617 |
('' writeStream) endsBeforePositionWith:'Bla' |
|
618 |
('' writeStream) endsBeforePositionWith:'' |
|
16195 | 619 |
'' endsWith:'' |
620 |
" |
|
621 |
! ! |
|
622 |
||
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
623 |
!PositionableStream methodsFor:'reading'! |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
624 |
|
9003 | 625 |
nextAvailable:count |
17606 | 626 |
collection notNil ifTrue:[ |
627 |
|end result| |
|
9003 | 628 |
|
17606 | 629 |
end := position + count. |
630 |
end >= readLimit ifTrue:[ |
|
631 |
end := readLimit. |
|
632 |
]. |
|
633 |
||
634 |
result := collection copyFrom:position+1 to:end. |
|
635 |
position := end. |
|
636 |
^ result. |
|
9003 | 637 |
]. |
638 |
||
17606 | 639 |
^ super nextAvailable:count. |
9003 | 640 |
|
641 |
" |
|
642 |
'abc' readStream nextAvailable:1. |
|
643 |
'abc' readStream nextAvailable:2. |
|
644 |
'abc' readStream nextAvailable:3. |
|
645 |
'abc' readStream nextAvailable:4. |
|
646 |
||
647 |
'abc' readStream nextAvailable:2; nextAvailable:2. |
|
648 |
'abc' readStream nextAvailable:3; nextAvailable:3. |
|
649 |
" |
|
650 |
! |
|
651 |
||
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
652 |
upToAll:aCollection |
16186 | 653 |
"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
|
654 |
Return everything read excluding the elements in aCollection. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
655 |
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
|
656 |
read operations will return those elements. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
657 |
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
|
658 |
are read and returned. |
6494 | 659 |
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
|
660 |
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
|
661 |
collection. |
6494 | 662 |
See also #upToAllExcluding:, which returns the same, but leaves the |
663 |
read pointer after the matched subcollection. |
|
3184
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
664 |
|
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
665 |
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
|
666 |
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
|
667 |
this way for the sake of ST80-compatibility." |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
668 |
|
16189 | 669 |
"/ in the future, this wil have the Squeak semantics, which is upToAllExcluding: |
670 |
||
671 |
^ self upToAll_positionBefore:aCollection |
|
672 |
||
673 |
" |
|
674 |
|s| |
|
675 |
s := ReadStream on:'hello world'. |
|
676 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
677 |
Transcript showCR:s atEnd. |
|
678 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
|
679 |
" |
|
680 |
" |
|
681 |
|s| |
|
682 |
s := ReadStream on:'hello world'. |
|
683 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
684 |
Transcript showCR:s atEnd. |
|
685 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
686 |
" |
|
687 |
" |
|
688 |
|s| |
|
689 |
s := ReadStream on:'hello world'. |
|
690 |
Transcript show:'<'; show:(s upToAll:'xx'); showCR:'>'. |
|
691 |
Transcript showCR:s atEnd. |
|
692 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
|
693 |
" |
|
694 |
||
695 |
"Modified: / 12.1.1998 / 22:06:42 / cg" |
|
696 |
"Created: / 12.1.1998 / 22:07:01 / cg" |
|
697 |
! |
|
698 |
||
699 |
upToAll_positionBefore:aCollection |
|
700 |
"read until a subcollection consisisting of the elements in aCollection is encountered. |
|
701 |
Return everything read excluding the elements in aCollection. |
|
702 |
The position is left before the collection; i.e. the next |
|
703 |
read operations will return those elements. |
|
704 |
If no such subcollection is encountered, all elements up to the end |
|
705 |
are read and returned. |
|
706 |
See also #throughAll: which also reads up to some objects |
|
707 |
but positions behind it and DOES include it in the returned |
|
16840 | 708 |
collection." |
16189 | 709 |
|
16209 | 710 |
|answerStream element last| |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
711 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
712 |
last := aCollection last. |
16209 | 713 |
answerStream := WriteStream on:(self contentsSpecies new:100). |
714 |
[(element := self nextOrNil) notNil] whileTrue:[ |
|
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
715 |
answerStream nextPut:element. |
16209 | 716 |
(element = last and:[answerStream endsBeforePositionWith:aCollection]) ifTrue:[ |
717 |
|backStep| |
|
718 |
backStep := aCollection size negated. |
|
719 |
self skip:backStep. |
|
720 |
answerStream skip:backStep. |
|
721 |
^ answerStream contents |
|
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
722 |
]. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
723 |
]. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
724 |
^ answerStream contents |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
725 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
726 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
727 |
|s| |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
728 |
s := ReadStream on:'hello world'. |
16209 | 729 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
6494 | 730 |
Transcript showCR:s atEnd. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
731 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
732 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
733 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
734 |
|s| |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
735 |
s := ReadStream on:'hello world'. |
16209 | 736 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
16186 | 737 |
Transcript showCR:s atEnd. |
16209 | 738 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
16186 | 739 |
" |
740 |
" |
|
741 |
|s| |
|
742 |
s := ReadStream on:'hello world'. |
|
16209 | 743 |
Transcript show:'<'; show:(s upToAll_positionBefore:'xx'); showCR:'>'. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
744 |
Transcript showCR:s atEnd. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
745 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
746 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
747 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
748 |
"Modified: / 12.1.1998 / 22:06:42 / cg" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
749 |
"Created: / 12.1.1998 / 22:07:01 / cg" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
750 |
! ! |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
751 |
|
611 | 752 |
!PositionableStream methodsFor:'testing'! |
753 |
||
754 |
atEnd |
|
755 |
"return true, if the read-position is at the end" |
|
756 |
||
15357 | 757 |
^ position >= readLimit |
611 | 758 |
! |
759 |
||
760 |
isEmpty |
|
761 |
"return true, if the contents of the stream is empty" |
|
762 |
||
763 |
^ readLimit == 0 |
|
9006 | 764 |
! |
765 |
||
10577 | 766 |
isInternalByteStream |
767 |
"return true, if the stream is an internal stream reading bytes" |
|
768 |
||
769 |
^ collection class isBytes |
|
770 |
||
771 |
"Created: / 30-05-2007 / 16:16:12 / cg" |
|
772 |
! |
|
773 |
||
9006 | 774 |
isPositionable |
775 |
"return true, if the stream supports positioning (this one is)" |
|
776 |
||
777 |
^ true |
|
611 | 778 |
! ! |
779 |
||
1897 | 780 |
!PositionableStream class methodsFor:'documentation'! |
701 | 781 |
|
782 |
version |
|
17606 | 783 |
^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.170 2015-03-12 17:14:00 stefan Exp $' |
13379 | 784 |
! |
785 |
||
786 |
version_CVS |
|
17606 | 787 |
^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.170 2015-03-12 17:14:00 stefan Exp $' |
701 | 788 |
! ! |
6777
5efddf5358b2
currentFileInDirectory query
Claus Gittinger <cg@exept.de>
parents:
6707
diff
changeset
|
789 |
|
14934 | 790 |
|
611 | 791 |
PositionableStream initialize! |