Paste: throwing streams

Author: erg
Mode: factor
Date: Fri, 9 Jul 2010 17:39:17
Plain Text |
USING: accessors destructors io kernel locals namespaces
sequences ;
IN: io.streams.throwing

TUPLE: throws-on-eof stream ;

: <throws-on-eof> ( stream -- stream' )
    throws-on-eof new
        swap >>stream ; inline

: with-throws-on-eof ( stream quot -- )
    [ <throws-on-eof> ] dip with-input-stream ; inline

: with-input-throws-on-eof ( quot -- )
    [ input-stream get <throws-on-eof> ] dip with-input-stream ; inline

: input-throws-on-eof ( -- )
    input-stream get [ <throws-on-eof> ] change ; inline

GENERIC: silent-on-eof ( stream -- stream' )
M: throws-on-eof silent-on-eof stream>> ;
M: object silent-on-eof ;

: input-silent-on-eof ( -- )
    input-stream [ silent-on-eof ] change ;

M: throws-on-eof stream-element-type stream>> stream-element-type ;

M: throws-on-eof dispose stream>> dispose ;

ERROR: stream-exhausted-read1 stream ;
ERROR: stream-exhausted-read returned n stream ;
ERROR: stream-exhausted-read-partial n stream ;
ERROR: stream-exhausted-contents stream ;

M:: throws-on-eof stream-read1 ( stream -- obj )
    stream stream>> stream-read1 [ stream stream-exhausted-read1 ] unless* ;

M:: throws-on-eof stream-read ( n stream -- seq )
    n stream stream>> stream-read
    dup length n = [ n stream stream-exhausted-read ] unless ;

M:: throws-on-eof stream-read-partial ( n stream -- seq )
    n stream stream>> stream-read-partial
    [ n stream stream-exhausted-read-partial ] unless* ;

M:: throws-on-eof stream-contents ( stream -- seq )
    stream stream>> stream-contents
    [ stream stream-exhausted-contents ] when-empty ;

Annotation: throwing streams'

Author: j
Mode: factor
Date: Fri, 9 Jul 2010 17:46:57
Plain Text |
USING: accessors destructors io kernel locals namespaces
sequences ;
IN: io.streams.throwing

<PRIVATE

TUPLE: throws-on-eof stream ;

C: <throws-on-eof> throws-on-eof

M: throws-on-eof stream-element-type stream>> stream-element-type ;

M: throws-on-eof dispose stream>> dispose ;

ERROR: stream-exhausted stream word ;

M:: throws-on-eof stream-read1 ( stream -- obj )
    stream stream>> stream-read1 [ stream \ read1 stream-exhausted ] unless* ;

M:: throws-on-eof stream-read ( n stream -- seq )
    n stream stream>> stream-read
    dup length n = [ n stream \ read stream-exhausted ] unless ;

M:: throws-on-eof stream-read-partial ( n stream -- seq )
    n stream stream>> stream-read-partial
    [ n stream \ read-partial stream-exhausted ] unless* ;

PRIVATE>

: with-throws-on-eof ( stream quot -- )
    [ <throws-on-eof> ] dip with-input-stream ; inline

: with-input-throws-on-eof ( quot -- )
    [ input-stream get <throws-on-eof> ] dip with-input-stream ; inline

Annotation: revised error reporting

Author: j
Mode: factor
Date: Fri, 9 Jul 2010 17:56:47
Plain Text |
USING: accessors destructors io kernel locals namespaces
sequences ;
IN: io.streams.throwing

<PRIVATE

TUPLE: throws-on-eof stream ;

C: <throws-on-eof> throws-on-eof

M: throws-on-eof stream-element-type stream>> stream-element-type ;

M: throws-on-eof dispose stream>> dispose ;

ERROR: stream-exhausted n stream ;

M:: throws-on-eof stream-read1 ( stream -- obj )
    stream stream>> stream-read1 [ 1 stream stream-exhausted ] unless* ;

M:: throws-on-eof stream-read ( n stream -- seq )
    n stream stream>> stream-read
    dup length n = [ n stream stream-exhausted ] unless ;

M:: throws-on-eof stream-read-partial ( n stream -- seq )
    n stream stream>> stream-read-partial
    [ n stream stream-exhausted ] unless* ;

PRIVATE>

: with-throws-on-eof ( stream quot -- )
    [ <throws-on-eof> ] dip with-input-stream ; inline

: with-input-throws-on-eof ( quot -- )
    [ input-stream get <throws-on-eof> ] dip with-input-stream ; inline

New Annotation

Summary:
Author:
Mode:
Body: