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
|
611
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams'
|
1
|
18 |
!
|
|
19 |
|
68
|
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.
|
68
|
43 |
"
|
345
|
44 |
! !
|
|
45 |
|
1
|
46 |
!WriteStream methodsFor:'accessing'!
|
|
47 |
|
|
48 |
contents
|
345
|
49 |
"return the current contents (a collection) of the stream.
|
|
50 |
Currently, this returns the actual collection if possible
|
|
51 |
(and reset is implemented to create a new one) in contrast to
|
|
52 |
ST80, where contents returns a copy and reset only sets the writePointer.
|
|
53 |
The ST/X behavior creates less temporary garbage in the normal case
|
|
54 |
(whre things are written for the contents only) but may be incompatible
|
|
55 |
with some applications. Time will show, if this is to be changed."
|
1
|
56 |
|
260
|
57 |
|lastIndex|
|
|
58 |
|
|
59 |
lastIndex := position - 1.
|
|
60 |
collection size == lastIndex ifFalse:[
|
252
|
61 |
collection isFixedSize ifTrue:[
|
260
|
62 |
"
|
|
63 |
grow is expensive - return a copy.
|
|
64 |
(is this what users of writeStream expect ?
|
|
65 |
"
|
|
66 |
collection := collection copyFrom:1 to:lastIndex
|
252
|
67 |
] ifFalse:[
|
260
|
68 |
collection grow:lastIndex
|
252
|
69 |
]
|
1
|
70 |
].
|
|
71 |
^ collection
|
|
72 |
!
|
|
73 |
|
329
|
74 |
reset
|
345
|
75 |
"reset the stream; write anew.
|
|
76 |
See the comment in WriteStream>>contnts"
|
329
|
77 |
|
|
78 |
collection := collection species new:(collection size).
|
|
79 |
super reset
|
1
|
80 |
! !
|
|
81 |
|
611
|
82 |
!WriteStream methodsFor:'ignored'!
|
|
83 |
|
|
84 |
bold
|
|
85 |
"change font to bold - ignored here.
|
|
86 |
- this allows WriteStreams to be compatible to PrinterStreams"
|
|
87 |
!
|
|
88 |
|
|
89 |
boldItalic
|
|
90 |
"change font to italic - ignored here.
|
|
91 |
- this allows WriteStreams to be compatible to PrinterStreams"
|
|
92 |
!
|
|
93 |
|
|
94 |
italic
|
|
95 |
"change font to italic - ignored here.
|
|
96 |
- this allows WriteStreams to be compatible to PrinterStreams"
|
|
97 |
!
|
|
98 |
|
|
99 |
normal
|
|
100 |
"change font to normal - ignored here.
|
|
101 |
- this allows WriteStreams to be compatible to PrinterStreams"
|
|
102 |
! !
|
|
103 |
|
|
104 |
!WriteStream methodsFor:'positioning'!
|
|
105 |
|
|
106 |
position:index
|
|
107 |
"redefined to allow positioning past the readLimit"
|
|
108 |
|
|
109 |
((index > (collection size + 1)) or:[index < 0]) ifTrue: [^ self positionError].
|
|
110 |
position := index
|
|
111 |
! !
|
|
112 |
|
|
113 |
!WriteStream methodsFor:'private'!
|
|
114 |
|
|
115 |
growCollection
|
|
116 |
self growCollection:10
|
|
117 |
!
|
|
118 |
|
|
119 |
growCollection:minNewSize
|
|
120 |
"grow the streamed collection to at least minNewSize"
|
|
121 |
|
|
122 |
|oldSize newSize newColl|
|
|
123 |
|
|
124 |
oldSize := collection size.
|
|
125 |
(oldSize == 0) ifTrue:[
|
|
126 |
newSize := minNewSize
|
|
127 |
] ifFalse:[
|
|
128 |
newSize := oldSize * 2.
|
|
129 |
[newSize < minNewSize] whileTrue:[
|
|
130 |
newSize := newSize * 2
|
|
131 |
]
|
|
132 |
].
|
|
133 |
collection isFixedSize ifTrue:[
|
|
134 |
newColl := collection species new:newSize.
|
|
135 |
newColl replaceFrom:1 to:oldSize with:collection startingAt:1.
|
|
136 |
collection := newColl
|
|
137 |
] ifFalse:[
|
|
138 |
collection grow:newSize
|
|
139 |
].
|
|
140 |
! !
|
|
141 |
|
|
142 |
!WriteStream methodsFor:'private accessing'!
|
|
143 |
|
|
144 |
on:aCollection from:start to:last
|
|
145 |
"create and return a new stream for writing onto aCollection, where
|
|
146 |
writing is limited to the elements in the range start to last."
|
|
147 |
|
|
148 |
super on:aCollection from:start to:last.
|
|
149 |
writeLimit := last.
|
|
150 |
! !
|
|
151 |
|
|
152 |
!WriteStream methodsFor:'queries'!
|
|
153 |
|
|
154 |
isWritable
|
|
155 |
^ true
|
|
156 |
! !
|
|
157 |
|
63
|
158 |
!WriteStream methodsFor:'reading'!
|
1
|
159 |
|
|
160 |
next
|
10
|
161 |
"catch read access to write stream - report an error"
|
1
|
162 |
|
10
|
163 |
self shouldNotImplement
|
1
|
164 |
!
|
|
165 |
|
|
166 |
peek
|
10
|
167 |
"catch read access to write stream - report an error"
|
1
|
168 |
|
10
|
169 |
self shouldNotImplement
|
63
|
170 |
! !
|
|
171 |
|
611
|
172 |
!WriteStream methodsFor:'writing'!
|
369
|
173 |
|
611
|
174 |
next:count put:anObject
|
|
175 |
"append anObject count times to the receiver.
|
|
176 |
Redefined to avoid count grows of the underlying collection -
|
|
177 |
instead a single grow on the final size is performed."
|
|
178 |
|
|
179 |
|final|
|
|
180 |
|
|
181 |
(collection isNil or:[writeLimit notNil]) ifTrue:[
|
|
182 |
^ super next:count put:anObject
|
|
183 |
].
|
369
|
184 |
|
611
|
185 |
final := position + count - 1.
|
|
186 |
(final > collection size) ifTrue:[
|
|
187 |
self growCollection:final
|
|
188 |
].
|
369
|
189 |
|
611
|
190 |
position to:final do:[:index |
|
|
191 |
collection at:index put:anObject.
|
|
192 |
].
|
|
193 |
position := position + count.
|
|
194 |
(position > readLimit) ifTrue:[readLimit := position - 1].
|
|
195 |
^ anObject
|
|
196 |
!
|
1
|
197 |
|
|
198 |
nextPut:anObject
|
|
199 |
"append the argument, anObject to the stream"
|
|
200 |
|
63
|
201 |
%{ /* NOCONTEXT */
|
|
202 |
|
|
203 |
REGISTER int pos;
|
|
204 |
unsigned ch;
|
|
205 |
OBJ coll;
|
252
|
206 |
OBJ p, l;
|
63
|
207 |
|
1136
|
208 |
coll = __INST(collection);
|
|
209 |
p = __INST(position);
|
63
|
210 |
|
293
|
211 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) {
|
68
|
212 |
|
1136
|
213 |
pos = __intVal(p);
|
|
214 |
l = __INST(writeLimit);
|
63
|
215 |
|
293
|
216 |
if ((l == nil)
|
1136
|
217 |
|| (__isSmallInteger(l) && (pos <= __intVal(l)))) {
|
252
|
218 |
OBJ cls;
|
|
219 |
|
329
|
220 |
cls = __qClass(coll);
|
293
|
221 |
|
838
|
222 |
if (cls == @global(String)) {
|
252
|
223 |
if (__isCharacter(anObject)
|
1136
|
224 |
&& (pos <= __stringSize(coll))) {
|
|
225 |
ch = __intVal(_characterVal(anObject));
|
293
|
226 |
if ((ch >= 0) && (ch <= 255)) {
|
1136
|
227 |
__StringInstPtr(coll)->s_element[pos-1] = ch;
|
|
228 |
__INST(position) = __MKSMALLINT(pos + 1);
|
|
229 |
if (__isSmallInteger(__INST(readLimit))
|
|
230 |
&& (pos >= __intVal(__INST(readLimit)))) {
|
|
231 |
__INST(readLimit) = __MKSMALLINT(pos);
|
329
|
232 |
}
|
|
233 |
RETURN ( anObject );
|
252
|
234 |
}
|
|
235 |
}
|
293
|
236 |
} else {
|
838
|
237 |
if (cls == @global(ByteArray)) {
|
293
|
238 |
if (__isSmallInteger(anObject)
|
1136
|
239 |
&& ((ch = __intVal(anObject)) >= 0)
|
293
|
240 |
&& (ch <= 255)
|
|
241 |
&& (pos <= _byteArraySize(coll))) {
|
1136
|
242 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch;
|
|
243 |
__INST(position) = __MKSMALLINT(pos + 1);
|
|
244 |
if (__isSmallInteger(__INST(readLimit))
|
|
245 |
&& (pos >= __intVal(__INST(readLimit)))) {
|
|
246 |
__INST(readLimit) = __MKSMALLINT(pos);
|
329
|
247 |
}
|
|
248 |
RETURN ( anObject );
|
252
|
249 |
}
|
329
|
250 |
} else {
|
838
|
251 |
if (cls == @global(Array)) {
|
329
|
252 |
if (pos <= _arraySize(coll)) {
|
1136
|
253 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject;
|
329
|
254 |
__STORE(coll, anObject);
|
1136
|
255 |
__INST(position) = __MKSMALLINT(pos + 1);
|
|
256 |
if (__isSmallInteger(__INST(readLimit))
|
|
257 |
&& (pos >= __intVal(__INST(readLimit)))) {
|
|
258 |
__INST(readLimit) = __MKSMALLINT(pos);
|
329
|
259 |
}
|
|
260 |
RETURN ( anObject );
|
|
261 |
}
|
293
|
262 |
}
|
252
|
263 |
}
|
|
264 |
}
|
|
265 |
}
|
63
|
266 |
}
|
260
|
267 |
%}.
|
293
|
268 |
(writeLimit isNil
|
|
269 |
or:[position <= writeLimit]) ifTrue:[
|
|
270 |
(position > collection size) ifTrue:[self growCollection].
|
|
271 |
collection at:position put:anObject.
|
|
272 |
(position > readLimit) ifTrue:[readLimit := position].
|
|
273 |
position := position + 1.
|
838
|
274 |
] ifFalse:[
|
|
275 |
WriteErrorSignal raiseErrorString:'write behond writeLimit'
|
293
|
276 |
].
|
1
|
277 |
^anObject
|
|
278 |
!
|
|
279 |
|
|
280 |
nextPutAll:aCollection
|
63
|
281 |
"append all elements of the argument, aCollection to the stream.
|
|
282 |
Redefined to avoid count grows of the underlying collection -
|
|
283 |
instead a single grow on the final size is performed."
|
1
|
284 |
|
|
285 |
|nMore final|
|
|
286 |
|
77
|
287 |
collection isNil ifTrue:[
|
260
|
288 |
^ super nextPutAll:aCollection
|
77
|
289 |
].
|
|
290 |
|
1
|
291 |
nMore := aCollection size.
|
|
292 |
final := position + nMore - 1.
|
293
|
293 |
(writeLimit notNil
|
|
294 |
and:[final > writeLimit]) ifTrue:[
|
|
295 |
final := writeLimit.
|
|
296 |
nMore := final - position + 1
|
|
297 |
].
|
1
|
298 |
(final > collection size) ifTrue:[
|
252
|
299 |
self growCollection:final
|
1
|
300 |
].
|
|
301 |
collection replaceFrom:position
|
252
|
302 |
to:final
|
|
303 |
with:aCollection
|
|
304 |
startingAt:1.
|
1
|
305 |
|
|
306 |
position := position + nMore.
|
|
307 |
(position > readLimit) ifTrue:[readLimit := position - 1].
|
|
308 |
^ aCollection
|
|
309 |
! !
|
|
310 |
|
701
|
311 |
!WriteStream class methodsFor:'documentation'!
|
|
312 |
|
|
313 |
version
|
1136
|
314 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/WriteStr.st,v 1.25 1996-04-02 22:14:55 cg Exp $'
|
701
|
315 |
! !
|