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