"
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
PositionableStream subclass:#WriteStream
instanceVariableNames:'writeLimit'
classVariableNames:''
poolDictionaries:''
category:'Streams'
!
WriteStream comment:'
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
Streams for writing into - this implementation currently DOES change the identity if the
streamed-upon collection if it cannot grow. Thus its slightly incompatible to ST-80 since
aStream contents does not always return the original collection. This will change soon.
$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.6 1994-02-25 13:07:09 claus Exp $
'!
!WriteStream class methodsFor:'instance creation'!
on:aCollection from:start to:last
"create and return a new stream for writing onto aCollection, where
writing is limited to the elements in the range start to last."
|newStream|
newStream := super on:aCollection from:start to:last.
newStream writeLimit:last.
^ newStream
! !
!WriteStream methodsFor:'private'!
on:aCollection
"create and return a new stream for writing onto aCollection"
super on:aCollection.
writeLimit := readLimit
! !
!WriteStream methodsFor:'accessing'!
contents
"return the current contents (a collection) of the stream"
collection size == (position - 1) ifFalse:[
collection isFixedSize ifTrue:[
collection := collection copyFrom:1 to:(position - 1)
] ifFalse:[
collection grow:(position - 1)
]
].
^ collection
!
writeLimit:aNumber
"set the writeLimit, thats the position after which writing is
prohibited."
(aNumber < collection size) ifTrue:[writeLimit := aNumber]
! !
!WriteStream methodsFor:'reading'!
next
"catch read access to write stream - report an error"
self shouldNotImplement
!
peek
"catch read access to write stream - report an error"
self shouldNotImplement
! !
!WriteStream methodsFor:'writing'!
nextPut:anObject
"append the argument, anObject to the stream"
%{ /* NOCONTEXT */
#ifdef NOTDEF
REGISTER int pos;
unsigned ch;
OBJ coll;
coll = _INST(collection);
if (_isNonNilObject(coll)
&& _isSmallInteger(_INST(position))) {
pos = _intVal(_INST(position));
if (_isSmallInteger(_INST(writeLimit))
&& (pos <= _intVal(_INST(writeLimit)))
&& (pos > 0)) {
OBJ cls;
cls = _qClass(coll);
if (cls == String) {
if (__isCharacter(anObject)
&& (pos <= _stringSize(coll))) {
_StringInstPtr(coll)->s_element[pos-1] = _intVal(_characterVal(anObject));
_INST(position) = _MKSMALLINT(pos + 1);
if (_isSmallInteger(_INST(readLimit))
&& (pos >= _intVal(_INST(readLimit)))) {
_INST(readLimit) = _MKSMALLINT(pos + 1);
}
RETURN ( anObject );
}
} else if (cls == ByteArray) {
if (_isSmallInteger(anObject)
&& ((ch = _intVal(anObject)) >= 0)
&& (ch <= 255)
&& (pos <= _byteArraySize(coll))) {
_ByteArrayInstPtr(coll)->ba_element[pos-1] = ch;
_INST(position) = _MKSMALLINT(pos + 1);
if (_isSmallInteger(_INST(readLimit))
&& (pos >= _intVal(_INST(readLimit)))) {
_INST(readLimit) = _MKSMALLINT(pos + 1);
}
RETURN ( anObject );
}
} else if (cls == Array) {
if (pos <= _arraySize(coll)) {
_ArrayInstPtr(coll)->a_element[pos-1] = anObject;
__STORE(coll, anObject);
_INST(position) = _MKSMALLINT(pos + 1);
if (_isSmallInteger(_INST(readLimit))
&& (pos >= _intVal(_INST(readLimit)))) {
_INST(readLimit) = _MKSMALLINT(pos + 1);
}
RETURN ( anObject );
}
}
}
}
#endif
%}
.
(position > collection size) ifTrue:[self growCollection].
collection at:position put:anObject.
(position > readLimit) ifTrue:[readLimit := position].
position := position + 1.
^anObject
!
next:count put:anObject
"append anObject count times to the receiver.
Redefined to avoid count grows of the underlying collection -
instead a single grow on the final size is performed."
|nMore final|
final := position + count - 1.
(final > collection size) ifTrue:[
self growCollection:final
].
collection atAll:(position to:final) put:anObject.
position := position + count.
(position > readLimit) ifTrue:[readLimit := position - 1].
^ anObject
!
nextPutAll:aCollection
"append all elements of the argument, aCollection to the stream.
Redefined to avoid count grows of the underlying collection -
instead a single grow on the final size is performed."
|nMore final|
nMore := aCollection size.
final := position + nMore - 1.
(final > collection size) ifTrue:[
self growCollection:final
].
collection replaceFrom:position
to:final
with:aCollection
startingAt:1.
position := position + nMore.
(position > readLimit) ifTrue:[readLimit := position - 1].
^ aCollection
! !
!WriteStream methodsFor:'ignored'!
bold
"change font to bold - ignored here.
- this allows WriteStreams to be compatible to PrinterStreams"
!
italic
"change font to italic - ignored here.
- this allows WriteStreams to be compatible to PrinterStreams"
!
normal
"change font to normal - ignored here.
- this allows WriteStreams to be compatible to PrinterStreams"
! !
!WriteStream methodsFor:'private'!
growCollection
|oldSize newSize newColl|
oldSize := collection size.
(oldSize == 0) ifTrue:[
newSize := 10
] ifFalse:[
newSize := oldSize * 2
].
collection isFixedSize ifTrue:[
newColl := collection species new:newSize.
newColl replaceFrom:1 to:oldSize with:collection startingAt:1.
collection := newColl
] ifFalse:[
collection grow:newSize.
].
writeLimit := newSize
!
growCollection:minNewSize
|oldSize newSize newColl|
oldSize := collection size.
(oldSize == 0) ifTrue:[
newSize := minNewSize
] ifFalse:[
newSize := oldSize * 2.
[newSize < minNewSize] whileTrue:[
newSize := newSize * 2
]
].
collection isFixedSize ifTrue:[
newColl := collection species new:newSize.
newColl replaceFrom:1 to:oldSize with:collection startingAt:1.
collection := newColl
] ifFalse:[
collection grow:newSize
].
writeLimit := newSize
! !