author | Claus Gittinger <cg@exept.de> |
Fri, 19 Jul 1996 12:26:56 +0200 | |
changeset 1584 | cad4f2c515c7 |
parent 1541 | 6ed5feddc4ed |
child 1965 | 4ca38574df3b |
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 |
||
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
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 |
|
57 |
(whre things are written for the contents only) but may be incompatible |
|
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:[ |
|
252 | 64 |
collection isFixedSize ifTrue:[ |
260 | 65 |
" |
66 |
grow is expensive - return a copy. |
|
67 |
(is this what users of writeStream expect ? |
|
68 |
" |
|
69 |
collection := collection copyFrom:1 to:lastIndex |
|
252 | 70 |
] ifFalse:[ |
260 | 71 |
collection grow:lastIndex |
252 | 72 |
] |
1 | 73 |
]. |
74 |
^ collection |
|
75 |
! |
|
76 |
||
329 | 77 |
reset |
345 | 78 |
"reset the stream; write anew. |
79 |
See the comment in WriteStream>>contnts" |
|
329 | 80 |
|
81 |
collection := collection species new:(collection size). |
|
82 |
super reset |
|
1 | 83 |
! ! |
84 |
||
611 | 85 |
!WriteStream methodsFor:'positioning'! |
86 |
||
87 |
position:index |
|
88 |
"redefined to allow positioning past the readLimit" |
|
89 |
||
90 |
((index > (collection size + 1)) or:[index < 0]) ifTrue: [^ self positionError]. |
|
91 |
position := index |
|
92 |
! ! |
|
93 |
||
94 |
!WriteStream methodsFor:'private'! |
|
95 |
||
96 |
growCollection |
|
97 |
self growCollection:10 |
|
98 |
! |
|
99 |
||
100 |
growCollection:minNewSize |
|
101 |
"grow the streamed collection to at least minNewSize" |
|
102 |
||
103 |
|oldSize newSize newColl| |
|
104 |
||
105 |
oldSize := collection size. |
|
106 |
(oldSize == 0) ifTrue:[ |
|
107 |
newSize := minNewSize |
|
108 |
] ifFalse:[ |
|
109 |
newSize := oldSize * 2. |
|
110 |
[newSize < minNewSize] whileTrue:[ |
|
111 |
newSize := newSize * 2 |
|
112 |
] |
|
113 |
]. |
|
114 |
collection isFixedSize ifTrue:[ |
|
115 |
newColl := collection species new:newSize. |
|
116 |
newColl replaceFrom:1 to:oldSize with:collection startingAt:1. |
|
117 |
collection := newColl |
|
118 |
] ifFalse:[ |
|
119 |
collection grow:newSize |
|
120 |
]. |
|
121 |
! ! |
|
122 |
||
123 |
!WriteStream methodsFor:'private accessing'! |
|
124 |
||
125 |
on:aCollection from:start to:last |
|
126 |
"create and return a new stream for writing onto aCollection, where |
|
127 |
writing is limited to the elements in the range start to last." |
|
128 |
||
129 |
super on:aCollection from:start to:last. |
|
130 |
writeLimit := last. |
|
131 |
! ! |
|
132 |
||
133 |
!WriteStream methodsFor:'queries'! |
|
134 |
||
135 |
isWritable |
|
1405 | 136 |
"return true, if writing is supported by the recevier. |
137 |
Always return true here" |
|
138 |
||
611 | 139 |
^ true |
1405 | 140 |
|
141 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
611 | 142 |
! ! |
143 |
||
63 | 144 |
!WriteStream methodsFor:'reading'! |
1 | 145 |
|
146 |
next |
|
10 | 147 |
"catch read access to write stream - report an error" |
1 | 148 |
|
10 | 149 |
self shouldNotImplement |
1 | 150 |
! |
151 |
||
152 |
peek |
|
10 | 153 |
"catch read access to write stream - report an error" |
1 | 154 |
|
10 | 155 |
self shouldNotImplement |
63 | 156 |
! ! |
157 |
||
611 | 158 |
!WriteStream methodsFor:'writing'! |
369 | 159 |
|
611 | 160 |
next:count put:anObject |
161 |
"append anObject count times to the receiver. |
|
162 |
Redefined to avoid count grows of the underlying collection - |
|
163 |
instead a single grow on the final size is performed." |
|
164 |
||
165 |
|final| |
|
166 |
||
167 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
|
168 |
^ super next:count put:anObject |
|
169 |
]. |
|
369 | 170 |
|
611 | 171 |
final := position + count - 1. |
172 |
(final > collection size) ifTrue:[ |
|
173 |
self growCollection:final |
|
174 |
]. |
|
369 | 175 |
|
611 | 176 |
position to:final do:[:index | |
177 |
collection at:index put:anObject. |
|
178 |
]. |
|
179 |
position := position + count. |
|
180 |
(position > readLimit) ifTrue:[readLimit := position - 1]. |
|
181 |
^ anObject |
|
182 |
! |
|
1 | 183 |
|
184 |
nextPut:anObject |
|
1584 | 185 |
"append the argument, anObject to the stream. |
186 |
Special tuned for appending to String, ByteArray and Array streams." |
|
1 | 187 |
|
63 | 188 |
%{ /* NOCONTEXT */ |
189 |
||
190 |
REGISTER int pos; |
|
191 |
unsigned ch; |
|
192 |
OBJ coll; |
|
1584 | 193 |
OBJ p, l, rL; |
194 |
int __readLimit = -1; |
|
63 | 195 |
|
1136 | 196 |
coll = __INST(collection); |
197 |
p = __INST(position); |
|
63 | 198 |
|
293 | 199 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
1584 | 200 |
pos = __intVal(p); |
201 |
l = __INST(writeLimit); |
|
68 | 202 |
|
1584 | 203 |
if ((l == nil) |
204 |
|| (__isSmallInteger(l) && (pos <= __intVal(l)))) { |
|
205 |
OBJ cls; |
|
63 | 206 |
|
1584 | 207 |
cls = __qClass(coll); |
252 | 208 |
|
1584 | 209 |
rL = __INST(readLimit); |
210 |
if (__isSmallInteger(rL)) { |
|
211 |
__readLimit = __intVal(rL); |
|
212 |
} |
|
293 | 213 |
|
1584 | 214 |
if (cls == @global(String)) { |
215 |
if (__isCharacter(anObject) |
|
216 |
&& (pos <= __stringSize(coll))) { |
|
217 |
ch = __intVal(_characterVal(anObject)); |
|
218 |
if ((ch >= 0) && (ch <= 255)) { |
|
219 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
220 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
221 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
222 |
__INST(readLimit) = __MKSMALLINT(pos); |
|
223 |
} |
|
224 |
RETURN ( anObject ); |
|
225 |
} |
|
226 |
} |
|
227 |
} else if (cls == @global(ByteArray)) { |
|
228 |
if (__isSmallInteger(anObject) |
|
229 |
&& ((ch = __intVal(anObject)) >= 0) |
|
230 |
&& (ch <= 255) |
|
231 |
&& (pos <= _byteArraySize(coll))) { |
|
232 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
233 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
234 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
235 |
__INST(readLimit) = __MKSMALLINT(pos); |
|
236 |
} |
|
237 |
RETURN ( anObject ); |
|
238 |
} |
|
239 |
} else if (cls == @global(Array)) { |
|
240 |
if (pos <= _arraySize(coll)) { |
|
241 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
|
242 |
__STORE(coll, anObject); |
|
243 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
244 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
245 |
__INST(readLimit) = __MKSMALLINT(pos); |
|
246 |
} |
|
247 |
RETURN ( anObject ); |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
63 | 251 |
} |
260 | 252 |
%}. |
293 | 253 |
(writeLimit isNil |
254 |
or:[position <= writeLimit]) ifTrue:[ |
|
1584 | 255 |
(position > collection size) ifTrue:[self growCollection]. |
256 |
collection at:position put:anObject. |
|
257 |
(position > readLimit) ifTrue:[readLimit := position]. |
|
258 |
position := position + 1. |
|
838 | 259 |
] ifFalse:[ |
1584 | 260 |
WriteErrorSignal raiseErrorString:'write behond writeLimit' |
293 | 261 |
]. |
1 | 262 |
^anObject |
263 |
! |
|
264 |
||
265 |
nextPutAll:aCollection |
|
63 | 266 |
"append all elements of the argument, aCollection to the stream. |
267 |
Redefined to avoid count grows of the underlying collection - |
|
268 |
instead a single grow on the final size is performed." |
|
1 | 269 |
|
270 |
|nMore final| |
|
271 |
||
77 | 272 |
collection isNil ifTrue:[ |
260 | 273 |
^ super nextPutAll:aCollection |
77 | 274 |
]. |
275 |
||
1 | 276 |
nMore := aCollection size. |
277 |
final := position + nMore - 1. |
|
293 | 278 |
(writeLimit notNil |
279 |
and:[final > writeLimit]) ifTrue:[ |
|
280 |
final := writeLimit. |
|
281 |
nMore := final - position + 1 |
|
282 |
]. |
|
1 | 283 |
(final > collection size) ifTrue:[ |
252 | 284 |
self growCollection:final |
1 | 285 |
]. |
286 |
collection replaceFrom:position |
|
252 | 287 |
to:final |
288 |
with:aCollection |
|
289 |
startingAt:1. |
|
1 | 290 |
|
291 |
position := position + nMore. |
|
292 |
(position > readLimit) ifTrue:[readLimit := position - 1]. |
|
293 |
^ aCollection |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
294 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
295 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
296 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
297 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
298 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
299 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
300 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
301 |
|nMore final| |
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 |
collection isNil ifTrue:[ |
1541 | 304 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
305 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
306 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
307 |
nMore := pos2 - pos1 + 1. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
308 |
final := position + nMore - 1. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
309 |
(writeLimit notNil |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
310 |
and:[final > writeLimit]) ifTrue:[ |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
311 |
final := writeLimit. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
312 |
nMore := final - position + 1 |
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 |
(final > collection size) ifTrue:[ |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
315 |
self growCollection:final |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
316 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
317 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
318 |
collection replaceFrom:position |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
319 |
to:final |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
320 |
with:aCollection |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
321 |
startingAt:pos1. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
322 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
323 |
position := position + nMore. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
324 |
(position > readLimit) ifTrue:[readLimit := position - 1]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
325 |
^ aCollection |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
326 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
327 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
328 |
|s| |
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 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
331 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
332 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
333 |
s contents |
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 |
|
1541 | 336 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
1 | 337 |
! ! |
338 |
||
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
339 |
!WriteStream class methodsFor:'documentation'! |
701 | 340 |
|
341 |
version |
|
1584 | 342 |
^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.31 1996-07-19 10:26:56 cg Exp $' |
701 | 343 |
! ! |