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