FilteringStream.st
author Claus Gittinger <cg@exept.de>
Fri, 10 Jan 1997 21:51:24 +0100
changeset 472 33f423823933
parent 401 f97488701314
child 474 61cb199537b5
permissions -rw-r--r--
commentary & doc

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


Stream subclass:#FilteringStream
	instanceVariableNames:'inputStream outputStream filter unbound'
	classVariableNames:''
	poolDictionaries:''
	category:'Streams'
!

!FilteringStream class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996 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
"
    A FilteringStream can be connected to some input
    (from which elements are read via the ReadStream protocol),
    and/or to some output (to which elements are written via
    the WriteStream protocol.
    The FilteringStream itself performs some filtering/processing
    on the elements as they arrive.


    [instance variables:]
        inputStream     <Stream>  the stream from which elements are read
        outputStream    <Stream>  the stream to which elements are written
        filter          <Block>   the filter block;
        unbound         <Boolean> if true, the stream is unbound.

    [author:]
        Claus Gittinger

    [See also:]
        ReadStream WriteStream
"

!

examples
"
  filter random numbers:
                                                                [exBegin]
    |in filter|

    in := Random new.

    filter := FilteringLineStream basicNew.
    filter inputStream:in.
    filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]].

    20 timesRepeat:[
        Transcript showCR:(filter next printString).
    ]
                                                                [exEnd]

  filtering prime numbers:
                                                                [exBegin]
    |num generator primeFilter addFilter|

    num := 1.
    generator := Plug new.
    generator respondTo:#next
                   with:[num := num + 1. num].
    generator respondTo:#atEnd
                   with:[false].

    addFilter := [:prime | |newFilter|
        newFilter := FilteringLineStream basicNew.
        newFilter filter:[:num | (num \\ prime) == 0 ifTrue:[
                                    nil
                                 ] ifFalse:[
                                    num
                                 ]
                         ].
        newFilter inputStream:primeFilter.
        primeFilter := newFilter
    ].

    addFilter value:2.
    primeFilter inputStream:generator.

    10000 timesRepeat:[
        |nextPrime|

        nextPrime := primeFilter next.
        addFilter value:nextPrime.
        Transcript showCR:nextPrime.
    ]
                                                                [exEnd]
"



! !

!FilteringStream methodsFor:'access - pull-reading'!

filterUpToEnd
    "pull input from inputStream up to the end,
     push it filtered into the outputStream"

    [inputStream atEnd] whileFalse:[
        self nextPut:(inputStream next)
    ].

    "Modified: 2.7.1996 / 21:02:49 / cg"
    "Created: 2.7.1996 / 21:06:42 / cg"
!

next
    "pull input from inputStream and
     push it filtered into the outputStream"

    |input output|

    [inputStream atEnd] whileFalse:[
        "/ get an element
        input := inputStream next.
        "/ filter it - this may return nil, to eat it
        output := filter value:input.
        output notNil ifTrue:[
            "/ good - output it
            ^ output.
        ].
    ].
    ^ nil

    "Created: 2.7.1996 / 21:09:58 / cg"
    "Modified: 10.1.1997 / 20:23:36 / cg"
! !

!FilteringStream methodsFor:'access - push-writing'!

nextPut:something
    "push something through the filter"

    |output|

    "/ filter it
    output := filter value:something.
    output notNil ifTrue:[
        outputStream nextPut:output
    ]

    "Created: 2.7.1996 / 21:07:58 / cg"
    "Modified: 10.1.1997 / 20:23:56 / cg"
! !

!FilteringStream methodsFor:'accessing'!

filter
    "return the filter"

    ^ filter

    "Modified: 2.7.1996 / 21:03:36 / cg"
    "Created: 2.7.1996 / 21:06:42 / cg"
!

filter:something
    "set the filter"

    filter := something.

    "Modified: 2.7.1996 / 21:03:40 / cg"
    "Created: 2.7.1996 / 21:06:42 / cg"
!

inputStream
    "return the inputStream"

    ^ inputStream

    "Modified: 2.7.1996 / 21:03:43 / cg"
    "Created: 2.7.1996 / 21:06:42 / cg"
!

inputStream:something
    "set the inputStream"

    inputStream := something.

    "Modified: 2.7.1996 / 21:03:46 / cg"
    "Created: 2.7.1996 / 21:06:42 / cg"
!

outputStream
    "return the outputStream"

    ^ outputStream

    "Modified: 2.7.1996 / 21:03:49 / cg"
    "Created: 2.7.1996 / 21:06:42 / cg"
!

outputStream:something
    "set the outputStream"

    outputStream := something.

    "Modified: 2.7.1996 / 21:03:52 / cg"
    "Created: 2.7.1996 / 21:06:42 / cg"
!

unbound
    "return the unbound flag"

    ^ unbound

    "Created: 2.7.1996 / 21:25:40 / cg"
    "Modified: 10.1.1997 / 20:21:53 / cg"
!

unbound:something
    "set the unbound flag"

    unbound := something.

    "Created: 2.7.1996 / 21:25:40 / cg"
    "Modified: 10.1.1997 / 20:21:59 / cg"
! !

!FilteringStream methodsFor:'queries'!

atEnd
    "return true, if the receiver stream is at the end"

    unbound ifTrue:[^ false].
    ^ inputStream atEnd

    "Created: 2.7.1996 / 21:19:51 / cg"
    "Modified: 10.1.1997 / 20:22:20 / cg"
! !

!FilteringStream class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.2 1997-01-10 20:51:24 cg Exp $'
! !