author | Claus Gittinger <cg@exept.de> |
Wed, 07 May 2003 16:15:41 +0200 | |
changeset 7260 | edfa8d6a6046 |
parent 7113 | 23182ef346a5 |
child 7261 | f35fc9cee675 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
252 | 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 |
" |
|
12 |
||
5556 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
PositionableStream subclass:#WriteStream |
1295 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Streams' |
|
1 | 20 |
! |
21 |
||
1965 | 22 |
!WriteStream class methodsFor:'documentation'! |
1 | 23 |
|
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
252 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
68 | 38 |
documentation |
39 |
" |
|
2883 | 40 |
Streams for writing into. |
41 |
WriteStreams are especially useful, if big strings are to be constructed |
|
42 |
from pieces - create a writeStream, add the pieces (with #nextPut or |
|
43 |
#nextPutAll) and finally fetch the concatenated string via #contents. |
|
44 |
This is much better than constructing the big string by concatenating via |
|
45 |
the comma (,) operator, since less intermediate garbage objects are created. |
|
46 |
||
47 |
This implementation currently DOES change the |
|
261 | 48 |
identity if the streamed-upon collection IF it cannot grow easily. |
49 |
Collections which cannot grow easily are for example: Array, ByteArray and String. |
|
88 | 50 |
Thus it is slightly incompatible to ST-80 since 'aStream contents' does |
51 |
not always return the original collection. This may change. |
|
1295 | 52 |
|
53 |
[author:] |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
54 |
Claus Gittinger |
68 | 55 |
" |
2883 | 56 |
! |
57 |
||
58 |
examples |
|
59 |
" |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
60 |
[exBegin] |
2883 | 61 |
|s| |
62 |
||
63 |
s := WriteStream on:''. |
|
64 |
s nextPutAll:'hello'; |
|
65 |
space; |
|
66 |
nextPutAll:'world'. |
|
67 |
||
68 |
s contents inspect |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
69 |
[exEnd] |
2883 | 70 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
71 |
[exBegin] |
2883 | 72 |
|s| |
73 |
||
74 |
s := WriteStream on:''. |
|
75 |
s nextPutAll:'hello'; |
|
76 |
space; |
|
77 |
nextPutAll:'world'. |
|
78 |
||
79 |
Transcript nextPutLine:(s contents) |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
80 |
[exEnd] |
2883 | 81 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
82 |
[exBegin] |
2883 | 83 |
|s| |
84 |
||
85 |
s := '' writeStream. |
|
86 |
s nextPutAll:'hello'; |
|
87 |
space; |
|
88 |
nextPutAll:'world'. |
|
89 |
||
90 |
Transcript nextPutLine:(s contents) |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
91 |
[exEnd] |
2883 | 92 |
" |
93 |
||
345 | 94 |
! ! |
95 |
||
6325 | 96 |
!WriteStream methodsFor:'Compatibility - Dolphin'! |
97 |
||
98 |
display:someObject |
|
6871
1c4a799a048c
display: use printOn instead of printString.
Claus Gittinger <cg@exept.de>
parents:
6326
diff
changeset
|
99 |
someObject printOn:self. |
1c4a799a048c
display: use printOn instead of printString.
Claus Gittinger <cg@exept.de>
parents:
6326
diff
changeset
|
100 |
"/ self nextPutAll:someObject printString |
6325 | 101 |
! ! |
102 |
||
1 | 103 |
!WriteStream methodsFor:'accessing'! |
104 |
||
105 |
contents |
|
345 | 106 |
"return the current contents (a collection) of the stream. |
107 |
Currently, this returns the actual collection if possible |
|
108 |
(and reset is implemented to create a new one) in contrast to |
|
109 |
ST80, where contents returns a copy and reset only sets the writePointer. |
|
110 |
The ST/X behavior creates less temporary garbage in the normal case |
|
2410 | 111 |
(where things are written for the contents only) but may be incompatible |
345 | 112 |
with some applications. Time will show, if this is to be changed." |
1 | 113 |
|
260 | 114 |
|lastIndex| |
115 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
116 |
lastIndex := position - ZeroPosition. |
260 | 117 |
collection size == lastIndex ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
118 |
collection isFixedSize ifTrue:[ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
119 |
" |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
120 |
grow is expensive - return a copy. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
121 |
(is this what users of writeStream expect ?) |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
122 |
" |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
123 |
collection := collection copyFrom:1 to:lastIndex |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
124 |
] ifFalse:[ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
125 |
collection grow:lastIndex |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
126 |
] |
1 | 127 |
]. |
128 |
^ collection |
|
2410 | 129 |
|
3087 | 130 |
"Modified: / 19.2.1997 / 08:57:28 / stefan" |
131 |
"Modified: / 30.10.1997 / 16:21:23 / cg" |
|
1 | 132 |
! |
133 |
||
5888 | 134 |
last |
135 |
"return the last element - report an error if the stream is empty" |
|
136 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
137 |
|position1Based| |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
138 |
|
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
139 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
140 |
^ collection at:(position1Based - 1). |
5888 | 141 |
|
142 |
" |
|
143 |
|s| |
|
144 |
||
145 |
s := '' writeStream. |
|
146 |
s nextPut:$a. |
|
147 |
s last. |
|
148 |
s nextPut:$b. |
|
149 |
s last. |
|
150 |
s nextPut:$c. |
|
151 |
s last. |
|
152 |
" |
|
153 |
! |
|
154 |
||
155 |
last:n |
|
156 |
"return the last n elements as species of the underlying collection; |
|
157 |
Report an error if the stream is empty" |
|
158 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
159 |
|position1Based| |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
160 |
|
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
161 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
162 |
^ collection copyFrom:(position1Based - n) to:(position1Based - 1). |
5888 | 163 |
|
164 |
" |
|
165 |
|s| |
|
166 |
||
167 |
s := '' writeStream. |
|
168 |
s nextPut:$a. |
|
169 |
s last:1. |
|
170 |
s nextPut:$b. |
|
171 |
s last:1. |
|
172 |
s last:2. |
|
173 |
s nextPut:$c. |
|
174 |
s last:1. |
|
175 |
s last:2. |
|
176 |
s last:3. |
|
177 |
" |
|
178 |
! |
|
179 |
||
329 | 180 |
reset |
345 | 181 |
"reset the stream; write anew. |
2410 | 182 |
See the comment in WriteStream>>contents" |
329 | 183 |
|
184 |
collection := collection species new:(collection size). |
|
185 |
super reset |
|
2410 | 186 |
|
187 |
"Modified: 19.2.1997 / 08:57:00 / stefan" |
|
1 | 188 |
! ! |
189 |
||
611 | 190 |
!WriteStream methodsFor:'positioning'! |
191 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
192 |
position0Based:index0Based |
611 | 193 |
"redefined to allow positioning past the readLimit" |
194 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
195 |
((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError]. |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
196 |
position := index0Based + ZeroPosition |
611 | 197 |
! ! |
198 |
||
199 |
!WriteStream methodsFor:'private'! |
|
200 |
||
201 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
202 |
"grow the streamed collection to at least 10 elements" |
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
203 |
|
611 | 204 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
205 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
206 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 207 |
! |
208 |
||
209 |
growCollection:minNewSize |
|
210 |
"grow the streamed collection to at least minNewSize" |
|
211 |
||
212 |
|oldSize newSize newColl| |
|
213 |
||
214 |
oldSize := collection size. |
|
215 |
(oldSize == 0) ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
216 |
newSize := minNewSize |
611 | 217 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
218 |
newSize := oldSize * 2. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
219 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 220 |
]. |
221 |
collection isFixedSize ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
222 |
newColl := collection species new:newSize. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
223 |
newColl replaceFrom:1 to:oldSize with:collection startingAt:1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
224 |
collection := newColl |
611 | 225 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
226 |
collection grow:newSize |
611 | 227 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
228 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
229 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
611 | 230 |
! ! |
231 |
||
7260 | 232 |
!WriteStream methodsFor:'private-accessing'! |
611 | 233 |
|
234 |
on:aCollection from:start to:last |
|
235 |
"create and return a new stream for writing onto aCollection, where |
|
236 |
writing is limited to the elements in the range start to last." |
|
237 |
||
238 |
super on:aCollection from:start to:last. |
|
239 |
writeLimit := last. |
|
240 |
! ! |
|
241 |
||
242 |
!WriteStream methodsFor:'queries'! |
|
243 |
||
3098
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
244 |
isReadable |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
245 |
"return true if the receiver supports reading - thats not true" |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
246 |
|
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
247 |
^ false |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
248 |
|
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
249 |
"Created: / 8.11.1997 / 14:06:07 / cg" |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
250 |
! |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
251 |
|
611 | 252 |
isWritable |
1405 | 253 |
"return true, if writing is supported by the recevier. |
254 |
Always return true here" |
|
255 |
||
611 | 256 |
^ true |
1405 | 257 |
|
258 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
3031 | 259 |
! |
260 |
||
261 |
size |
|
262 |
"return the current size" |
|
263 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
264 |
|position1Based| |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
265 |
|
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
266 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
267 |
^ position1Based - 1. |
3031 | 268 |
|
269 |
"Created: 14.10.1997 / 20:43:49 / cg" |
|
611 | 270 |
! ! |
271 |
||
63 | 272 |
!WriteStream methodsFor:'reading'! |
1 | 273 |
|
274 |
next |
|
10 | 275 |
"catch read access to write stream - report an error" |
1 | 276 |
|
10 | 277 |
self shouldNotImplement |
1 | 278 |
! |
279 |
||
280 |
peek |
|
10 | 281 |
"catch read access to write stream - report an error" |
1 | 282 |
|
10 | 283 |
self shouldNotImplement |
63 | 284 |
! ! |
285 |
||
5556 | 286 |
!WriteStream methodsFor:'testing'! |
287 |
||
288 |
isEmpty |
|
289 |
"return true, if the contents of the stream is empty" |
|
290 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
291 |
^ position == ZeroPosition |
5556 | 292 |
|
293 |
"Created: 14.10.1997 / 20:44:37 / cg" |
|
294 |
! ! |
|
295 |
||
611 | 296 |
!WriteStream methodsFor:'writing'! |
369 | 297 |
|
611 | 298 |
next:count put:anObject |
299 |
"append anObject count times to the receiver. |
|
300 |
Redefined to avoid count grows of the underlying collection - |
|
301 |
instead a single grow on the final size is performed." |
|
302 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
303 |
|final position1Based| |
611 | 304 |
|
305 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
|
7113 | 306 |
^ super next:count put:anObject |
611 | 307 |
]. |
369 | 308 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
309 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
310 |
final := position1Based + count - 1. |
611 | 311 |
(final > collection size) ifTrue:[ |
7113 | 312 |
self growCollection:final |
611 | 313 |
]. |
369 | 314 |
|
7113 | 315 |
position1Based to:final do:[:index | |
316 |
collection at:index put:anObject. |
|
611 | 317 |
]. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
318 |
position1Based := position1Based + count. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
319 |
(position1Based > readLimit) ifTrue:[readLimit := position1Based - 1]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
320 |
position := position1Based - 1 + ZeroPosition. |
611 | 321 |
^ anObject |
322 |
! |
|
1 | 323 |
|
5198
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
324 |
next:n putAll:aCollection startingAt:pos1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
325 |
"append some elements of the argument, aCollection to the stream." |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
326 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
327 |
^ self nextPutAll:aCollection startingAt:pos1 to:pos1+n-1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
328 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
329 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
330 |
|s| |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
331 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
332 |
s := '' writeStream. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
333 |
s nextPutAll:'hello '. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
334 |
s next:5 putAll:'1234world012345' startingAt:5. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
335 |
s contents |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
336 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
337 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
338 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
339 |
! |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
340 |
|
1 | 341 |
nextPut:anObject |
1584 | 342 |
"append the argument, anObject to the stream. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
343 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 344 |
|
63 | 345 |
%{ /* NOCONTEXT */ |
346 |
||
347 |
REGISTER int pos; |
|
348 |
unsigned ch; |
|
349 |
OBJ coll; |
|
2883 | 350 |
OBJ p, wL, rL; |
1584 | 351 |
int __readLimit = -1; |
63 | 352 |
|
1136 | 353 |
coll = __INST(collection); |
354 |
p = __INST(position); |
|
63 | 355 |
|
2894 | 356 |
#ifndef NO_PRIM_STREAM |
293 | 357 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
358 |
pos = __intVal(p); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
359 |
/* make 1-based */ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
360 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
361 |
wL = __INST(writeLimit); |
63 | 362 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
363 |
if ((wL == nil) |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
364 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
365 |
OBJ cls; |
252 | 366 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
367 |
cls = __qClass(coll); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
368 |
|
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
369 |
rL = __INST(readLimit); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
370 |
if (__isSmallInteger(rL)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
371 |
__readLimit = __intVal(rL); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
372 |
} |
293 | 373 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
374 |
if (cls == @global(String)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
375 |
if (__isCharacter(anObject) |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
376 |
&& ((ch = __intVal(__characterVal(anObject))) <= 255) /* ch is unsigned */ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
377 |
&& (pos <= __stringSize(coll))) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
378 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
379 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
380 |
__INST(readLimit) = __MKSMALLINT(pos); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
381 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
382 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
383 |
RETURN ( anObject ); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
384 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
385 |
} else if (cls == @global(ByteArray)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
386 |
if (__isSmallInteger(anObject) |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
387 |
&& ((ch = __intVal(anObject)) <= 255) /* ch is unsigned */ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
388 |
&& (pos <= __byteArraySize(coll))) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
389 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
390 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
391 |
__INST(readLimit) = __MKSMALLINT(pos); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
392 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
393 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
394 |
RETURN ( anObject ); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
395 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
396 |
} else if (cls == @global(Array)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
397 |
if (pos <= __arraySize(coll)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
398 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
399 |
__STORE(coll, anObject); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
400 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
401 |
__INST(readLimit) = __MKSMALLINT(pos); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
402 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
403 |
__INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
404 |
RETURN ( anObject ); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
405 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
406 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
407 |
} |
63 | 408 |
} |
2894 | 409 |
#endif |
260 | 410 |
%}. |
293 | 411 |
(writeLimit isNil |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
412 |
or:[(position + 1 - ZeroPosition) <= writeLimit]) ifTrue:[ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
413 |
((position + 1 - ZeroPosition) > collection size) ifTrue:[self growCollection]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
414 |
collection at:(position + 1 - ZeroPosition) put:anObject. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
415 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[readLimit := (position + 1 - ZeroPosition)]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
416 |
position := position + 1. |
838 | 417 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
418 |
WriteErrorSignal raiseErrorString:'write beyond writeLimit' |
293 | 419 |
]. |
1 | 420 |
^anObject |
421 |
! |
|
422 |
||
423 |
nextPutAll:aCollection |
|
63 | 424 |
"append all elements of the argument, aCollection to the stream. |
425 |
Redefined to avoid count grows of the underlying collection - |
|
426 |
instead a single grow on the final size is performed." |
|
1 | 427 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
428 |
|nMore final position1Based| |
1 | 429 |
|
77 | 430 |
collection isNil ifTrue:[ |
260 | 431 |
^ super nextPutAll:aCollection |
77 | 432 |
]. |
433 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
434 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
435 |
|
1 | 436 |
nMore := aCollection size. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
437 |
final := position1Based + nMore - 1. |
293 | 438 |
(writeLimit notNil |
439 |
and:[final > writeLimit]) ifTrue:[ |
|
440 |
final := writeLimit. |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
441 |
nMore := final - position1Based + 1 |
293 | 442 |
]. |
1 | 443 |
(final > collection size) ifTrue:[ |
252 | 444 |
self growCollection:final |
1 | 445 |
]. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
446 |
collection replaceFrom:position1Based |
252 | 447 |
to:final |
448 |
with:aCollection |
|
449 |
startingAt:1. |
|
1 | 450 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
451 |
position1Based := position1Based + nMore. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
452 |
(position1Based > readLimit) ifTrue:[readLimit := position1Based - 1]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
453 |
position := position1Based - 1 + ZeroPosition. |
1 | 454 |
^ aCollection |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
455 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
456 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
457 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
458 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
459 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
460 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
461 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
462 |
|nMore final position1Based| |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
463 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
464 |
collection isNil ifTrue:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
465 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
466 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
467 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
468 |
position1Based := position - ZeroPosition + 1. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
469 |
nMore := pos2 - pos1 + 1. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
470 |
final := position1Based + nMore - 1. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
471 |
(writeLimit notNil |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
472 |
and:[final > writeLimit]) ifTrue:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
473 |
final := writeLimit. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
474 |
nMore := final - position1Based + 1 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
475 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
476 |
(final > collection size) ifTrue:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
477 |
self growCollection:final |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
478 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
479 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
480 |
collection replaceFrom:position1Based |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
481 |
to:final |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
482 |
with:aCollection |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
483 |
startingAt:pos1. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
484 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
485 |
position1Based := position1Based + nMore. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
486 |
(position1Based > readLimit) ifTrue:[readLimit := position1Based - 1]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
487 |
position := position1Based - 1 + ZeroPosition. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
488 |
^ aCollection |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
489 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
490 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
491 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
492 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
493 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
494 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
495 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
496 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
497 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
498 |
|
1541 | 499 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
1 | 500 |
! ! |
501 |
||
1965 | 502 |
!WriteStream class methodsFor:'documentation'! |
701 | 503 |
|
504 |
version |
|
7260 | 505 |
^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.52 2003-05-07 14:14:43 cg Exp $' |
701 | 506 |
! ! |