author | Jan Vrany <jan.vrany@labware.com> |
Wed, 23 Jun 2021 12:50:05 +0100 | |
branch | jv |
changeset 25430 | acd92449dc30 |
parent 21242 | 19fabe339f8b |
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 |
" |
|
5556 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
17305 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
PositionableStream subclass:#WriteStream |
1295 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Streams' |
|
1 | 21 |
! |
22 |
||
1965 | 23 |
!WriteStream class methodsFor:'documentation'! |
1 | 24 |
|
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
252 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
68 | 39 |
documentation |
40 |
" |
|
2883 | 41 |
Streams for writing into. |
42 |
WriteStreams are especially useful, if big strings are to be constructed |
|
16201 | 43 |
from pieces - create a writeStream, add the pieces (with #nextPut: or |
44 |
#nextPutAll:) and finally fetch the concatenated string via #contents. |
|
2883 | 45 |
This is much better than constructing the big string by concatenating via |
46 |
the comma (,) operator, since less intermediate garbage objects are created. |
|
47 |
||
18294 | 48 |
This implementation currently DOES change the |
20271 | 49 |
identity of the streamed-upon collection IF it cannot grow easily. |
261 | 50 |
Collections which cannot grow easily are for example: Array, ByteArray and String. |
18294 | 51 |
Thus it is slightly incompatible to ST-80 since 'aStream contents' does |
88 | 52 |
not always return the original collection. This may change. |
1295 | 53 |
|
17557 | 54 |
[caveat:] |
20271 | 55 |
Basing capabilities like readability/writability/positionability/peekability on inheritance makes |
56 |
the class hierarchy ugly and leads to strange and hard to teach redefinitions (aka. NonPositionableStream |
|
57 |
below PositionableStream or ExternalReadStream under WriteStream) |
|
17557 | 58 |
|
1295 | 59 |
[author:] |
20271 | 60 |
Claus Gittinger |
10281 | 61 |
|
62 |
[see also:] |
|
20271 | 63 |
CharacterWriteStream (if streaming for a unicode string) |
68 | 64 |
" |
2883 | 65 |
! |
66 |
||
67 |
examples |
|
68 |
" |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
69 |
[exBegin] |
2883 | 70 |
|s| |
71 |
||
72 |
s := WriteStream on:''. |
|
73 |
s nextPutAll:'hello'; |
|
74 |
space; |
|
75 |
nextPutAll:'world'. |
|
76 |
||
77 |
s contents inspect |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
78 |
[exEnd] |
2883 | 79 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
80 |
[exBegin] |
2883 | 81 |
|s| |
82 |
||
83 |
s := WriteStream on:''. |
|
84 |
s nextPutAll:'hello'; |
|
85 |
space; |
|
86 |
nextPutAll:'world'. |
|
87 |
||
88 |
Transcript nextPutLine:(s contents) |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
89 |
[exEnd] |
2883 | 90 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
91 |
[exBegin] |
2883 | 92 |
|s| |
93 |
||
94 |
s := '' writeStream. |
|
95 |
s nextPutAll:'hello'; |
|
96 |
space; |
|
97 |
nextPutAll:'world'. |
|
98 |
||
99 |
Transcript nextPutLine:(s contents) |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
100 |
[exEnd] |
2883 | 101 |
" |
102 |
||
345 | 103 |
! ! |
104 |
||
1 | 105 |
!WriteStream methodsFor:'accessing'! |
106 |
||
15179 | 107 |
clear |
108 |
"for compatibility with Transcript" |
|
109 |
||
110 |
self reset |
|
111 |
! |
|
112 |
||
1 | 113 |
contents |
345 | 114 |
"return the current contents (a collection) of the stream. |
115 |
Currently, this returns the actual collection if possible |
|
116 |
(and reset is implemented to create a new one) in contrast to |
|
117 |
ST80, where contents returns a copy and reset only sets the writePointer. |
|
118 |
The ST/X behavior creates less temporary garbage in the normal case |
|
2410 | 119 |
(where things are written for the contents only) but may be incompatible |
345 | 120 |
with some applications. Time will show, if this is to be changed." |
1 | 121 |
|
260 | 122 |
|lastIndex| |
123 |
||
15347 | 124 |
lastIndex := position. |
260 | 125 |
collection size == lastIndex ifFalse:[ |
18294 | 126 |
collection isFixedSize ifTrue:[ |
127 |
" |
|
128 |
grow is expensive - return a copy. |
|
129 |
(is this what users of writeStream expect ?) |
|
130 |
" |
|
131 |
collection := collection copyFrom:1 to:lastIndex |
|
132 |
] ifFalse:[ |
|
133 |
collection grow:lastIndex |
|
134 |
] |
|
1 | 135 |
]. |
136 |
^ collection |
|
2410 | 137 |
|
3087 | 138 |
"Modified: / 19.2.1997 / 08:57:28 / stefan" |
139 |
"Modified: / 30.10.1997 / 16:21:23 / cg" |
|
1 | 140 |
! |
141 |
||
5888 | 142 |
last |
143 |
"return the last element - report an error if the stream is empty" |
|
144 |
||
16201 | 145 |
^ collection at:position. |
5888 | 146 |
|
147 |
" |
|
148 |
|s| |
|
149 |
||
150 |
s := '' writeStream. |
|
151 |
s nextPut:$a. |
|
18294 | 152 |
s last. |
5888 | 153 |
s nextPut:$b. |
18294 | 154 |
s last. |
5888 | 155 |
s nextPut:$c. |
18294 | 156 |
s last. |
5888 | 157 |
" |
158 |
! |
|
159 |
||
160 |
last:n |
|
161 |
"return the last n elements as species of the underlying collection; |
|
162 |
Report an error if the stream is empty" |
|
163 |
||
17181 | 164 |
^ collection copyFrom:(position - n + 1) to:position. |
5888 | 165 |
|
166 |
" |
|
167 |
|s| |
|
168 |
||
169 |
s := '' writeStream. |
|
170 |
s nextPut:$a. |
|
18294 | 171 |
s last:1. |
5888 | 172 |
s nextPut:$b. |
18294 | 173 |
s last:1. |
174 |
s last:2. |
|
5888 | 175 |
s nextPut:$c. |
18294 | 176 |
s last:1. |
177 |
s last:2. |
|
178 |
s last:3. |
|
5888 | 179 |
" |
180 |
! |
|
181 |
||
329 | 182 |
reset |
345 | 183 |
"reset the stream; write anew. |
2410 | 184 |
See the comment in WriteStream>>contents" |
329 | 185 |
|
186 |
collection := collection species new:(collection size). |
|
19887 | 187 |
position := 0. |
2410 | 188 |
|
189 |
"Modified: 19.2.1997 / 08:57:00 / stefan" |
|
1 | 190 |
! ! |
191 |
||
611 | 192 |
!WriteStream methodsFor:'positioning'! |
193 |
||
15617 | 194 |
position:index0Based |
611 | 195 |
"redefined to allow positioning past the readLimit" |
196 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
197 |
((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError]. |
15347 | 198 |
position := index0Based |
17688 | 199 |
! |
200 |
||
201 |
skip:count |
|
202 |
"redefined to allow positioning back over already written characters" |
|
203 |
||
204 |
self position:(self position + count). |
|
611 | 205 |
! ! |
206 |
||
207 |
!WriteStream methodsFor:'private'! |
|
208 |
||
209 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
210 |
"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
|
211 |
|
611 | 212 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
213 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
214 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 215 |
! |
216 |
||
217 |
growCollection:minNewSize |
|
218 |
"grow the streamed collection to at least minNewSize" |
|
219 |
||
220 |
|oldSize newSize newColl| |
|
221 |
||
222 |
oldSize := collection size. |
|
223 |
(oldSize == 0) ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
224 |
newSize := minNewSize |
611 | 225 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
226 |
newSize := oldSize * 2. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
227 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 228 |
]. |
229 |
collection isFixedSize ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
230 |
newColl := collection species new:newSize. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
231 |
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
|
232 |
collection := newColl |
611 | 233 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
234 |
collection grow:newSize |
611 | 235 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
236 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
237 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
611 | 238 |
! ! |
239 |
||
7260 | 240 |
!WriteStream methodsFor:'private-accessing'! |
611 | 241 |
|
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
242 |
on:aCollection |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
243 |
super on:aCollection. |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
244 |
readLimit := 0. |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
245 |
! |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
246 |
|
611 | 247 |
on:aCollection from:start to:last |
248 |
"create and return a new stream for writing onto aCollection, where |
|
249 |
writing is limited to the elements in the range start to last." |
|
250 |
||
251 |
super on:aCollection from:start to:last. |
|
252 |
writeLimit := last. |
|
253 |
! ! |
|
254 |
||
255 |
!WriteStream methodsFor:'queries'! |
|
256 |
||
21132 | 257 |
endsWith:aCollection |
258 |
"Answer true, if the contents of the stream ends with aCollection. |
|
259 |
Speedup for upToAll:, throughAll etc." |
|
260 |
||
261 |
|sz start "{ Class: SmallInteger }"| |
|
262 |
||
263 |
collection isNil ifTrue:[ |
|
264 |
"only supported for internal streams" |
|
265 |
self shouldNotImplement. |
|
266 |
]. |
|
267 |
||
268 |
sz := aCollection size. |
|
269 |
start := position + 1 - sz. |
|
270 |
^ start > 0 and:[(collection copyFrom:position+1-sz to:position) endsWith:aCollection]. |
|
271 |
||
272 |
" |
|
273 |
(WriteStream with:'abcdef') endsWith:'def' |
|
274 |
(WriteStream with:'def') endsWith:'def' |
|
275 |
(WriteStream with:'abc') endsWith:'def' |
|
276 |
(WriteStream with:'ef') endsWith:'def' |
|
277 |
false " |
|
278 |
! |
|
279 |
||
3031 | 280 |
size |
281 |
"return the current size" |
|
282 |
||
15347 | 283 |
^ position |
611 | 284 |
! ! |
285 |
||
63 | 286 |
!WriteStream methodsFor:'reading'! |
1 | 287 |
|
288 |
next |
|
10 | 289 |
"catch read access to write stream - report an error" |
1 | 290 |
|
10 | 291 |
self shouldNotImplement |
1 | 292 |
! |
293 |
||
294 |
peek |
|
10 | 295 |
"catch read access to write stream - report an error" |
1 | 296 |
|
10 | 297 |
self shouldNotImplement |
63 | 298 |
! ! |
299 |
||
5556 | 300 |
!WriteStream methodsFor:'testing'! |
301 |
||
302 |
isEmpty |
|
303 |
"return true, if the contents of the stream is empty" |
|
304 |
||
15617 | 305 |
^ self position == 0 |
5556 | 306 |
|
307 |
"Created: 14.10.1997 / 20:44:37 / cg" |
|
16201 | 308 |
! |
309 |
||
18294 | 310 |
isReadable |
20432 | 311 |
"return true if the receiver supports reading - that's not true" |
16201 | 312 |
|
313 |
^ false |
|
314 |
||
315 |
"Created: / 8.11.1997 / 14:06:07 / cg" |
|
316 |
! |
|
317 |
||
318 |
isWritable |
|
19437 | 319 |
"return true, if writing is supported by the receiver. |
16201 | 320 |
Always return true here" |
321 |
||
322 |
^ true |
|
323 |
||
324 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
5556 | 325 |
! ! |
326 |
||
611 | 327 |
!WriteStream methodsFor:'writing'! |
369 | 328 |
|
611 | 329 |
next:count put:anObject |
330 |
"append anObject count times to the receiver. |
|
331 |
Redefined to avoid count grows of the underlying collection - |
|
332 |
instead a single grow on the final size is performed." |
|
333 |
||
17181 | 334 |
|final| |
611 | 335 |
|
17062 | 336 |
count == 0 ifTrue:[^ self]. |
337 |
||
611 | 338 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
19643 | 339 |
super next:count put:anObject. |
340 |
^ self. |
|
611 | 341 |
]. |
369 | 342 |
|
17181 | 343 |
final := position + count. |
611 | 344 |
(final > collection size) ifTrue:[ |
19643 | 345 |
self growCollection:final |
611 | 346 |
]. |
369 | 347 |
|
17181 | 348 |
collection from:position + 1 to:final put:anObject. |
349 |
position := position + count. |
|
19643 | 350 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 351 |
"/ ^ anObject -- return self |
16793 | 352 |
|
353 |
" |
|
354 |
'' writeStream next:10 put:$* |
|
355 |
" |
|
611 | 356 |
! |
1 | 357 |
|
5198
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
358 |
next:n putAll:aCollection startingAt:pos1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
359 |
"append some elements of the argument, aCollection to the stream." |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
360 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
361 |
^ self nextPutAll:aCollection startingAt:pos1 to:pos1+n-1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
362 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
363 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
364 |
|s| |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
365 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
366 |
s := '' writeStream. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
367 |
s nextPutAll:'hello '. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
368 |
s next:5 putAll:'1234world012345' startingAt:5. |
18294 | 369 |
s contents |
5198
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
370 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
371 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
372 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
373 |
! |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
374 |
|
1 | 375 |
nextPut:anObject |
1584 | 376 |
"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
|
377 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 378 |
|
63 | 379 |
%{ /* NOCONTEXT */ |
380 |
||
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
381 |
#ifndef NO_PRIM_STREAM |
63 | 382 |
REGISTER int pos; |
383 |
unsigned ch; |
|
384 |
OBJ coll; |
|
2883 | 385 |
OBJ p, wL, rL; |
1584 | 386 |
int __readLimit = -1; |
63 | 387 |
|
1136 | 388 |
coll = __INST(collection); |
389 |
p = __INST(position); |
|
63 | 390 |
|
293 | 391 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
19643 | 392 |
pos = __intVal(p); |
393 |
/* make 1-based */ |
|
394 |
pos = pos + 1; |
|
395 |
wL = __INST(writeLimit); |
|
63 | 396 |
|
19643 | 397 |
if ((wL == nil) |
398 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
399 |
OBJ cls; |
|
252 | 400 |
|
19643 | 401 |
cls = __qClass(coll); |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
402 |
|
19643 | 403 |
rL = __INST(readLimit); |
404 |
if (__isSmallInteger(rL)) { |
|
405 |
__readLimit = __intVal(rL); |
|
406 |
} |
|
293 | 407 |
|
19643 | 408 |
if (cls == @global(String)) { |
409 |
if (__isCharacter(anObject) |
|
410 |
&& ((ch = __intVal(__characterVal(anObject))) <= 255) /* ch is unsigned */ |
|
411 |
&& (pos <= __stringSize(coll))) { |
|
412 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
18294 | 413 |
advancePositionAndReturn: ; |
19643 | 414 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
415 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
416 |
} |
|
417 |
__INST(position) = __mkSmallInteger(pos); |
|
418 |
RETURN ( anObject ); |
|
419 |
} |
|
420 |
} else if (cls == @global(ByteArray)) { |
|
421 |
if (__isSmallInteger(anObject) |
|
422 |
&& ((ch = __intVal(anObject)) <= 0xFF) /* ch is unsigned */ |
|
423 |
&& (pos <= __byteArraySize(coll))) { |
|
424 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
425 |
goto advancePositionAndReturn; |
|
426 |
} |
|
427 |
} else if (cls == @global(Array)) { |
|
428 |
if (pos <= __arraySize(coll)) { |
|
429 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
|
430 |
__STORE(coll, anObject); |
|
431 |
goto advancePositionAndReturn; |
|
432 |
} |
|
433 |
} else if (cls == @global(Unicode16String)) { |
|
434 |
if (__isCharacter(anObject) |
|
435 |
&& ((ch = __intVal(__characterVal(anObject))) <= 0xFFFF) /* ch is unsigned */ |
|
436 |
&& (pos <= __unicode16StringSize(coll))) { |
|
437 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
|
438 |
goto advancePositionAndReturn; |
|
439 |
} |
|
440 |
} else if (cls == @global(Unicode32String)) { |
|
441 |
if (__isCharacter(anObject) |
|
442 |
&& (pos <= __unicode32StringSize(coll))) { |
|
443 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = __intVal(__characterVal(anObject)); |
|
444 |
goto advancePositionAndReturn; |
|
445 |
} |
|
446 |
} |
|
447 |
} |
|
63 | 448 |
} |
2894 | 449 |
#endif |
260 | 450 |
%}. |
293 | 451 |
(writeLimit isNil |
15347 | 452 |
or:[(position + 1) <= writeLimit]) ifTrue:[ |
19643 | 453 |
(position >= collection size) ifTrue:[self growCollection]. |
454 |
collection at:(position + 1) put:anObject. |
|
455 |
position := position + 1. |
|
456 |
(position > readLimit) ifTrue:[readLimit := position]. |
|
838 | 457 |
] ifFalse:[ |
19643 | 458 |
WriteError raiseErrorString:'write beyond writeLimit' |
293 | 459 |
]. |
1 | 460 |
^anObject |
461 |
! |
|
462 |
||
463 |
nextPutAll:aCollection |
|
63 | 464 |
"append all elements of the argument, aCollection to the stream. |
465 |
Redefined to avoid count grows of the underlying collection - |
|
466 |
instead a single grow on the final size is performed." |
|
1 | 467 |
|
17181 | 468 |
|nMore "{ Class: SmallInteger }" |
469 |
final "{ Class: SmallInteger }" | |
|
1 | 470 |
|
18294 | 471 |
(collection notNil and:[aCollection isSequenceable]) ifFalse:[ |
19643 | 472 |
"/ fallback |
473 |
super nextPutAll:aCollection. |
|
474 |
^ self. |
|
13628 | 475 |
]. |
77 | 476 |
|
1 | 477 |
nMore := aCollection size. |
9499 | 478 |
nMore == 0 ifTrue:[ |
19643 | 479 |
"/ for the programmer.. |
480 |
aCollection isCollection ifFalse:[ |
|
481 |
self error:'invalid argument (not a collection)' mayProceed:true |
|
482 |
]. |
|
9499 | 483 |
]. |
484 |
||
17181 | 485 |
final := position + nMore. |
293 | 486 |
(writeLimit notNil |
487 |
and:[final > writeLimit]) ifTrue:[ |
|
19643 | 488 |
final := writeLimit. |
489 |
nMore := final - position |
|
293 | 490 |
]. |
1 | 491 |
(final > collection size) ifTrue:[ |
19643 | 492 |
self growCollection:final |
1 | 493 |
]. |
18294 | 494 |
collection |
19643 | 495 |
replaceFrom:(position + 1) |
496 |
to:final |
|
497 |
with:aCollection |
|
498 |
startingAt:1. |
|
1 | 499 |
|
17181 | 500 |
position := position + nMore. |
19643 | 501 |
(position > readLimit) ifTrue:[readLimit := position]. |
12130 | 502 |
"/ ^ aCollection -- self |
13628 | 503 |
|
504 |
"Modified: / 04-09-2011 / 20:03:32 / cg" |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
505 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
506 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
507 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
508 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
509 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
510 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
511 |
|
17181 | 512 |
|nMore final| |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
513 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
514 |
collection isNil ifTrue:[ |
19643 | 515 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
516 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
517 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
518 |
nMore := pos2 - pos1 + 1. |
17181 | 519 |
final := position + nMore. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
520 |
(writeLimit notNil |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
521 |
and:[final > writeLimit]) ifTrue:[ |
19643 | 522 |
final := writeLimit. |
523 |
nMore := final - position |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
524 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
525 |
(final > collection size) ifTrue:[ |
19643 | 526 |
self growCollection:final |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
527 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
528 |
|
17181 | 529 |
collection replaceFrom:position + 1 |
19643 | 530 |
to:final |
531 |
with:aCollection |
|
532 |
startingAt:pos1. |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
533 |
|
17181 | 534 |
position := position + nMore. |
19643 | 535 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 536 |
"/ ^ aCollection -- return self |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
537 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
538 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
539 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
540 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
541 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
542 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
543 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
544 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
545 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
546 |
|
1541 | 547 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
8361 | 548 |
! |
549 |
||
11324 | 550 |
nextPutAllUnicode:aString |
551 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
552 |
||
15653 | 553 |
"this code is not perfect if you use both #nextPutAll: and #nextPutAllUnicode: |
18294 | 554 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 555 |
are not stored as UTF, so we get some inconsistent string" |
556 |
||
557 |
collection isString ifTrue:[ |
|
18294 | 558 |
collection bitsPerCharacter == 16 ifTrue:[ |
559 |
self nextPutAllUtf16:aString. |
|
560 |
] ifFalse:[ |
|
561 |
self nextPutAllUtf8:aString. |
|
562 |
]. |
|
11324 | 563 |
] ifFalse:[ |
18294 | 564 |
self nextPutAll:aString |
11324 | 565 |
]. |
13738
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
566 |
|
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
567 |
"Modified: / 28-09-2011 / 16:15:52 / cg" |
11324 | 568 |
! |
569 |
||
8615 | 570 |
nextPutByte:anObject |
571 |
"append the argument, anObject to the stream. |
|
572 |
Specially tuned for appending to String and ByteArray streams." |
|
573 |
||
574 |
%{ /* NOCONTEXT */ |
|
575 |
||
576 |
#ifndef NO_PRIM_STREAM |
|
19828 | 577 |
OBJ coll = __INST(collection); |
578 |
OBJ p = __INST(position); |
|
8615 | 579 |
|
580 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isSmallInteger(anObject)) { |
|
19828 | 581 |
OBJ wL = __INST(writeLimit); |
582 |
INT pos = __intVal(p) + 1; /* make 1-based and usable for update below */ |
|
583 |
unsigned int ch = __intVal(anObject); |
|
8615 | 584 |
|
19828 | 585 |
if (ch <= 0xFF && /* ch is unsigned */ |
586 |
((wL == nil) || (__isSmallInteger(wL) && (pos <= __intVal(wL))))) { |
|
587 |
OBJ cls = __qClass(coll); |
|
588 |
OBJ rL = __INST(readLimit); |
|
589 |
INT __readLimit = -1; |
|
8615 | 590 |
|
19643 | 591 |
if (__isSmallInteger(rL)) { |
592 |
__readLimit = __intVal(rL); |
|
593 |
} |
|
8615 | 594 |
|
19643 | 595 |
if (cls == @global(String)) { |
19828 | 596 |
if (pos <= __stringSize(coll)) { |
19643 | 597 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
18294 | 598 |
advancePositionAndReturn: ; |
19643 | 599 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
600 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
601 |
} |
|
602 |
__INST(position) = __mkSmallInteger(pos); |
|
603 |
RETURN ( anObject ); |
|
604 |
} |
|
605 |
} else if (cls == @global(ByteArray)) { |
|
19828 | 606 |
if (pos <= __byteArraySize(coll)) { |
19643 | 607 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
608 |
goto advancePositionAndReturn; |
|
609 |
} |
|
610 |
} |
|
611 |
} |
|
8615 | 612 |
} |
613 |
#endif |
|
614 |
%}. |
|
19828 | 615 |
((writeLimit isNil or:[(position + 1) <= writeLimit]) |
616 |
and:[position >= collection size]) ifTrue:[ |
|
617 |
self growCollection. |
|
618 |
^ self nextPutByte:anObject. "try again" |
|
619 |
] ifFalse:[ |
|
620 |
^ super nextPutByte:anObject |
|
621 |
]. |
|
8615 | 622 |
! |
623 |
||
8361 | 624 |
nextPutBytes:count from:anObject startingAt:start |
625 |
"write count bytes from an object starting at index start. |
|
626 |
Return the number of bytes written. |
|
18294 | 627 |
The object must have non-pointer indexed instvars |
628 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
8361 | 629 |
Use with care - non object oriented i/o. |
630 |
This is provided for compatibility with externalStream; |
|
631 |
to support binary storage" |
|
632 |
||
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
633 |
anObject isByteCollection ifTrue:[ |
18294 | 634 |
self nextPutAll:anObject startingAt:start to:(start + count - 1). |
635 |
^ count. |
|
8361 | 636 |
]. |
637 |
^ super nextPutBytes:count from:anObject startingAt:start |
|
11324 | 638 |
! |
639 |
||
640 |
nextPutUnicode:aCharacter |
|
641 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
642 |
||
15653 | 643 |
"this code is not perfect if you use both #nextPut: and #nextPutUnicode: |
18294 | 644 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 645 |
are not stored as UTF, so we get some inconsistent string" |
646 |
||
647 |
collection isString ifTrue:[ |
|
18294 | 648 |
collection bitsPerCharacter == 16 ifTrue:[ |
649 |
self nextPutUtf16:aCharacter. |
|
650 |
] ifFalse:[ |
|
651 |
self nextPutUtf8:aCharacter. |
|
652 |
]. |
|
11324 | 653 |
] ifFalse:[ |
18294 | 654 |
self nextPut:aCharacter. |
11324 | 655 |
]. |
1 | 656 |
! ! |
657 |
||
1965 | 658 |
!WriteStream class methodsFor:'documentation'! |
701 | 659 |
|
660 |
version |
|
19437 | 661 |
^ '$Header$' |
12130 | 662 |
! |
663 |
||
664 |
version_CVS |
|
19437 | 665 |
^ '$Header$' |
701 | 666 |
! ! |
19437 | 667 |