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