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