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