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