author | Patrik Svestka <patrik.svestka@gmail.com> |
Thu, 02 Dec 2021 12:27:29 +0100 | |
branch | jv |
changeset 25440 | 039a5cea86c3 |
parent 25408 | b3dc7dede514 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
23107 | 3 |
COPYRIGHT (c) 2015 Jan Vrany |
25408
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4 |
COPYRIGHT (c) 2021 LabWare |
159 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5393
7e2230bd4bad
moved some stuff to superclass
Claus Gittinger <cg@exept.de>
parents:
5308
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
7e2230bd4bad
moved some stuff to superclass
Claus Gittinger <cg@exept.de>
parents:
5308
diff
changeset
|
15 |
|
17559 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
57 | 18 |
PeekableStream subclass:#PositionableStream |
973 | 19 |
instanceVariableNames:'collection position readLimit writeLimit' |
8443
7bc4348c059e
fileIn stuff moved to peekableStream
Claus Gittinger <cg@exept.de>
parents:
8363
diff
changeset
|
20 |
classVariableNames:'ZeroPosition InvalidPositionErrorSignal' |
973 | 21 |
poolDictionaries:'' |
22 |
category:'Streams' |
|
1 | 23 |
! |
24 |
||
1897 | 25 |
!PositionableStream class methodsFor:'documentation'! |
88 | 26 |
|
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
23107 | 30 |
COPYRIGHT (c) 2015 Jan Vrany |
25408
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
31 |
COPYRIGHT (c) 2021 LabWare |
159 | 32 |
All Rights Reserved |
1 | 33 |
|
88 | 34 |
This software is furnished under a license and may be used |
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
! |
|
10 | 42 |
|
88 | 43 |
documentation |
44 |
" |
|
45 |
Instances of PositionableStream allow positioning the read pointer. |
|
46 |
The PositionableStream class also adds methods for source-chunk reading |
|
47 |
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
|
48 |
|
88 | 49 |
This is an abstract class. |
1295 | 50 |
|
4405 | 51 |
Compatibility Notice: |
9434 | 52 |
In previous versions of ST/X, streams started with a 1-position (i.e. as in collections), |
53 |
while ST-80 has always been using 0-based postions for streams and 1-based positions for collections. |
|
4405 | 54 |
|
17559 | 55 |
THIS CERTAINLY IS BAD. |
56 |
||
57 |
Although this is confusing ST/X has been changed to now also uses 0-based stream positioning. |
|
9434 | 58 |
|
17559 | 59 |
[caveat:] |
60 |
Basing capabilities like readability/writability/positionability/peekability on inheritance makes |
|
61 |
the class hierarchy ugly and leads to strange and hard to teach redefinitions (aka. NonPositionableStream |
|
62 |
below PositionableStream or ExternalReadStream under WriteStream) |
|
4405 | 63 |
|
1295 | 64 |
[author:] |
9434 | 65 |
Claus Gittinger |
88 | 66 |
" |
67 |
! ! |
|
1 | 68 |
|
1897 | 69 |
!PositionableStream class methodsFor:'initialization'! |
1 | 70 |
|
10 | 71 |
initialize |
15353 | 72 |
ZeroPosition := 0. "/ changed with stx rel5.1; |
73 |
"/ rel6.2: no longer used (i.e. 0 has now ben hardcoded) |
|
74 |
"/ class variable to be eliminated. |
|
7363 | 75 |
|
8443
7bc4348c059e
fileIn stuff moved to peekableStream
Claus Gittinger <cg@exept.de>
parents:
8363
diff
changeset
|
76 |
InvalidPositionErrorSignal isNil ifTrue:[ |
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
77 |
InvalidPositionErrorSignal := PositionOutOfBoundsError. |
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
78 |
InvalidPositionErrorSignal notifierString:'position out of bounds: '. |
10 | 79 |
] |
80 |
! ! |
|
1 | 81 |
|
1897 | 82 |
!PositionableStream class methodsFor:'instance creation'! |
1 | 83 |
|
84 |
on:aCollection |
|
85 |
"return a new PositionableStream streaming on aCollection" |
|
86 |
||
87 |
^ (self basicNew) on:aCollection |
|
88 |
! |
|
89 |
||
90 |
on:aCollection from:first to:last |
|
91 |
"return a new PositionableStream streaming on aCollection |
|
92 |
from first to last" |
|
93 |
||
369 | 94 |
^ (self basicNew) on:aCollection from:first to:last |
57 | 95 |
! |
96 |
||
97 |
with:aCollection |
|
98 |
"return a new PositionableStream streaming on aCollection, |
|
99 |
the stream is positioned to the end of the collection." |
|
100 |
||
362 | 101 |
^ (self basicNew) with:aCollection |
1 | 102 |
! ! |
103 |
||
3032
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
104 |
!PositionableStream class methodsFor:'Signal constants'! |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
105 |
|
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
106 |
invalidPositionErrorSignal |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
107 |
"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
|
108 |
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
|
109 |
the end)" |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
110 |
|
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
111 |
^ PositionOutOfBoundsError |
3032
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
112 |
! ! |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
113 |
|
1897 | 114 |
!PositionableStream class methodsFor:'constants'! |
1 | 115 |
|
4405 | 116 |
zeroPosition |
117 |
"return the number, which marks the initial position. |
|
118 |
Compatibility Notice: |
|
9434 | 119 |
In previous versions of ST/X, streams started with a 1-position (i.e. as in collections), |
120 |
while ST-80 has always been using 0-based postions for streams and 1-based positions for collections. |
|
4405 | 121 |
|
9434 | 122 |
Although this is confusing ST/X has been changed to now also use 0-based stream positioning. |
123 |
" |
|
4405 | 124 |
|
15353 | 125 |
^ 0 |
9434 | 126 |
|
127 |
"Modified: / 13-07-2006 / 20:36:54 / cg" |
|
360 | 128 |
! ! |
129 |
||
21034 | 130 |
!PositionableStream class methodsFor:'testing'! |
131 |
||
132 |
isAbstract |
|
133 |
^ self == PositionableStream |
|
134 |
! ! |
|
135 |
||
25408
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
136 |
|
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
137 |
|
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
138 |
|
7261 | 139 |
!PositionableStream methodsFor:'Compatibility-Dolphin'! |
6413 | 140 |
|
141 |
endChunk |
|
142 |
self nextPutChunkSeparator |
|
143 |
! ! |
|
144 |
||
9284 | 145 |
!PositionableStream methodsFor:'Compatibility-ST/V'! |
146 |
||
147 |
skipTo:anElement |
|
148 |
"ST/V compatibility: |
|
149 |
skip for the element given by the argument, anElement; |
|
150 |
return nil if not found, self otherwise. |
|
151 |
On a successful match, the next read will return the element after anElement." |
|
152 |
||
153 |
^ self skipThrough:anElement |
|
154 |
||
155 |
" |
|
156 |
|s| |
|
157 |
s := ReadStream on:'12345678901234567890'. |
|
158 |
s skipTo:$5. |
|
159 |
s copyFrom:1 to:(s position). |
|
160 |
s upToEnd |
|
161 |
" |
|
162 |
! ! |
|
163 |
||
16876
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
164 |
!PositionableStream methodsFor:'Compatibility-Squeak'! |
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 |
back |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
167 |
"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
|
168 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
169 |
self position = 0 ifTrue: [self positionError]. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
170 |
self skip: -1. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
171 |
^ self peek |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
172 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
173 |
"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
|
174 |
! |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
175 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
176 |
peekBack |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
177 |
"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
|
178 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
179 |
| element | |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
180 |
element := self back. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
181 |
self skip: 1. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
182 |
^ element |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
183 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
184 |
"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
|
185 |
! |
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
186 |
|
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
187 |
upToAnyOf: aCollection |
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
188 |
"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
|
189 |
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
|
190 |
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
|
191 |
^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
|
192 |
|
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
193 |
"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
|
194 |
! |
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
195 |
|
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
196 |
upToAnyOf: subcollection do: aBlock |
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
197 |
"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
|
198 |
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
|
199 |
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
|
200 |
|
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
201 |
^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
|
202 |
| ch | |
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
203 |
[ 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
|
204 |
whileFalse: [ stream nextPut: ch ] ] |
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
205 |
|
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
206 |
"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
|
207 |
! ! |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
208 |
|
1 | 209 |
!PositionableStream methodsFor:'accessing'! |
210 |
||
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
211 |
collection |
19591 | 212 |
"return the underlying collection buffer. |
213 |
Notice, that this buffer may become invalid after being retrieved, |
|
214 |
if more data is written to the stream (because a bigger buffer might be |
|
215 |
allocated). Therefore, it should be only used in special situations, |
|
216 |
where an already filed buffer needs to be backpatched later |
|
217 |
(eg. before being sent out to some external stream or socket)" |
|
218 |
||
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
219 |
^ collection |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
220 |
! |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
221 |
|
1 | 222 |
contents |
223 |
"return the entire contents of the stream" |
|
224 |
||
225 |
^ collection |
|
226 |
! |
|
227 |
||
2432 | 228 |
peekForAll:aCollection |
229 |
"return true and advance if the next elements are the same |
|
230 |
as aCollection. |
|
231 |
otherwise stay and let the position unchanged" |
|
232 |
||
233 |
|oldPos| |
|
234 |
||
235 |
oldPos := self position. |
|
25408
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
236 |
(self nextAvailable:(aCollection size)) = aCollection ifTrue:[ |
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
237 |
^ true |
2432 | 238 |
]. |
239 |
self position:oldPos. |
|
240 |
^ false |
|
241 |
||
25408
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
242 |
"Created: / 01-03-1997 / 15:11:25 / cg" |
b3dc7dede514
Fix `PositionableStream >> #peekForAll:` w.r.t end of stream
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
243 |
"Modified: / 02-02-2021 / 08:53:11 / Jan Vrany <jan.vrany@labware.com>" |
2432 | 244 |
! |
245 |
||
3905
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
246 |
readLimit |
20423 | 247 |
"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
|
248 |
|
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
249 |
^ readLimit |
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 |
"Created: / 30.10.1998 / 16:47:04 / cg" |
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 |
|
1 | 254 |
readLimit:aNumber |
20423 | 255 |
"set the read-limit; that's the position at which EOF is returned" |
1 | 256 |
|
257 |
readLimit := aNumber |
|
369 | 258 |
! |
259 |
||
14934 | 260 |
setCollection:newCollection |
261 |
collection := newCollection |
|
262 |
! |
|
263 |
||
369 | 264 |
writeLimit:aNumber |
20423 | 265 |
"set the writeLimit; that's the position after which writing is prohibited" |
369 | 266 |
|
267 |
writeLimit := aNumber |
|
10592 | 268 |
|
269 |
" |
|
270 |
|s| |
|
271 |
s := WriteStream on:String new. |
|
272 |
s nextPutAll:'hello world'. |
|
273 |
s contents. |
|
274 |
||
275 |
|s| |
|
276 |
s := WriteStream on:String new. |
|
277 |
s writeLimit:5. |
|
278 |
s nextPutAll:'hello world'. |
|
279 |
s contents. |
|
280 |
||
281 |
" |
|
282 |
||
283 |
"Modified: / 04-06-2007 / 17:21:55 / cg" |
|
1 | 284 |
! ! |
285 |
||
17611 | 286 |
!PositionableStream methodsFor:'misc functions'! |
287 |
||
19890 | 288 |
copy:numberOfBytes into:aWriteStream |
289 |
"read from the receiver, and write numberOfBytes data to another aWriteStream. |
|
290 |
Return the number of bytes which have been transferred. |
|
291 |
Redefined here to avoid intermediate buffers/garbage." |
|
292 |
||
293 |
|endPosition cnt| |
|
294 |
||
295 |
collection notNil ifTrue:[ |
|
296 |
endPosition := (position+numberOfBytes) min:readLimit. |
|
297 |
aWriteStream nextPutAll:collection startingAt:position+1 to:endPosition. |
|
298 |
cnt := endPosition - position. |
|
299 |
position := endPosition. |
|
300 |
^ cnt. |
|
301 |
]. |
|
302 |
||
303 |
^ super copy:numberOfBytes into:aWriteStream. |
|
304 |
||
305 |
||
306 |
" |
|
307 |
'hello world' readStream copy:5 into:'/tmp/mist' asFilename writeStream. |
|
308 |
'hello world' readStream |
|
309 |
copy:5 into:Transcript; |
|
310 |
copy:20 into:Transcript. |
|
311 |
'hello world' readStream copy:5 into:'' writeStream inspect. |
|
312 |
#[1 2 3 4 5 6 7] readStream copy:2 into:'/tmp/mist' asFilename writeStream binary. |
|
313 |
#[1 2 3 4 5 6 7] readStream copy:3 into:#[] writeStream. |
|
314 |
" |
|
315 |
||
316 |
" |
|
317 |
|rs ws cnt| |
|
318 |
||
319 |
ws := #() writeStream. |
|
320 |
rs := #( 1 2 3 4 a nil true) readWriteStream. |
|
321 |
rs next. |
|
322 |
cnt := rs copyToEndInto:ws bufferSize:0. |
|
323 |
Transcript show:cnt; show:' '; showCR:ws contents. |
|
324 |
" |
|
325 |
! |
|
326 |
||
327 |
copy:numberOfBytes into:aWriteStream bufferSize:bufferSize |
|
328 |
"read from the receiver, and write numberOfBytes data to another aWriteStream. |
|
329 |
Return the number of bytes which have been transferred. |
|
330 |
Redefined here to avoid intermediate buffers/garbage. |
|
331 |
bufferSize does not matter here." |
|
332 |
||
333 |
collection notNil ifTrue:[ |
|
334 |
^ self copy:numberOfBytes into:aWriteStream. |
|
335 |
]. |
|
336 |
^ super copy:numberOfBytes into:aWriteStream bufferSize:bufferSize. |
|
337 |
! |
|
338 |
||
19403 | 339 |
copyToEndInto:aWriteStream |
17611 | 340 |
"read from the receiver, and write all data up to the end to another stream. |
341 |
Return the number of bytes which have been transferred. |
|
19403 | 342 |
Redefined here to avoid intermediate buffers/garbage." |
17611 | 343 |
|
344 |
|cnt| |
|
345 |
||
346 |
collection notNil ifTrue:[ |
|
347 |
aWriteStream nextPutAll:collection startingAt:position+1 to:readLimit. |
|
348 |
cnt := readLimit - position. |
|
349 |
position := readLimit. |
|
350 |
^ cnt. |
|
351 |
]. |
|
19403 | 352 |
^ super copyToEndInto:aWriteStream. |
353 |
||
354 |
" |
|
355 |
|rs ws cnt| |
|
356 |
||
357 |
ws := #() writeStream. |
|
358 |
rs := #( 1 2 3 4 a nil true) readWriteStream. |
|
359 |
rs next. |
|
360 |
cnt := rs copyToEndInto:ws bufferSize:0. |
|
361 |
Transcript show:cnt; show:' '; showCR:ws contents. |
|
362 |
" |
|
363 |
! |
|
364 |
||
365 |
copyToEndInto:aWriteStream bufferSize:bufferSize |
|
366 |
"read from the receiver, and write all data up to the end to another stream. |
|
367 |
Return the number of bytes which have been transferred. |
|
368 |
Redefined here to avoid intermediate buffers/garbage. |
|
369 |
bufferSize does not matter here." |
|
370 |
||
371 |
collection notNil ifTrue:[ |
|
372 |
^ self copyToEndInto:aWriteStream. |
|
373 |
]. |
|
17611 | 374 |
^ super copyToEndInto:aWriteStream bufferSize:bufferSize. |
375 |
||
376 |
" |
|
377 |
|rs ws cnt| |
|
378 |
||
379 |
ws := #() writeStream. |
|
380 |
rs := #( 1 2 3 4 a nil true) readWriteStream. |
|
381 |
rs next. |
|
382 |
cnt := rs copyToEndInto:ws bufferSize:0. |
|
383 |
Transcript show:cnt; show:' '; showCR:ws contents. |
|
384 |
" |
|
385 |
! ! |
|
386 |
||
21136 | 387 |
!PositionableStream methodsFor:'non homogenous reading'! |
388 |
||
389 |
nextBytes:numBytes into:aCollection startingAt:initialIndex |
|
390 |
"return the next numBytes from the stream. If the end is |
|
391 |
reached before, only that many bytes are copyied into the |
|
392 |
collection. |
|
393 |
Returns the number of bytes that have been actually read. |
|
394 |
The receiver must support reading of binary bytes. |
|
395 |
||
396 |
Notice: this method is provided here for protocol completeness |
|
397 |
with externalStreams - it is normally not used with other |
|
398 |
streams." |
|
399 |
||
400 |
|max| |
|
401 |
||
402 |
(collection isByteCollection |
|
403 |
and:[aCollection isByteCollection]) ifTrue:[ |
|
404 |
"do it the fast way" |
|
405 |
max := (readLimit - position) min: numBytes. |
|
406 |
aCollection |
|
407 |
replaceBytesFrom:initialIndex |
|
408 |
to:(initialIndex + max - 1) |
|
409 |
with:collection |
|
410 |
startingAt:position+1. |
|
411 |
position := position + max. |
|
412 |
^ max |
|
413 |
]. |
|
414 |
"do it the hard way" |
|
415 |
^ super nextBytes:numBytes into:aCollection startingAt:initialIndex |
|
416 |
||
417 |
" |
|
418 |
|s n buffer| |
|
419 |
||
420 |
buffer := ByteArray new:10. |
|
421 |
||
422 |
s := ReadStream on:#[1 2 3 4 5 6 7 8 9]. |
|
423 |
s next:3. |
|
424 |
n := s nextBytes:9 into:buffer startingAt:1. |
|
425 |
Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer) |
|
426 |
" |
|
427 |
! ! |
|
428 |
||
611 | 429 |
!PositionableStream methodsFor:'positioning'! |
430 |
||
431 |
backStep |
|
432 |
"move backward read position by one" |
|
433 |
||
4405 | 434 |
self position:(self position - 1) |
611 | 435 |
! |
436 |
||
16188 | 437 |
match: subCollection |
438 |
"Set the access position of the receiver to be past the next occurrence of the subCollection. |
|
439 |
Answer whether subCollection is found. |
|
440 |
No wildcards, and case does matter." |
|
441 |
||
442 |
| pattern startMatch | |
|
443 |
||
444 |
pattern := ReadStream on: subCollection. |
|
445 |
startMatch := nil. |
|
446 |
[pattern atEnd] whileFalse: [ |
|
447 |
self atEnd ifTrue: [^ false]. |
|
448 |
(self next) = (pattern next) |
|
449 |
ifTrue: [pattern position = 1 ifTrue: [startMatch := self position]] |
|
450 |
ifFalse: [ |
|
451 |
pattern position: 0. |
|
452 |
startMatch ifNotNil: [ |
|
453 |
self position: startMatch. |
|
454 |
startMatch := nil |
|
455 |
] |
|
456 |
] |
|
457 |
]. |
|
458 |
^ true |
|
459 |
||
460 |
" |
|
461 |
'abc def ghi' readStream match:'def'; upToEnd |
|
462 |
" |
|
463 |
! |
|
464 |
||
611 | 465 |
position |
15615 | 466 |
"return the read position (0-based)" |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
467 |
|
15348 | 468 |
^ position |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
469 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
470 |
|
15615 | 471 |
position0Based |
472 |
<resource: #obsolete> |
|
473 |
"return the read position 0-based" |
|
474 |
||
475 |
^ self position |
|
476 |
! |
|
477 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
478 |
position0Based:index0Based |
15615 | 479 |
<resource: #obsolete> |
4405 | 480 |
"set the read (or write) position" |
611 | 481 |
|
15615 | 482 |
self position:index0Based |
7052
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 |
|
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
485 |
position1Based |
15615 | 486 |
<resource: #obsolete> |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
487 |
"return the read position 1-based" |
611 | 488 |
|
15615 | 489 |
^ self position + 1 |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
490 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
491 |
|
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
492 |
position1Based:index1Based |
15615 | 493 |
<resource: #obsolete> |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
494 |
"set the read (or write) position" |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
495 |
|
15615 | 496 |
self position:(index1Based - 1) |
4405 | 497 |
|
498 |
" |
|
499 |
|s| |
|
500 |
||
501 |
s := '1234567890' readStream. |
|
502 |
s next:5. |
|
503 |
s position:1. |
|
504 |
s next:7. |
|
505 |
" |
|
506 |
||
507 |
" |
|
508 |
|s| |
|
509 |
||
510 |
s := '' writeStream. |
|
511 |
s nextPutAll:'1234567890'. |
|
512 |
s position:5. |
|
513 |
s nextPutAll:'abcdefg'. |
|
514 |
s contents |
|
515 |
" |
|
516 |
||
517 |
" |
|
518 |
|s| |
|
519 |
||
520 |
s := '' writeStream. |
|
521 |
s nextPutAll:'1234567890'. |
|
522 |
s position:0. |
|
523 |
s nextPutAll:'abcdefg'. |
|
524 |
s contents |
|
525 |
" |
|
611 | 526 |
! |
527 |
||
15615 | 528 |
position:index0Based |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
529 |
"set the read (or write) position" |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
530 |
|
15615 | 531 |
((index0Based > readLimit) or:[index0Based < 0]) ifTrue: [^ self positionError:index0Based]. |
532 |
position := index0Based |
|
6626
f7b08d6bee3f
#positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents:
6538
diff
changeset
|
533 |
! |
f7b08d6bee3f
#positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents:
6538
diff
changeset
|
534 |
|
611 | 535 |
reset |
536 |
"set the read position to the beginning of the collection" |
|
537 |
||
19885 | 538 |
position := 0. |
5308 | 539 |
|
540 |
" |
|
541 |
|s| |
|
542 |
||
543 |
s := 'hello world' readStream. |
|
544 |
Transcript showCR:(s next:5). |
|
545 |
s reset. |
|
546 |
Transcript showCR:(s next:10). |
|
547 |
" |
|
548 |
! |
|
549 |
||
550 |
resetPosition |
|
19885 | 551 |
<resource: #obsolete> |
5308 | 552 |
"set the read position to the beginning of the collection" |
553 |
||
15348 | 554 |
position := 0 |
4405 | 555 |
|
556 |
" |
|
557 |
|s| |
|
558 |
||
559 |
s := 'hello world' readStream. |
|
560 |
Transcript showCR:(s next:5). |
|
561 |
s reset. |
|
562 |
Transcript showCR:(s next:10). |
|
563 |
" |
|
611 | 564 |
! |
565 |
||
566 |
setToEnd |
|
11433 | 567 |
"set the read position to the end of the collection. |
568 |
#next will return EOF, #nextPut: will append to the stream. |
|
569 |
(same Behavior as FileStream." |
|
611 | 570 |
|
15348 | 571 |
position := readLimit |
611 | 572 |
! |
573 |
||
574 |
skip:numberToSkip |
|
575 |
"skip the next numberToSkip elements" |
|
576 |
||
5774 | 577 |
numberToSkip ~~ 0 ifTrue:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6924
diff
changeset
|
578 |
self position:(self position + numberToSkip) |
5774 | 579 |
] |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
580 |
! |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
581 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
582 |
skipThroughAll:aCollection |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
583 |
"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
|
584 |
return nil if not found, self otherwise. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
585 |
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
|
586 |
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
|
587 |
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
|
588 |
|
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
589 |
|buffer len first idx| |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
590 |
|
16186 | 591 |
self isPositionable ifFalse:[ |
592 |
"/ for non-positionable subclasses |
|
593 |
^ super skipThroughAll:aCollection |
|
594 |
]. |
|
595 |
||
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
596 |
len := aCollection size. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
597 |
first := aCollection at:1. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
598 |
[self atEnd] whileFalse:[ |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
599 |
buffer := self nextAvailable:len. |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
600 |
buffer size == len ifTrue:[ |
16209 | 601 |
buffer = aCollection ifTrue:[ |
602 |
^ self |
|
603 |
]. |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
604 |
"expect more input" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
605 |
idx := buffer indexOf:first startingAt:2. |
16209 | 606 |
idx ~~ 0 ifTrue:[ |
607 |
self skip:(idx - len - 1) |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
608 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
609 |
]. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
610 |
]. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
611 |
^ nil |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
612 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
613 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
614 |
|s| |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
615 |
s := ReadStream on:'12345678901234567890a'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
616 |
s skipThroughAll:'90ab'. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
617 |
s upToEnd |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
618 |
" |
2153
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
619 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
620 |
|s| |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
621 |
s := ReadStream on:'12345678901234567890'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
622 |
s skipThroughAll:'1234'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
623 |
s upToEnd |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
624 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
625 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
626 |
|s| |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
627 |
s := ReadStream on:'12345678901234567890'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
628 |
s skipThroughAll:'999'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
629 |
s atEnd |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
630 |
" |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
631 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
632 |
"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
|
633 |
"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
|
634 |
! |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
635 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
636 |
skipToAll:aCollection |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
637 |
"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
|
638 |
return nil if not found, self otherwise. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
639 |
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
|
640 |
|
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
641 |
|oldPos buffer len first idx| |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
642 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
643 |
oldPos := self position. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
644 |
len := aCollection size. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
645 |
first := aCollection at:1. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
646 |
[self atEnd] whileFalse:[ |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
647 |
buffer := self nextAvailable:len. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
648 |
buffer = aCollection ifTrue:[ |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
649 |
self position:(self position - len). |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
650 |
^ self |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
651 |
]. |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
652 |
buffer size == len ifTrue:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
653 |
"more input can be expected" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
654 |
idx := buffer indexOf:first startingAt:2. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
655 |
idx == 0 ifFalse:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
656 |
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
|
657 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
658 |
]. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
659 |
]. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
660 |
self position:oldPos. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
661 |
^ nil |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
662 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
663 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
664 |
|s| |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
665 |
s := ReadStream on:'12345678901234567890'. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
666 |
s skipToAll:'901'. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
667 |
s upToEnd |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
668 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
669 |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
670 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
671 |
|s| |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
672 |
s := ReadStream on:'1234567890'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
673 |
s skipToAll:'901'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
674 |
s upToEnd |
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 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
677 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
678 |
|s| |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
679 |
s := 'Makefile' asFilename readStream. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
680 |
[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
681 |
(s skipToAll:'EDIT') notNil ifTrue:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
682 |
s next:100. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
683 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
684 |
] ensure:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
685 |
s close. |
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 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
688 |
|
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
689 |
"Modified: 26.6.1996 / 09:28:27 / cg" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
690 |
"Created: 26.6.1996 / 09:35:06 / cg" |
44 | 691 |
! ! |
611 | 692 |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
693 |
!PositionableStream methodsFor:'printing & storing'! |
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 |
printOn:aStream |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
696 |
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
|
697 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
698 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
699 |
'' readStream printString |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
700 |
'' writeStream printString |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
701 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
702 |
! ! |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
703 |
|
611 | 704 |
!PositionableStream methodsFor:'private'! |
705 |
||
706 |
contentsSpecies |
|
707 |
"return a class of which instances will be returned, when |
|
708 |
parts of the collection are asked for. |
|
15640 | 709 |
(see upTo-kind of methods in Stream)" |
611 | 710 |
|
711 |
^ collection species |
|
712 |
! |
|
713 |
||
714 |
on:aCollection |
|
715 |
"setup for streaming on aCollection" |
|
716 |
||
717 |
collection := aCollection. |
|
718 |
readLimit := aCollection size. |
|
13379 | 719 |
readLimit == 0 ifTrue:[ |
720 |
self assert:(aCollection isCollection) |
|
721 |
]. |
|
15348 | 722 |
position := 0 |
611 | 723 |
! |
724 |
||
725 |
on:aCollection from:first to:last |
|
726 |
"setup for streaming on aCollection from first to last" |
|
727 |
||
728 |
collection := aCollection. |
|
15348 | 729 |
position := first - 1. |
611 | 730 |
readLimit := last |
731 |
! |
|
732 |
||
733 |
positionError |
|
1998 | 734 |
"{ Pragma: +optSpace }" |
735 |
||
2966 | 736 |
"report an error when positioning past the end |
737 |
or before the beginning." |
|
611 | 738 |
|
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
739 |
^ PositionOutOfBoundsError raiseRequestWith:nil |
4452
3d050f505a0a
Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents:
4416
diff
changeset
|
740 |
|
3d050f505a0a
Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents:
4416
diff
changeset
|
741 |
"Modified: / 26.7.1999 / 10:59:13 / stefan" |
611 | 742 |
! |
743 |
||
6314 | 744 |
positionError:badPostition |
745 |
"{ Pragma: +optSpace }" |
|
746 |
||
747 |
"report an error when positioning past the end |
|
748 |
or before the beginning." |
|
749 |
||
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
750 |
^ PositionOutOfBoundsError raiseRequestWith:badPostition |
6314 | 751 |
! |
752 |
||
611 | 753 |
with:aCollection |
754 |
"setup for streaming to the end of aCollection" |
|
755 |
||
756 |
collection := aCollection. |
|
11434 | 757 |
readLimit := aCollection size. |
611 | 758 |
self setToEnd |
11434 | 759 |
|
760 |
" |
|
761 |
(WriteStream with:#(1 2 3 4 5)) |
|
762 |
nextPut:6; |
|
763 |
contents |
|
764 |
" |
|
611 | 765 |
! ! |
766 |
||
16195 | 767 |
!PositionableStream methodsFor:'queries'! |
768 |
||
16209 | 769 |
endsBeforePositionWith:aSequenceableCollection |
16195 | 770 |
"answer true, if the elements in aSequenceableCollection |
16209 | 771 |
are at the current end of the stream up to position." |
16195 | 772 |
|
16209 | 773 |
|sz pos| |
16195 | 774 |
|
16209 | 775 |
sz := aSequenceableCollection size. |
776 |
pos := self position. |
|
777 |
pos < sz ifTrue:[ |
|
778 |
^ false. |
|
16195 | 779 |
]. |
16209 | 780 |
self contentsSpecies == collection class ifTrue:[ |
781 |
^ collection sameContentsFrom:pos+1-sz to:pos as:aSequenceableCollection startingAt:1. |
|
782 |
]. |
|
783 |
self position:pos-sz. |
|
784 |
^ (self next:sz) = aSequenceableCollection. |
|
16195 | 785 |
|
786 |
" |
|
16209 | 787 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'World' |
788 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello World' |
|
789 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello Worldx' |
|
790 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Bla' |
|
791 |
('' writeStream) endsBeforePositionWith:'Bla' |
|
792 |
('' writeStream) endsBeforePositionWith:'' |
|
16195 | 793 |
'' endsWith:'' |
794 |
" |
|
795 |
! ! |
|
796 |
||
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
797 |
!PositionableStream methodsFor:'reading'! |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
798 |
|
9003 | 799 |
nextAvailable:count |
17606 | 800 |
collection notNil ifTrue:[ |
801 |
|end result| |
|
9003 | 802 |
|
17606 | 803 |
end := position + count. |
804 |
end >= readLimit ifTrue:[ |
|
805 |
end := readLimit. |
|
806 |
]. |
|
807 |
||
808 |
result := collection copyFrom:position+1 to:end. |
|
809 |
position := end. |
|
810 |
^ result. |
|
9003 | 811 |
]. |
812 |
||
17606 | 813 |
^ super nextAvailable:count. |
9003 | 814 |
|
815 |
" |
|
816 |
'abc' readStream nextAvailable:1. |
|
817 |
'abc' readStream nextAvailable:2. |
|
818 |
'abc' readStream nextAvailable:3. |
|
819 |
'abc' readStream nextAvailable:4. |
|
820 |
||
821 |
'abc' readStream nextAvailable:2; nextAvailable:2. |
|
822 |
'abc' readStream nextAvailable:3; nextAvailable:3. |
|
823 |
" |
|
824 |
! |
|
825 |
||
17611 | 826 |
nextAvailable:count into:aCollection startingAt:initialIndex |
827 |
"return the next count objects from the stream. If the end is |
|
828 |
reached before, only that many objects are copyied into the |
|
829 |
collection. |
|
830 |
Returns the number of objects that have been actually read." |
|
831 |
||
832 |
| max | |
|
833 |
||
834 |
collection notNil ifTrue:[ |
|
835 |
max := (readLimit - position) min: count. |
|
836 |
aCollection |
|
837 |
replaceFrom: initialIndex |
|
838 |
to: initialIndex+max-1 |
|
839 |
with: collection |
|
840 |
startingAt: position+1. |
|
841 |
position := position + max. |
|
842 |
^ max. |
|
843 |
]. |
|
844 |
||
845 |
^ super nextAvailable:count 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 |
||
21136 | 859 |
peek |
860 |
"look ahead for and return the next element" |
|
17611 | 861 |
|
21136 | 862 |
|peekObject| |
20691 | 863 |
|
21136 | 864 |
peekObject := self next. |
865 |
self backStep. |
|
866 |
^ peekObject |
|
20691 | 867 |
! |
868 |
||
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
869 |
upToAll:aCollection |
16186 | 870 |
"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
|
871 |
Return everything read excluding the elements in aCollection. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
872 |
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
|
873 |
read operations will return those elements. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
874 |
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
|
875 |
are read and returned. |
6494 | 876 |
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
|
877 |
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
|
878 |
collection. |
6494 | 879 |
See also #upToAllExcluding:, which returns the same, but leaves the |
880 |
read pointer after the matched subcollection. |
|
3184
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
881 |
|
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
882 |
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
|
883 |
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
|
884 |
this way for the sake of ST80-compatibility." |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
885 |
|
17611 | 886 |
"/ in the future, this will have the Squeak semantics, which is upToAllExcluding: |
16189 | 887 |
|
888 |
^ self upToAll_positionBefore:aCollection |
|
889 |
||
890 |
" |
|
891 |
|s| |
|
892 |
s := ReadStream on:'hello world'. |
|
893 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
894 |
Transcript showCR:s atEnd. |
|
895 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
|
896 |
" |
|
897 |
" |
|
898 |
|s| |
|
899 |
s := ReadStream on:'hello world'. |
|
900 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
901 |
Transcript showCR:s atEnd. |
|
902 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
903 |
" |
|
904 |
" |
|
905 |
|s| |
|
906 |
s := ReadStream on:'hello world'. |
|
907 |
Transcript show:'<'; show:(s upToAll:'xx'); showCR:'>'. |
|
908 |
Transcript showCR:s atEnd. |
|
909 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
|
910 |
" |
|
911 |
||
912 |
"Modified: / 12.1.1998 / 22:06:42 / cg" |
|
913 |
"Created: / 12.1.1998 / 22:07:01 / cg" |
|
914 |
! |
|
915 |
||
916 |
upToAll_positionBefore:aCollection |
|
917 |
"read until a subcollection consisisting of the elements in aCollection is encountered. |
|
918 |
Return everything read excluding the elements in aCollection. |
|
919 |
The position is left before the collection; i.e. the next |
|
920 |
read operations will return those elements. |
|
921 |
If no such subcollection is encountered, all elements up to the end |
|
922 |
are read and returned. |
|
923 |
See also #throughAll: which also reads up to some objects |
|
924 |
but positions behind it and DOES include it in the returned |
|
16840 | 925 |
collection." |
16189 | 926 |
|
16209 | 927 |
|answerStream element last| |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
928 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
929 |
last := aCollection last. |
16209 | 930 |
answerStream := WriteStream on:(self contentsSpecies new:100). |
931 |
[(element := self nextOrNil) notNil] whileTrue:[ |
|
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
932 |
answerStream nextPut:element. |
16209 | 933 |
(element = last and:[answerStream endsBeforePositionWith:aCollection]) ifTrue:[ |
934 |
|backStep| |
|
935 |
backStep := aCollection size negated. |
|
936 |
self skip:backStep. |
|
937 |
answerStream skip:backStep. |
|
938 |
^ answerStream contents |
|
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
939 |
]. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
940 |
]. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
941 |
^ answerStream contents |
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 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
944 |
|s| |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
945 |
s := ReadStream on:'hello world'. |
16209 | 946 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
6494 | 947 |
Transcript showCR:s atEnd. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
948 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
949 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
950 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
951 |
|s| |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
952 |
s := ReadStream on:'hello world'. |
16209 | 953 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
16186 | 954 |
Transcript showCR:s atEnd. |
16209 | 955 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
16186 | 956 |
" |
957 |
" |
|
958 |
|s| |
|
959 |
s := ReadStream on:'hello world'. |
|
16209 | 960 |
Transcript show:'<'; show:(s upToAll_positionBefore:'xx'); showCR:'>'. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
961 |
Transcript showCR:s atEnd. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
962 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
963 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
964 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
965 |
"Modified: / 12.1.1998 / 22:06:42 / cg" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
966 |
"Created: / 12.1.1998 / 22:07:01 / cg" |
20101 | 967 |
! |
968 |
||
969 |
upToEnd |
|
970 |
collection notNil ifTrue:[ |
|
971 |
|result| |
|
972 |
||
973 |
result := collection copyFrom:position+1 to:readLimit. |
|
974 |
position := readLimit. |
|
975 |
^ result. |
|
976 |
]. |
|
977 |
||
978 |
^ super upToEnd. |
|
979 |
||
980 |
" |
|
981 |
'abc' readStream upToEnd. |
|
982 |
" |
|
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
983 |
! ! |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
984 |
|
21136 | 985 |
!PositionableStream methodsFor:'reading-strings'! |
986 |
||
987 |
nextLine |
|
988 |
"return the characters upTo (but excluding) the next cr (carriage return) |
|
989 |
character (i.e. read a single line of text). |
|
990 |
If the previous-to-last character is a cr, this is also removed, |
|
991 |
so it's possible to read alien (i.e. ms-dos) text as well. |
|
992 |
Added for protocol compatibility with externalStreams." |
|
993 |
||
994 |
|start "{ Class:SmallInteger }" |
|
995 |
end "{ Class:SmallInteger }"| |
|
996 |
||
997 |
collection isString ifTrue:[ |
|
998 |
position == readLimit ifTrue:[ |
|
999 |
^ self pastEndRead |
|
1000 |
]. |
|
1001 |
start := position+1. |
|
1002 |
end := collection indexOf:Character cr startingAt:start. |
|
1003 |
||
1004 |
(end == 0 or:[end > readLimit]) ifTrue:[ |
|
1005 |
end := position := readLimit. |
|
1006 |
] ifFalse:[ |
|
1007 |
position := end. |
|
1008 |
end := end - 1. "skip lf" |
|
1009 |
]. |
|
1010 |
start > end ifTrue:[ |
|
1011 |
^ ''. |
|
1012 |
]. |
|
1013 |
(collection at:end) == Character return ifTrue:[ |
|
1014 |
end := end - 1. "skip return" |
|
1015 |
]. |
|
1016 |
^ collection copyFrom:start to:end. |
|
1017 |
]. |
|
1018 |
^ super nextLine. |
|
1019 |
||
1020 |
" |
|
1021 |
'12345678' readStream nextLine |
|
1022 |
'12345678' allBold readStream nextLine |
|
1023 |
'12\34\56\78' withCRs readStream nextLine |
|
1024 |
'12\34\56\78' withCRs readStream nextLine; nextLine |
|
1025 |
(ReadStream on:('12\34\56\78' withCRs) from:1 to:4) nextLine; nextLine |
|
1026 |
('12\' withCRs, Character return, '34') readStream nextLine; nextLine |
|
1027 |
Character cr asString readStream nextLine |
|
1028 |
Character return asString readStream nextLine |
|
1029 |
(Character return, Character cr) asString readStream nextLine |
|
1030 |
Character return asString readStream nextLine; nextLine |
|
1031 |
" |
|
1032 |
! ! |
|
1033 |
||
611 | 1034 |
!PositionableStream methodsFor:'testing'! |
1035 |
||
1036 |
atEnd |
|
1037 |
"return true, if the read-position is at the end" |
|
1038 |
||
15357 | 1039 |
^ position >= readLimit |
611 | 1040 |
! |
1041 |
||
1042 |
isEmpty |
|
1043 |
"return true, if the contents of the stream is empty" |
|
1044 |
||
1045 |
^ readLimit == 0 |
|
9006 | 1046 |
! |
1047 |
||
10577 | 1048 |
isInternalByteStream |
1049 |
"return true, if the stream is an internal stream reading bytes" |
|
1050 |
||
1051 |
^ collection class isBytes |
|
1052 |
||
1053 |
"Created: / 30-05-2007 / 16:16:12 / cg" |
|
1054 |
! |
|
1055 |
||
9006 | 1056 |
isPositionable |
1057 |
"return true, if the stream supports positioning (this one is)" |
|
1058 |
||
1059 |
^ true |
|
611 | 1060 |
! ! |
1061 |
||
1897 | 1062 |
!PositionableStream class methodsFor:'documentation'! |
701 | 1063 |
|
1064 |
version |
|
19403 | 1065 |
^ '$Header$' |
13379 | 1066 |
! |
1067 |
||
1068 |
version_CVS |
|
19403 | 1069 |
^ '$Header$' |
18260
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1070 |
! |
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1071 |
|
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1072 |
version_HG |
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1073 |
|
e55af242e134
Added Squeak compatibility methods PositionableStream>>#upToAnyOf: / #upToAnyOf:do:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
1074 |
^ '$Changeset: <not expanded> $' |
701 | 1075 |
! ! |
6777
5efddf5358b2
currentFileInDirectory query
Claus Gittinger <cg@exept.de>
parents:
6707
diff
changeset
|
1076 |
|
14934 | 1077 |
|
611 | 1078 |
PositionableStream initialize! |