WriteStream.st
author Claus Gittinger <cg@exept.de>
Thu, 23 Nov 1995 02:16:37 +0100
changeset 605 8b17f96bf05a
parent 530 07d0bce293c9
child 611 80bb0f1a7bab
permissions -rw-r--r--
checkin from browser

"
 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:''
       classVariableNames:''
       poolDictionaries:''
       category:'Streams'
!

!WriteStream class methodsFor:'documentation'!

copyright
"
 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.
"
!

version
    ^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.21 1995-11-11 15:28:41 cg Exp $'
!

documentation
"
    Streams for writing into - this implementation currently DOES change the 
    identity if the streamed-upon collection IF it cannot grow easily. 
    Collections which cannot grow easily are for example: Array, ByteArray and String.
    Thus it is slightly incompatible to ST-80 since 'aStream contents' does 
    not always return the original collection. This may change.
"
! !

!WriteStream methodsFor:'queries'!

isWritable
    ^ true
! !

!WriteStream methodsFor:'private accessing'!

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."

    super on:aCollection from:start to:last.
    writeLimit := last.
! !

!WriteStream methodsFor:'accessing'!

contents
    "return the current contents (a collection) of the stream.
     Currently, this returns the actual collection if possible
     (and reset is implemented to create a new one) in contrast to
     ST80, where contents returns a copy and reset only sets the writePointer.
     The ST/X behavior creates less temporary garbage in the normal case
     (whre things are written for the contents only) but may be incompatible
     with some applications. Time will show, if this is to be changed."

    |lastIndex|

    lastIndex := position - 1.
    collection size == lastIndex ifFalse:[
	collection isFixedSize ifTrue:[
	    "
	     grow is expensive - return a copy.
	     (is this what users of writeStream expect ?
	    "
	    collection := collection copyFrom:1 to:lastIndex
	] ifFalse:[
	    collection grow:lastIndex
	]
    ].
    ^ collection
!

reset
    "reset the stream; write anew.
     See the comment in WriteStream>>contnts"

    collection := collection species new:(collection size).
    super reset
! !

!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:'positioning'!

position:index
    "redefined to allow positioning past the readLimit"

    ((index > (collection size + 1)) or:[index < 0]) ifTrue: [^ self positionError].
    position := index
! !

!WriteStream methodsFor:'writing'!

nextPut:anObject
    "append the argument, anObject to the stream"

%{  /* NOCONTEXT */

    REGISTER int pos;
    unsigned ch;
    OBJ coll;
    OBJ p, l;

    coll = _INST(collection);
    p = _INST(position);

    if (__isNonNilObject(coll) && __isSmallInteger(p)) {

	pos = _intVal(p);
	l = _INST(writeLimit);

	if ((l == nil)
	 || (__isSmallInteger(l) && (pos <= _intVal(l)))) {
	    OBJ cls;

	    cls = __qClass(coll);

	    if (cls == String) {
		if (__isCharacter(anObject) 
		 && (pos <= _stringSize(coll))) {
		    ch = _intVal(_characterVal(anObject));
		    if ((ch >= 0) && (ch <= 255)) {
			_StringInstPtr(coll)->s_element[pos-1] = ch;
			_INST(position) = _MKSMALLINT(pos + 1);
			if (__isSmallInteger(_INST(readLimit))
			 && (pos >= _intVal(_INST(readLimit)))) {
			    _INST(readLimit) = _MKSMALLINT(pos);
			}
			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);
			}
			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);
			    }
			    RETURN ( anObject );
			}
		    }
		}
	    }
	}
    }
%}.
    (writeLimit isNil
    or:[position <= writeLimit]) ifTrue:[
	(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."

    |final|

    (collection isNil or:[writeLimit notNil]) ifTrue:[
	^ super next:count put:anObject
    ].

    final := position + count - 1.
    (final > collection size) ifTrue:[
	self growCollection:final
    ].

    position to:final do:[:index |
	collection at:index 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|

    collection isNil ifTrue:[
	^ super nextPutAll:aCollection
    ].

    nMore := aCollection size.
    final := position + nMore - 1.
    (writeLimit notNil
    and:[final > writeLimit]) ifTrue:[
	final := writeLimit.
	nMore := final - position + 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"
!

boldItalic
    "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
    self growCollection:10
!

growCollection:minNewSize
    "grow the streamed collection to at least 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
    ].
! !