author | Claus Gittinger <cg@exept.de> |
Tue, 19 Aug 1997 18:10:02 +0200 | |
changeset 2864 | 328b52ecd339 |
parent 2863 | aec08a0c2c2e |
child 2883 | ee0eb799bd3e |
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 |
||
13 |
PositionableStream subclass:#WriteStream |
|
1295 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Streams' |
|
1 | 18 |
! |
19 |
||
1965 | 20 |
!WriteStream class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
252 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
68 | 36 |
documentation |
37 |
" |
|
88 | 38 |
Streams for writing into - this implementation currently DOES change the |
261 | 39 |
identity if the streamed-upon collection IF it cannot grow easily. |
40 |
Collections which cannot grow easily are for example: Array, ByteArray and String. |
|
88 | 41 |
Thus it is slightly incompatible to ST-80 since 'aStream contents' does |
42 |
not always return the original collection. This may change. |
|
1295 | 43 |
|
44 |
[author:] |
|
45 |
Claus Gittinger |
|
68 | 46 |
" |
345 | 47 |
! ! |
48 |
||
1 | 49 |
!WriteStream methodsFor:'accessing'! |
50 |
||
51 |
contents |
|
345 | 52 |
"return the current contents (a collection) of the stream. |
53 |
Currently, this returns the actual collection if possible |
|
54 |
(and reset is implemented to create a new one) in contrast to |
|
55 |
ST80, where contents returns a copy and reset only sets the writePointer. |
|
56 |
The ST/X behavior creates less temporary garbage in the normal case |
|
2410 | 57 |
(where things are written for the contents only) but may be incompatible |
345 | 58 |
with some applications. Time will show, if this is to be changed." |
1 | 59 |
|
260 | 60 |
|lastIndex| |
61 |
||
62 |
lastIndex := position - 1. |
|
63 |
collection size == lastIndex ifFalse:[ |
|
2410 | 64 |
collection isFixedSize ifTrue:[ |
65 |
" |
|
66 |
grow is expensive - return a copy. |
|
67 |
(is this what users of writeStream expect ? |
|
68 |
" |
|
69 |
collection := collection copyFrom:1 to:lastIndex |
|
70 |
] ifFalse:[ |
|
71 |
collection grow:lastIndex |
|
72 |
] |
|
1 | 73 |
]. |
74 |
^ collection |
|
2410 | 75 |
|
76 |
"Modified: 19.2.1997 / 08:57:28 / stefan" |
|
1 | 77 |
! |
78 |
||
329 | 79 |
reset |
345 | 80 |
"reset the stream; write anew. |
2410 | 81 |
See the comment in WriteStream>>contents" |
329 | 82 |
|
83 |
collection := collection species new:(collection size). |
|
84 |
super reset |
|
2410 | 85 |
|
86 |
"Modified: 19.2.1997 / 08:57:00 / stefan" |
|
1 | 87 |
! ! |
88 |
||
611 | 89 |
!WriteStream methodsFor:'positioning'! |
90 |
||
91 |
position:index |
|
92 |
"redefined to allow positioning past the readLimit" |
|
93 |
||
94 |
((index > (collection size + 1)) or:[index < 0]) ifTrue: [^ self positionError]. |
|
95 |
position := index |
|
96 |
! ! |
|
97 |
||
98 |
!WriteStream methodsFor:'private'! |
|
99 |
||
100 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
101 |
"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
|
102 |
|
611 | 103 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
104 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
105 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 106 |
! |
107 |
||
108 |
growCollection:minNewSize |
|
109 |
"grow the streamed collection to at least minNewSize" |
|
110 |
||
111 |
|oldSize newSize newColl| |
|
112 |
||
113 |
oldSize := collection size. |
|
114 |
(oldSize == 0) ifTrue:[ |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
115 |
newSize := minNewSize |
611 | 116 |
] ifFalse:[ |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
117 |
newSize := oldSize * 2. |
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
118 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 119 |
]. |
120 |
collection isFixedSize ifTrue:[ |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
121 |
newColl := collection species new:newSize. |
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
122 |
newColl replaceFrom:1 to:oldSize with:collection startingAt:1. |
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
123 |
collection := newColl |
611 | 124 |
] ifFalse:[ |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
125 |
collection grow:newSize |
611 | 126 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
127 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
128 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
611 | 129 |
! ! |
130 |
||
131 |
!WriteStream methodsFor:'private accessing'! |
|
132 |
||
133 |
on:aCollection from:start to:last |
|
134 |
"create and return a new stream for writing onto aCollection, where |
|
135 |
writing is limited to the elements in the range start to last." |
|
136 |
||
137 |
super on:aCollection from:start to:last. |
|
138 |
writeLimit := last. |
|
139 |
! ! |
|
140 |
||
141 |
!WriteStream methodsFor:'queries'! |
|
142 |
||
143 |
isWritable |
|
1405 | 144 |
"return true, if writing is supported by the recevier. |
145 |
Always return true here" |
|
146 |
||
611 | 147 |
^ true |
1405 | 148 |
|
149 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
611 | 150 |
! ! |
151 |
||
63 | 152 |
!WriteStream methodsFor:'reading'! |
1 | 153 |
|
154 |
next |
|
10 | 155 |
"catch read access to write stream - report an error" |
1 | 156 |
|
10 | 157 |
self shouldNotImplement |
1 | 158 |
! |
159 |
||
160 |
peek |
|
10 | 161 |
"catch read access to write stream - report an error" |
1 | 162 |
|
10 | 163 |
self shouldNotImplement |
63 | 164 |
! ! |
165 |
||
611 | 166 |
!WriteStream methodsFor:'writing'! |
369 | 167 |
|
611 | 168 |
next:count put:anObject |
169 |
"append anObject count times to the receiver. |
|
170 |
Redefined to avoid count grows of the underlying collection - |
|
171 |
instead a single grow on the final size is performed." |
|
172 |
||
173 |
|final| |
|
174 |
||
175 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
|
176 |
^ super next:count put:anObject |
|
177 |
]. |
|
369 | 178 |
|
611 | 179 |
final := position + count - 1. |
180 |
(final > collection size) ifTrue:[ |
|
181 |
self growCollection:final |
|
182 |
]. |
|
369 | 183 |
|
611 | 184 |
position to:final do:[:index | |
185 |
collection at:index put:anObject. |
|
186 |
]. |
|
187 |
position := position + count. |
|
188 |
(position > readLimit) ifTrue:[readLimit := position - 1]. |
|
189 |
^ anObject |
|
190 |
! |
|
1 | 191 |
|
192 |
nextPut:anObject |
|
1584 | 193 |
"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
|
194 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 195 |
|
63 | 196 |
%{ /* NOCONTEXT */ |
197 |
||
198 |
REGISTER int pos; |
|
199 |
unsigned ch; |
|
200 |
OBJ coll; |
|
1584 | 201 |
OBJ p, l, rL; |
202 |
int __readLimit = -1; |
|
63 | 203 |
|
1136 | 204 |
coll = __INST(collection); |
205 |
p = __INST(position); |
|
63 | 206 |
|
293 | 207 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
1584 | 208 |
pos = __intVal(p); |
209 |
l = __INST(writeLimit); |
|
68 | 210 |
|
1584 | 211 |
if ((l == nil) |
212 |
|| (__isSmallInteger(l) && (pos <= __intVal(l)))) { |
|
213 |
OBJ cls; |
|
63 | 214 |
|
1584 | 215 |
cls = __qClass(coll); |
252 | 216 |
|
1584 | 217 |
rL = __INST(readLimit); |
218 |
if (__isSmallInteger(rL)) { |
|
219 |
__readLimit = __intVal(rL); |
|
220 |
} |
|
293 | 221 |
|
1584 | 222 |
if (cls == @global(String)) { |
223 |
if (__isCharacter(anObject) |
|
224 |
&& (pos <= __stringSize(coll))) { |
|
225 |
ch = __intVal(_characterVal(anObject)); |
|
2864
328b52ecd339
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2863
diff
changeset
|
226 |
if (ch <= 255) { /* ch is unsigned */ |
1584 | 227 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
228 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
229 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
230 |
__INST(readLimit) = __MKSMALLINT(pos); |
|
231 |
} |
|
232 |
RETURN ( anObject ); |
|
233 |
} |
|
234 |
} |
|
235 |
} else if (cls == @global(ByteArray)) { |
|
236 |
if (__isSmallInteger(anObject) |
|
2864
328b52ecd339
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2863
diff
changeset
|
237 |
&& ((ch = __intVal(anObject)) <= 255) /* ch is unsigned */ |
1584 | 238 |
&& (pos <= _byteArraySize(coll))) { |
239 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
240 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
241 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
242 |
__INST(readLimit) = __MKSMALLINT(pos); |
|
243 |
} |
|
244 |
RETURN ( anObject ); |
|
245 |
} |
|
246 |
} else if (cls == @global(Array)) { |
|
247 |
if (pos <= _arraySize(coll)) { |
|
248 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
|
249 |
__STORE(coll, anObject); |
|
250 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
251 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
252 |
__INST(readLimit) = __MKSMALLINT(pos); |
|
253 |
} |
|
254 |
RETURN ( anObject ); |
|
255 |
} |
|
256 |
} |
|
257 |
} |
|
63 | 258 |
} |
260 | 259 |
%}. |
293 | 260 |
(writeLimit isNil |
261 |
or:[position <= writeLimit]) ifTrue:[ |
|
1584 | 262 |
(position > collection size) ifTrue:[self growCollection]. |
263 |
collection at:position put:anObject. |
|
264 |
(position > readLimit) ifTrue:[readLimit := position]. |
|
265 |
position := position + 1. |
|
838 | 266 |
] ifFalse:[ |
1965 | 267 |
WriteErrorSignal raiseErrorString:'write beyond writeLimit' |
293 | 268 |
]. |
1 | 269 |
^anObject |
270 |
! |
|
271 |
||
272 |
nextPutAll:aCollection |
|
63 | 273 |
"append all elements of the argument, aCollection to the stream. |
274 |
Redefined to avoid count grows of the underlying collection - |
|
275 |
instead a single grow on the final size is performed." |
|
1 | 276 |
|
277 |
|nMore final| |
|
278 |
||
77 | 279 |
collection isNil ifTrue:[ |
260 | 280 |
^ super nextPutAll:aCollection |
77 | 281 |
]. |
282 |
||
1 | 283 |
nMore := aCollection size. |
284 |
final := position + nMore - 1. |
|
293 | 285 |
(writeLimit notNil |
286 |
and:[final > writeLimit]) ifTrue:[ |
|
287 |
final := writeLimit. |
|
288 |
nMore := final - position + 1 |
|
289 |
]. |
|
1 | 290 |
(final > collection size) ifTrue:[ |
252 | 291 |
self growCollection:final |
1 | 292 |
]. |
293 |
collection replaceFrom:position |
|
252 | 294 |
to:final |
295 |
with:aCollection |
|
296 |
startingAt:1. |
|
1 | 297 |
|
298 |
position := position + nMore. |
|
299 |
(position > readLimit) ifTrue:[readLimit := position - 1]. |
|
300 |
^ aCollection |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
301 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
302 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
303 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
304 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
305 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
306 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
307 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
308 |
|nMore final| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
309 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
310 |
collection isNil ifTrue:[ |
1541 | 311 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
312 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
313 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
314 |
nMore := pos2 - pos1 + 1. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
315 |
final := position + nMore - 1. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
316 |
(writeLimit notNil |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
317 |
and:[final > writeLimit]) ifTrue:[ |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
318 |
final := writeLimit. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
319 |
nMore := final - position + 1 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
320 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
321 |
(final > collection size) ifTrue:[ |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
322 |
self growCollection:final |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
323 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
324 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
325 |
collection replaceFrom:position |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
326 |
to:final |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
327 |
with:aCollection |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
328 |
startingAt:pos1. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
329 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
330 |
position := position + nMore. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
331 |
(position > readLimit) ifTrue:[readLimit := position - 1]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
332 |
^ aCollection |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
333 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
334 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
335 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
336 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
337 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
338 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
339 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
340 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
341 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
342 |
|
1541 | 343 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
1 | 344 |
! ! |
345 |
||
1965 | 346 |
!WriteStream class methodsFor:'documentation'! |
701 | 347 |
|
348 |
version |
|
2864
328b52ecd339
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2863
diff
changeset
|
349 |
^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.35 1997-08-19 16:10:02 cg Exp $' |
701 | 350 |
! ! |