WriteStream.st
author claus
Fri, 16 Jul 1993 11:39:45 +0200
changeset 1 a27a279701f8
child 3 24d81bf47225
permissions -rw-r--r--
Initial revision

"
 COPYRIGHT (c) 1989-93 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-93 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.

%W% %E%
'!

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

next
    "catch read access to write stream"

    ^ self error:'WriteStreams cannot read'
!

peek
    "catch read access to write stream"

    ^ self error:'WriteStreams cannot read'
!

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

    (position > collection size) ifTrue:[self growCollection].
    collection at:position put:anObject.
    (position > readLimit) ifTrue:[readLimit := position].
    position := position + 1.
    ^anObject
!

nextPutAll:aCollection
    "append all elements of the argument, aCollection to the stream"

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

cr
    "append a carriage-return to the stream"

    self nextPut:(Character cr)
!

tab
    "append a tab-character to the stream"

    self nextPut:(Character tab)
!

crTab
    "append a carriage-return followed by a tab to the stream"

    self nextPut:(Character cr).
    self nextPut:(Character tab)
!

space
    "append a space character to the receiver-stream"

    self nextPut:(Character space)
!

spaces:count
    "append count space-characters to the receiver-stream"

    1 to:count do:[:dummy |
        self nextPut:(Character space)
    ]
!

ff
    "append a form-feed (new-pagee) to the receiver-stream"

    self nextPut:(Character ff)
! !

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