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