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