WriteStream.st
author claus
Tue, 02 May 1995 01:03:57 +0200
changeset 330 ae624fbef977
parent 329 f14fc5ac11b7
child 345 cf2301210c47
permissions -rw-r--r--
.

"
 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

$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.15 1995-05-01 21:40:01 claus Exp $
'!

!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.15 1995-05-01 21:40:01 claus 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 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:'accessing'!

contents
    "return the current contents (a collection) of the stream"

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

writeLimit:aNumber
    "set the writeLimit, thats the position after which writing is
     prohibited (i.e. silently ignored)."

    writeLimit := aNumber
!

reset
    "reset the stream; write anew"

    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:'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 + 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 );
			}
		    }
		}
	    }
	}
    }
%}.
    (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
    ].
! !