1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:'writeLimit'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams'
|
|
18 |
!
|
|
19 |
|
|
20 |
WriteStream comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
22 |
All Rights Reserved
|
68
|
23 |
'!
|
|
24 |
|
|
25 |
!WriteStream class methodsFor:'documentation'!
|
1
|
26 |
|
88
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.9 1994-06-02 16:22:49 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
68
|
47 |
documentation
|
|
48 |
"
|
88
|
49 |
Streams for writing into - this implementation currently DOES change the
|
|
50 |
identity if the streamed-upon collection IF it cannot grow.
|
|
51 |
Thus it is slightly incompatible to ST-80 since 'aStream contents' does
|
|
52 |
not always return the original collection. This may change.
|
68
|
53 |
"
|
|
54 |
! !
|
1
|
55 |
|
|
56 |
!WriteStream class methodsFor:'instance creation'!
|
|
57 |
|
|
58 |
on:aCollection from:start to:last
|
|
59 |
"create and return a new stream for writing onto aCollection, where
|
|
60 |
writing is limited to the elements in the range start to last."
|
|
61 |
|
|
62 |
|newStream|
|
68
|
63 |
|
1
|
64 |
newStream := super on:aCollection from:start to:last.
|
|
65 |
newStream writeLimit:last.
|
|
66 |
^ newStream
|
|
67 |
! !
|
|
68 |
|
|
69 |
!WriteStream methodsFor:'private'!
|
|
70 |
|
|
71 |
on:aCollection
|
|
72 |
"create and return a new stream for writing onto aCollection"
|
|
73 |
|
|
74 |
super on:aCollection.
|
|
75 |
writeLimit := readLimit
|
|
76 |
! !
|
|
77 |
|
|
78 |
!WriteStream methodsFor:'accessing'!
|
|
79 |
|
|
80 |
contents
|
|
81 |
"return the current contents (a collection) of the stream"
|
|
82 |
|
|
83 |
collection size == (position - 1) ifFalse:[
|
|
84 |
collection isFixedSize ifTrue:[
|
|
85 |
collection := collection copyFrom:1 to:(position - 1)
|
|
86 |
] ifFalse:[
|
|
87 |
collection grow:(position - 1)
|
|
88 |
]
|
|
89 |
].
|
|
90 |
^ collection
|
|
91 |
!
|
|
92 |
|
|
93 |
writeLimit:aNumber
|
|
94 |
"set the writeLimit, thats the position after which writing is
|
|
95 |
prohibited."
|
|
96 |
|
77
|
97 |
writeLimit := aNumber
|
1
|
98 |
! !
|
|
99 |
|
63
|
100 |
!WriteStream methodsFor:'reading'!
|
1
|
101 |
|
|
102 |
next
|
10
|
103 |
"catch read access to write stream - report an error"
|
1
|
104 |
|
10
|
105 |
self shouldNotImplement
|
1
|
106 |
!
|
|
107 |
|
|
108 |
peek
|
10
|
109 |
"catch read access to write stream - report an error"
|
1
|
110 |
|
10
|
111 |
self shouldNotImplement
|
63
|
112 |
! !
|
|
113 |
|
|
114 |
!WriteStream methodsFor:'writing'!
|
1
|
115 |
|
|
116 |
nextPut:anObject
|
|
117 |
"append the argument, anObject to the stream"
|
|
118 |
|
63
|
119 |
%{ /* NOCONTEXT */
|
|
120 |
|
|
121 |
#ifdef NOTDEF
|
|
122 |
|
|
123 |
REGISTER int pos;
|
|
124 |
unsigned ch;
|
|
125 |
OBJ coll;
|
68
|
126 |
OBJ t;
|
63
|
127 |
|
|
128 |
coll = _INST(collection);
|
|
129 |
if (_isNonNilObject(coll)
|
68
|
130 |
&& _isSmallInteger(t = _INST(position))) {
|
63
|
131 |
|
68
|
132 |
pos = _intVal(t);
|
|
133 |
|
|
134 |
if (_isSmallInteger(t = _INST(writeLimit))
|
|
135 |
&& (pos <= _intVal(t)) && (pos > 0)) {
|
63
|
136 |
OBJ cls;
|
|
137 |
|
|
138 |
cls = _qClass(coll);
|
68
|
139 |
#ifdef NOTDEF
|
63
|
140 |
if (cls == String) {
|
|
141 |
if (__isCharacter(anObject)
|
|
142 |
&& (pos <= _stringSize(coll))) {
|
|
143 |
_StringInstPtr(coll)->s_element[pos-1] = _intVal(_characterVal(anObject));
|
|
144 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
145 |
if (_isSmallInteger(_INST(readLimit))
|
|
146 |
&& (pos >= _intVal(_INST(readLimit)))) {
|
|
147 |
_INST(readLimit) = _MKSMALLINT(pos + 1);
|
|
148 |
}
|
|
149 |
RETURN ( anObject );
|
|
150 |
}
|
68
|
151 |
} else
|
|
152 |
#endif
|
77
|
153 |
if (cls == ByteArray) {
|
63
|
154 |
if (_isSmallInteger(anObject)
|
|
155 |
&& ((ch = _intVal(anObject)) >= 0)
|
|
156 |
&& (ch <= 255)
|
|
157 |
&& (pos <= _byteArraySize(coll))) {
|
|
158 |
_ByteArrayInstPtr(coll)->ba_element[pos-1] = ch;
|
|
159 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
160 |
if (_isSmallInteger(_INST(readLimit))
|
|
161 |
&& (pos >= _intVal(_INST(readLimit)))) {
|
|
162 |
_INST(readLimit) = _MKSMALLINT(pos + 1);
|
|
163 |
}
|
|
164 |
RETURN ( anObject );
|
|
165 |
}
|
68
|
166 |
#ifdef NOTDEF
|
|
167 |
} else
|
77
|
168 |
if (cls == Array) {
|
63
|
169 |
if (pos <= _arraySize(coll)) {
|
|
170 |
_ArrayInstPtr(coll)->a_element[pos-1] = anObject;
|
|
171 |
__STORE(coll, anObject);
|
|
172 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
173 |
if (_isSmallInteger(_INST(readLimit))
|
|
174 |
&& (pos >= _intVal(_INST(readLimit)))) {
|
|
175 |
_INST(readLimit) = _MKSMALLINT(pos + 1);
|
|
176 |
}
|
|
177 |
RETURN ( anObject );
|
|
178 |
}
|
68
|
179 |
#endif
|
63
|
180 |
}
|
|
181 |
}
|
|
182 |
}
|
|
183 |
#endif
|
|
184 |
|
|
185 |
%}
|
|
186 |
.
|
1
|
187 |
(position > collection size) ifTrue:[self growCollection].
|
|
188 |
collection at:position put:anObject.
|
|
189 |
(position > readLimit) ifTrue:[readLimit := position].
|
|
190 |
position := position + 1.
|
|
191 |
^anObject
|
|
192 |
!
|
|
193 |
|
63
|
194 |
next:count put:anObject
|
|
195 |
"append anObject count times to the receiver.
|
|
196 |
Redefined to avoid count grows of the underlying collection -
|
|
197 |
instead a single grow on the final size is performed."
|
|
198 |
|
68
|
199 |
|final|
|
63
|
200 |
|
77
|
201 |
collection isNil ifTrue:[
|
|
202 |
count timesRepeat:[
|
|
203 |
self nextPut:anObject
|
|
204 |
].
|
|
205 |
^ self
|
|
206 |
].
|
|
207 |
|
63
|
208 |
final := position + count - 1.
|
|
209 |
(final > collection size) ifTrue:[
|
|
210 |
self growCollection:final
|
|
211 |
].
|
|
212 |
collection atAll:(position to:final) put:anObject.
|
|
213 |
|
|
214 |
position := position + count.
|
|
215 |
(position > readLimit) ifTrue:[readLimit := position - 1].
|
|
216 |
^ anObject
|
|
217 |
!
|
|
218 |
|
1
|
219 |
nextPutAll:aCollection
|
63
|
220 |
"append all elements of the argument, aCollection to the stream.
|
|
221 |
Redefined to avoid count grows of the underlying collection -
|
|
222 |
instead a single grow on the final size is performed."
|
1
|
223 |
|
|
224 |
|nMore final|
|
|
225 |
|
77
|
226 |
collection isNil ifTrue:[
|
|
227 |
aCollection do:[:element |
|
|
228 |
self nextPut:element
|
|
229 |
].
|
|
230 |
^ self
|
|
231 |
].
|
|
232 |
|
1
|
233 |
nMore := aCollection size.
|
|
234 |
final := position + nMore - 1.
|
|
235 |
(final > collection size) ifTrue:[
|
|
236 |
self growCollection:final
|
|
237 |
].
|
|
238 |
collection replaceFrom:position
|
|
239 |
to:final
|
|
240 |
with:aCollection
|
|
241 |
startingAt:1.
|
|
242 |
|
|
243 |
position := position + nMore.
|
|
244 |
(position > readLimit) ifTrue:[readLimit := position - 1].
|
|
245 |
^ aCollection
|
|
246 |
! !
|
|
247 |
|
10
|
248 |
!WriteStream methodsFor:'ignored'!
|
|
249 |
|
|
250 |
bold
|
63
|
251 |
"change font to bold - ignored here.
|
|
252 |
- this allows WriteStreams to be compatible to PrinterStreams"
|
10
|
253 |
!
|
|
254 |
|
|
255 |
italic
|
63
|
256 |
"change font to italic - ignored here.
|
|
257 |
- this allows WriteStreams to be compatible to PrinterStreams"
|
10
|
258 |
!
|
|
259 |
|
77
|
260 |
boldItalic
|
|
261 |
"change font to italic - ignored here.
|
|
262 |
- this allows WriteStreams to be compatible to PrinterStreams"
|
|
263 |
!
|
|
264 |
|
10
|
265 |
normal
|
63
|
266 |
"change font to normal - ignored here.
|
|
267 |
- this allows WriteStreams to be compatible to PrinterStreams"
|
10
|
268 |
! !
|
|
269 |
|
1
|
270 |
!WriteStream methodsFor:'private'!
|
|
271 |
|
|
272 |
growCollection
|
|
273 |
|oldSize newSize newColl|
|
|
274 |
|
|
275 |
oldSize := collection size.
|
|
276 |
(oldSize == 0) ifTrue:[
|
|
277 |
newSize := 10
|
|
278 |
] ifFalse:[
|
|
279 |
newSize := oldSize * 2
|
|
280 |
].
|
|
281 |
collection isFixedSize ifTrue:[
|
|
282 |
newColl := collection species new:newSize.
|
|
283 |
newColl replaceFrom:1 to:oldSize with:collection startingAt:1.
|
|
284 |
collection := newColl
|
|
285 |
] ifFalse:[
|
|
286 |
collection grow:newSize.
|
|
287 |
].
|
|
288 |
writeLimit := newSize
|
|
289 |
!
|
|
290 |
|
|
291 |
growCollection:minNewSize
|
|
292 |
|oldSize newSize newColl|
|
|
293 |
|
|
294 |
oldSize := collection size.
|
|
295 |
(oldSize == 0) ifTrue:[
|
|
296 |
newSize := minNewSize
|
|
297 |
] ifFalse:[
|
|
298 |
newSize := oldSize * 2.
|
|
299 |
[newSize < minNewSize] whileTrue:[
|
|
300 |
newSize := newSize * 2
|
|
301 |
]
|
|
302 |
].
|
|
303 |
collection isFixedSize ifTrue:[
|
|
304 |
newColl := collection species new:newSize.
|
|
305 |
newColl replaceFrom:1 to:oldSize with:collection startingAt:1.
|
|
306 |
collection := newColl
|
|
307 |
] ifFalse:[
|
|
308 |
collection grow:newSize
|
|
309 |
].
|
|
310 |
writeLimit := newSize
|
|
311 |
! !
|