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