author | Claus Gittinger <cg@exept.de> |
Fri, 28 Jun 2019 08:49:56 +0200 | |
changeset 24385 | dd84e4dfb5cc |
parent 24032 | a89bc30b9339 |
child 24629 | 5975b5d70a64 |
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 |
||
22569 | 105 |
!WriteStream methodsFor:'Compatibility-Squeak'! |
106 |
||
107 |
resetContents |
|
108 |
"Squeak compatibility" |
|
109 |
||
110 |
self reset |
|
111 |
! ! |
|
112 |
||
1 | 113 |
!WriteStream methodsFor:'accessing'! |
114 |
||
15179 | 115 |
clear |
116 |
"for compatibility with Transcript" |
|
117 |
||
118 |
self reset |
|
119 |
! |
|
120 |
||
1 | 121 |
contents |
345 | 122 |
"return the current contents (a collection) of the stream. |
123 |
Currently, this returns the actual collection if possible |
|
124 |
(and reset is implemented to create a new one) in contrast to |
|
125 |
ST80, where contents returns a copy and reset only sets the writePointer. |
|
126 |
The ST/X behavior creates less temporary garbage in the normal case |
|
2410 | 127 |
(where things are written for the contents only) but may be incompatible |
345 | 128 |
with some applications. Time will show, if this is to be changed." |
1 | 129 |
|
260 | 130 |
|lastIndex| |
131 |
||
15347 | 132 |
lastIndex := position. |
22645 | 133 |
collection size ~~ lastIndex ifTrue:[ |
134 |
collection isFixedSize ifTrue:[ |
|
135 |
" |
|
136 |
grow is expensive - return a copy. |
|
137 |
(is this what users of writeStream expect ?) |
|
138 |
" |
|
139 |
collection := collection copyFrom:1 to:lastIndex |
|
140 |
] ifFalse:[ |
|
141 |
collection grow:lastIndex |
|
142 |
] |
|
1 | 143 |
]. |
144 |
^ collection |
|
2410 | 145 |
|
22645 | 146 |
"Modified: / 30-10-1997 / 16:21:23 / cg" |
147 |
"Modified: / 20-03-2018 / 16:34:29 / stefan" |
|
1 | 148 |
! |
149 |
||
5888 | 150 |
last |
151 |
"return the last element - report an error if the stream is empty" |
|
152 |
||
16201 | 153 |
^ collection at:position. |
5888 | 154 |
|
155 |
" |
|
156 |
|s| |
|
157 |
||
158 |
s := '' writeStream. |
|
159 |
s nextPut:$a. |
|
18294 | 160 |
s last. |
5888 | 161 |
s nextPut:$b. |
18294 | 162 |
s last. |
5888 | 163 |
s nextPut:$c. |
18294 | 164 |
s last. |
5888 | 165 |
" |
166 |
! |
|
167 |
||
168 |
last:n |
|
169 |
"return the last n elements as species of the underlying collection; |
|
170 |
Report an error if the stream is empty" |
|
171 |
||
17181 | 172 |
^ collection copyFrom:(position - n + 1) to:position. |
5888 | 173 |
|
174 |
" |
|
175 |
|s| |
|
176 |
||
177 |
s := '' writeStream. |
|
178 |
s nextPut:$a. |
|
18294 | 179 |
s last:1. |
5888 | 180 |
s nextPut:$b. |
18294 | 181 |
s last:1. |
182 |
s last:2. |
|
5888 | 183 |
s nextPut:$c. |
18294 | 184 |
s last:1. |
185 |
s last:2. |
|
186 |
s last:3. |
|
5888 | 187 |
" |
188 |
! |
|
189 |
||
22553 | 190 |
readStream |
191 |
"return a readstream on the current contents; |
|
192 |
that is: if more is written to the stream in the meantime, |
|
193 |
the readStream will NOT show it. |
|
194 |
To avoid copying the contents, a readStream with a readLimit |
|
195 |
is created. In the hope, that noone repositions and rewrites the |
|
196 |
underlying writeStream. |
|
197 |
If that leads to problems, you should use contents readStream." |
|
198 |
||
199 |
^ ReadStream on:collection from:1 to:self position. |
|
200 |
||
201 |
" |
|
202 |
|w r| |
|
203 |
||
204 |
w := WriteStream on:(String new). |
|
205 |
w nextPutAll:'12345'. |
|
206 |
r := w readStream. |
|
207 |
w nextPutAll:'abc'. |
|
208 |
w nextPutAll:(r upToEnd). |
|
209 |
w nextPutAll:'def'. |
|
210 |
w contents. |
|
211 |
" |
|
212 |
! |
|
213 |
||
329 | 214 |
reset |
345 | 215 |
"reset the stream; write anew. |
2410 | 216 |
See the comment in WriteStream>>contents" |
329 | 217 |
|
218 |
collection := collection species new:(collection size). |
|
19887 | 219 |
position := 0. |
2410 | 220 |
|
221 |
"Modified: 19.2.1997 / 08:57:00 / stefan" |
|
22150 | 222 |
! ! |
223 |
||
224 |
!WriteStream methodsFor:'positioning'! |
|
225 |
||
226 |
position:index0Based |
|
227 |
"redefined to allow positioning past the readLimit" |
|
228 |
||
229 |
((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError]. |
|
230 |
position := index0Based |
|
231 |
! |
|
232 |
||
233 |
skip:count |
|
234 |
"redefined to allow positioning back over already written characters" |
|
235 |
||
236 |
self position:(self position + count). |
|
22149 | 237 |
! |
238 |
||
239 |
truncateTo:givenMax |
|
240 |
"if the streamed contents is larger than maxSize, |
|
241 |
reset the write position back to this size. |
|
242 |
Otherwise do nothing." |
|
243 |
||
244 |
self size > givenMax ifTrue:[ |
|
245 |
self position:givenMax. |
|
246 |
]. |
|
247 |
||
248 |
" |
|
249 |
|s| |
|
250 |
||
251 |
s := '' writeStream. |
|
252 |
s nextPutAll:'123456'. |
|
253 |
s truncateTo:4. |
|
254 |
s nextPutAll:'abc'. |
|
255 |
s contents |
|
256 |
" |
|
257 |
||
258 |
" |
|
259 |
|s| |
|
260 |
||
261 |
s := '' writeStream. |
|
262 |
s nextPutAll:'1234'. |
|
263 |
s truncateTo:4. |
|
264 |
s nextPutAll:'abc'. |
|
265 |
s contents |
|
266 |
" |
|
267 |
||
268 |
" |
|
269 |
|s| |
|
270 |
||
271 |
s := '' writeStream. |
|
272 |
s nextPutAll:'123'. |
|
273 |
s truncateTo:4. |
|
274 |
s nextPutAll:'abc'. |
|
275 |
s contents |
|
276 |
" |
|
277 |
||
22152 | 278 |
" |
279 |
|s| |
|
280 |
||
281 |
s := 'testFile' asFilename writeStream. |
|
282 |
s nextPutAll:'123456'. |
|
283 |
s truncateTo:4. |
|
284 |
s nextPutAll:'abc'. |
|
285 |
s close. |
|
286 |
'testFile' asFilename contents |
|
287 |
" |
|
288 |
||
22149 | 289 |
"Created: / 02-08-2017 / 09:24:58 / cg" |
1 | 290 |
! ! |
291 |
||
611 | 292 |
!WriteStream methodsFor:'private'! |
293 |
||
294 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
295 |
"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
|
296 |
|
611 | 297 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
298 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
299 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 300 |
! |
301 |
||
302 |
growCollection:minNewSize |
|
303 |
"grow the streamed collection to at least minNewSize" |
|
304 |
||
305 |
|oldSize newSize newColl| |
|
306 |
||
307 |
oldSize := collection size. |
|
308 |
(oldSize == 0) ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
309 |
newSize := minNewSize |
611 | 310 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
311 |
newSize := oldSize * 2. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
312 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 313 |
]. |
314 |
collection isFixedSize ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
315 |
newColl := collection species new:newSize. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
316 |
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
|
317 |
collection := newColl |
611 | 318 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
319 |
collection grow:newSize |
611 | 320 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
321 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
322 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
611 | 323 |
! ! |
324 |
||
7260 | 325 |
!WriteStream methodsFor:'private-accessing'! |
611 | 326 |
|
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
327 |
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
|
328 |
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
|
329 |
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
|
330 |
! |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
331 |
|
611 | 332 |
on:aCollection from:start to:last |
333 |
"create and return a new stream for writing onto aCollection, where |
|
334 |
writing is limited to the elements in the range start to last." |
|
335 |
||
336 |
super on:aCollection from:start to:last. |
|
337 |
writeLimit := last. |
|
338 |
! ! |
|
339 |
||
340 |
!WriteStream methodsFor:'queries'! |
|
341 |
||
21132 | 342 |
endsWith:aCollection |
343 |
"Answer true, if the contents of the stream ends with aCollection. |
|
344 |
Speedup for upToAll:, throughAll etc." |
|
345 |
||
346 |
|sz start "{ Class: SmallInteger }"| |
|
347 |
||
348 |
collection isNil ifTrue:[ |
|
349 |
"only supported for internal streams" |
|
350 |
self shouldNotImplement. |
|
351 |
]. |
|
352 |
||
353 |
sz := aCollection size. |
|
354 |
start := position + 1 - sz. |
|
355 |
^ start > 0 and:[(collection copyFrom:position+1-sz to:position) endsWith:aCollection]. |
|
356 |
||
357 |
" |
|
358 |
(WriteStream with:'abcdef') endsWith:'def' |
|
359 |
(WriteStream with:'def') endsWith:'def' |
|
360 |
(WriteStream with:'abc') endsWith:'def' |
|
361 |
(WriteStream with:'ef') endsWith:'def' |
|
362 |
false " |
|
363 |
! |
|
364 |
||
3031 | 365 |
size |
366 |
"return the current size" |
|
367 |
||
15347 | 368 |
^ position |
611 | 369 |
! ! |
370 |
||
63 | 371 |
!WriteStream methodsFor:'reading'! |
1 | 372 |
|
373 |
next |
|
10 | 374 |
"catch read access to write stream - report an error" |
1 | 375 |
|
10 | 376 |
self shouldNotImplement |
1 | 377 |
! |
378 |
||
379 |
peek |
|
10 | 380 |
"catch read access to write stream - report an error" |
1 | 381 |
|
10 | 382 |
self shouldNotImplement |
63 | 383 |
! ! |
384 |
||
5556 | 385 |
!WriteStream methodsFor:'testing'! |
386 |
||
387 |
isEmpty |
|
388 |
"return true, if the contents of the stream is empty" |
|
389 |
||
15617 | 390 |
^ self position == 0 |
5556 | 391 |
|
392 |
"Created: 14.10.1997 / 20:44:37 / cg" |
|
16201 | 393 |
! |
394 |
||
18294 | 395 |
isReadable |
20432 | 396 |
"return true if the receiver supports reading - that's not true" |
16201 | 397 |
|
398 |
^ false |
|
399 |
||
400 |
"Created: / 8.11.1997 / 14:06:07 / cg" |
|
401 |
! |
|
402 |
||
403 |
isWritable |
|
19437 | 404 |
"return true, if writing is supported by the receiver. |
16201 | 405 |
Always return true here" |
406 |
||
407 |
^ true |
|
408 |
||
409 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
5556 | 410 |
! ! |
411 |
||
611 | 412 |
!WriteStream methodsFor:'writing'! |
369 | 413 |
|
611 | 414 |
next:count put:anObject |
415 |
"append anObject count times to the receiver. |
|
416 |
Redefined to avoid count grows of the underlying collection - |
|
417 |
instead a single grow on the final size is performed." |
|
418 |
||
17181 | 419 |
|final| |
611 | 420 |
|
17062 | 421 |
count == 0 ifTrue:[^ self]. |
422 |
||
611 | 423 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
19643 | 424 |
super next:count put:anObject. |
425 |
^ self. |
|
611 | 426 |
]. |
369 | 427 |
|
17181 | 428 |
final := position + count. |
611 | 429 |
(final > collection size) ifTrue:[ |
19643 | 430 |
self growCollection:final |
611 | 431 |
]. |
369 | 432 |
|
17181 | 433 |
collection from:position + 1 to:final put:anObject. |
434 |
position := position + count. |
|
19643 | 435 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 436 |
"/ ^ anObject -- return self |
16793 | 437 |
|
438 |
" |
|
439 |
'' writeStream next:10 put:$* |
|
440 |
" |
|
611 | 441 |
! |
1 | 442 |
|
443 |
nextPut:anObject |
|
1584 | 444 |
"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
|
445 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 446 |
|
63 | 447 |
%{ /* NOCONTEXT */ |
448 |
||
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
449 |
#ifndef NO_PRIM_STREAM |
63 | 450 |
REGISTER int pos; |
451 |
unsigned ch; |
|
452 |
OBJ coll; |
|
2883 | 453 |
OBJ p, wL, rL; |
1584 | 454 |
int __readLimit = -1; |
63 | 455 |
|
1136 | 456 |
coll = __INST(collection); |
457 |
p = __INST(position); |
|
63 | 458 |
|
293 | 459 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
19643 | 460 |
pos = __intVal(p); |
461 |
/* make 1-based */ |
|
462 |
pos = pos + 1; |
|
463 |
wL = __INST(writeLimit); |
|
63 | 464 |
|
19643 | 465 |
if ((wL == nil) |
466 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
467 |
OBJ cls; |
|
252 | 468 |
|
19643 | 469 |
cls = __qClass(coll); |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
470 |
|
19643 | 471 |
rL = __INST(readLimit); |
472 |
if (__isSmallInteger(rL)) { |
|
473 |
__readLimit = __intVal(rL); |
|
474 |
} |
|
293 | 475 |
|
19643 | 476 |
if (cls == @global(String)) { |
477 |
if (__isCharacter(anObject) |
|
478 |
&& ((ch = __intVal(__characterVal(anObject))) <= 255) /* ch is unsigned */ |
|
479 |
&& (pos <= __stringSize(coll))) { |
|
480 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
18294 | 481 |
advancePositionAndReturn: ; |
19643 | 482 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
483 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
484 |
} |
|
485 |
__INST(position) = __mkSmallInteger(pos); |
|
486 |
RETURN ( anObject ); |
|
487 |
} |
|
488 |
} else if (cls == @global(ByteArray)) { |
|
489 |
if (__isSmallInteger(anObject) |
|
490 |
&& ((ch = __intVal(anObject)) <= 0xFF) /* ch is unsigned */ |
|
491 |
&& (pos <= __byteArraySize(coll))) { |
|
492 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
493 |
goto advancePositionAndReturn; |
|
494 |
} |
|
495 |
} else if (cls == @global(Array)) { |
|
496 |
if (pos <= __arraySize(coll)) { |
|
497 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
|
498 |
__STORE(coll, anObject); |
|
499 |
goto advancePositionAndReturn; |
|
500 |
} |
|
501 |
} else if (cls == @global(Unicode16String)) { |
|
502 |
if (__isCharacter(anObject) |
|
503 |
&& ((ch = __intVal(__characterVal(anObject))) <= 0xFFFF) /* ch is unsigned */ |
|
504 |
&& (pos <= __unicode16StringSize(coll))) { |
|
505 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
|
506 |
goto advancePositionAndReturn; |
|
507 |
} |
|
508 |
} else if (cls == @global(Unicode32String)) { |
|
509 |
if (__isCharacter(anObject) |
|
510 |
&& (pos <= __unicode32StringSize(coll))) { |
|
511 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = __intVal(__characterVal(anObject)); |
|
512 |
goto advancePositionAndReturn; |
|
513 |
} |
|
514 |
} |
|
515 |
} |
|
63 | 516 |
} |
2894 | 517 |
#endif |
260 | 518 |
%}. |
24032 | 519 |
(writeLimit isNil or:[(position + 1) <= writeLimit]) ifTrue:[ |
19643 | 520 |
(position >= collection size) ifTrue:[self growCollection]. |
521 |
collection at:(position + 1) put:anObject. |
|
522 |
position := position + 1. |
|
523 |
(position > readLimit) ifTrue:[readLimit := position]. |
|
838 | 524 |
] ifFalse:[ |
19643 | 525 |
WriteError raiseErrorString:'write beyond writeLimit' |
293 | 526 |
]. |
1 | 527 |
^anObject |
528 |
! |
|
529 |
||
530 |
nextPutAll:aCollection |
|
63 | 531 |
"append all elements of the argument, aCollection to the stream. |
532 |
Redefined to avoid count grows of the underlying collection - |
|
533 |
instead a single grow on the final size is performed." |
|
1 | 534 |
|
17181 | 535 |
|nMore "{ Class: SmallInteger }" |
536 |
final "{ Class: SmallInteger }" | |
|
1 | 537 |
|
18294 | 538 |
(collection notNil and:[aCollection isSequenceable]) ifFalse:[ |
19643 | 539 |
"/ fallback |
540 |
super nextPutAll:aCollection. |
|
541 |
^ self. |
|
13628 | 542 |
]. |
77 | 543 |
|
1 | 544 |
nMore := aCollection size. |
9499 | 545 |
nMore == 0 ifTrue:[ |
19643 | 546 |
"/ for the programmer.. |
547 |
aCollection isCollection ifFalse:[ |
|
23011 | 548 |
self proceedableError:'invalid argument (not a collection)'. |
19643 | 549 |
]. |
9499 | 550 |
]. |
551 |
||
17181 | 552 |
final := position + nMore. |
24032 | 553 |
(writeLimit notNil and:[final > writeLimit]) ifTrue:[ |
554 |
"/ cg: the code below silently truncated the written string, |
|
555 |
"/ which is incorrect, as it should report a writeError!! |
|
556 |
^ super nextPutAll:aCollection. |
|
557 |
"/ final := writeLimit. |
|
558 |
"/ nMore := final - position |
|
293 | 559 |
]. |
1 | 560 |
(final > collection size) ifTrue:[ |
19643 | 561 |
self growCollection:final |
1 | 562 |
]. |
24032 | 563 |
collection replaceFrom:(position + 1) to:final with:aCollection startingAt:1. |
1 | 564 |
|
17181 | 565 |
position := position + nMore. |
19643 | 566 |
(position > readLimit) ifTrue:[readLimit := position]. |
12130 | 567 |
"/ ^ aCollection -- self |
13628 | 568 |
|
569 |
"Modified: / 04-09-2011 / 20:03:32 / cg" |
|
24032 | 570 |
"Modified: / 01-04-2019 / 17:05:36 / Claus Gittinger" |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
571 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
572 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
573 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
574 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
575 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
576 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
577 |
|
17181 | 578 |
|nMore final| |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
579 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
580 |
collection isNil ifTrue:[ |
19643 | 581 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
582 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
583 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
584 |
nMore := pos2 - pos1 + 1. |
17181 | 585 |
final := position + nMore. |
24032 | 586 |
(writeLimit notNil and:[final > writeLimit]) ifTrue:[ |
587 |
"/ cg: the code below silently truncated the written string, |
|
588 |
"/ which is incorrect, as it should report a writeError!! |
|
589 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2. |
|
590 |
"/ final := writeLimit. |
|
591 |
"/ nMore := final - position |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
592 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
593 |
(final > collection size) ifTrue:[ |
19643 | 594 |
self growCollection:final |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
595 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
596 |
|
24032 | 597 |
collection replaceFrom:(position + 1) to:final with:aCollection startingAt:pos1. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
598 |
|
17181 | 599 |
position := position + nMore. |
19643 | 600 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 601 |
"/ ^ aCollection -- return self |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
602 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
603 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
604 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
605 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
606 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
607 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
608 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
609 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
610 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
611 |
|
24032 | 612 |
"Modified: / 12-07-1996 / 10:31:36 / cg" |
613 |
"Modified: / 01-04-2019 / 17:05:52 / Claus Gittinger" |
|
8361 | 614 |
! |
615 |
||
11324 | 616 |
nextPutAllUnicode:aString |
617 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
618 |
||
15653 | 619 |
"this code is not perfect if you use both #nextPutAll: and #nextPutAllUnicode: |
18294 | 620 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 621 |
are not stored as UTF, so we get some inconsistent string" |
622 |
||
623 |
collection isString ifTrue:[ |
|
18294 | 624 |
collection bitsPerCharacter == 16 ifTrue:[ |
625 |
self nextPutAllUtf16:aString. |
|
626 |
] ifFalse:[ |
|
627 |
self nextPutAllUtf8:aString. |
|
628 |
]. |
|
11324 | 629 |
] ifFalse:[ |
18294 | 630 |
self nextPutAll:aString |
11324 | 631 |
]. |
13738
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
632 |
|
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
633 |
"Modified: / 28-09-2011 / 16:15:52 / cg" |
11324 | 634 |
! |
635 |
||
8615 | 636 |
nextPutByte:anObject |
637 |
"append the argument, anObject to the stream. |
|
638 |
Specially tuned for appending to String and ByteArray streams." |
|
639 |
||
640 |
%{ /* NOCONTEXT */ |
|
641 |
||
642 |
#ifndef NO_PRIM_STREAM |
|
19828 | 643 |
OBJ coll = __INST(collection); |
644 |
OBJ p = __INST(position); |
|
8615 | 645 |
|
646 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isSmallInteger(anObject)) { |
|
19828 | 647 |
OBJ wL = __INST(writeLimit); |
648 |
INT pos = __intVal(p) + 1; /* make 1-based and usable for update below */ |
|
649 |
unsigned int ch = __intVal(anObject); |
|
8615 | 650 |
|
24032 | 651 |
if ((ch <= 0xFF) /* ch is unsigned */ |
652 |
&& ((wL == nil) || (__isSmallInteger(wL) && (pos <= __intVal(wL))))) { |
|
19828 | 653 |
OBJ cls = __qClass(coll); |
654 |
OBJ rL = __INST(readLimit); |
|
655 |
INT __readLimit = -1; |
|
8615 | 656 |
|
19643 | 657 |
if (__isSmallInteger(rL)) { |
658 |
__readLimit = __intVal(rL); |
|
659 |
} |
|
8615 | 660 |
|
19643 | 661 |
if (cls == @global(String)) { |
19828 | 662 |
if (pos <= __stringSize(coll)) { |
19643 | 663 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
18294 | 664 |
advancePositionAndReturn: ; |
19643 | 665 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
666 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
667 |
} |
|
668 |
__INST(position) = __mkSmallInteger(pos); |
|
669 |
RETURN ( anObject ); |
|
670 |
} |
|
671 |
} else if (cls == @global(ByteArray)) { |
|
19828 | 672 |
if (pos <= __byteArraySize(coll)) { |
19643 | 673 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
674 |
goto advancePositionAndReturn; |
|
675 |
} |
|
676 |
} |
|
677 |
} |
|
8615 | 678 |
} |
679 |
#endif |
|
680 |
%}. |
|
19828 | 681 |
((writeLimit isNil or:[(position + 1) <= writeLimit]) |
682 |
and:[position >= collection size]) ifTrue:[ |
|
683 |
self growCollection. |
|
684 |
^ self nextPutByte:anObject. "try again" |
|
685 |
] ifFalse:[ |
|
686 |
^ super nextPutByte:anObject |
|
687 |
]. |
|
8615 | 688 |
! |
689 |
||
8361 | 690 |
nextPutBytes:count from:anObject startingAt:start |
691 |
"write count bytes from an object starting at index start. |
|
692 |
Return the number of bytes written. |
|
18294 | 693 |
The object must have non-pointer indexed instvars |
694 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
8361 | 695 |
Use with care - non object oriented i/o. |
696 |
This is provided for compatibility with externalStream; |
|
697 |
to support binary storage" |
|
698 |
||
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
699 |
anObject isByteCollection ifTrue:[ |
18294 | 700 |
self nextPutAll:anObject startingAt:start to:(start + count - 1). |
701 |
^ count. |
|
8361 | 702 |
]. |
703 |
^ super nextPutBytes:count from:anObject startingAt:start |
|
11324 | 704 |
! |
705 |
||
706 |
nextPutUnicode:aCharacter |
|
707 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
708 |
||
15653 | 709 |
"this code is not perfect if you use both #nextPut: and #nextPutUnicode: |
18294 | 710 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 711 |
are not stored as UTF, so we get some inconsistent string" |
712 |
||
713 |
collection isString ifTrue:[ |
|
18294 | 714 |
collection bitsPerCharacter == 16 ifTrue:[ |
715 |
self nextPutUtf16:aCharacter. |
|
716 |
] ifFalse:[ |
|
717 |
self nextPutUtf8:aCharacter. |
|
718 |
]. |
|
11324 | 719 |
] ifFalse:[ |
18294 | 720 |
self nextPut:aCharacter. |
11324 | 721 |
]. |
1 | 722 |
! ! |
723 |
||
1965 | 724 |
!WriteStream class methodsFor:'documentation'! |
701 | 725 |
|
726 |
version |
|
19437 | 727 |
^ '$Header$' |
12130 | 728 |
! |
729 |
||
730 |
version_CVS |
|
19437 | 731 |
^ '$Header$' |
701 | 732 |
! ! |
19437 | 733 |