WriteStream.st
author Claus Gittinger <cg@exept.de>
Tue, 30 Apr 1996 17:39:28 +0200
changeset 1321 52e043fb7eaf
parent 1295 83f594f05c52
child 1398 6c594289c589
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.
"
!

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.

    [author:]
        Claus Gittinger
"
! !

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

bold
    "change font to bold - 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"
!

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

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

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

!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
    ].
! !

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

isWritable
    ^ true
! !

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

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
!

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 == @global(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 == @global(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 == @global(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.
    ] ifFalse:[
	WriteErrorSignal raiseErrorString:'write behond writeLimit'
    ].
    ^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 class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.26 1996-04-25 17:00:29 cg Exp $'
! !