io: fix with-streams to dispose the output stream first, move (stream-seek) word to io.streams.sequence where it belongs

release
Slava Pestov 2010-09-12 22:07:23 -07:00
parent d00cc766a4
commit 28eb59824d
2 changed files with 16 additions and 20 deletions

View File

@ -1,4 +1,4 @@
! Copyright (C) 2003, 2009 Slava Pestov. ! Copyright (C) 2003, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors combinators continuations destructors kernel USING: accessors combinators continuations destructors kernel
math namespaces sequences ; math namespaces sequences ;
@ -26,20 +26,6 @@ SINGLETONS: seek-absolute seek-relative seek-end ;
GENERIC: stream-tell ( stream -- n ) GENERIC: stream-tell ( stream -- n )
GENERIC: stream-seek ( n seek-type stream -- ) GENERIC: stream-seek ( n seek-type stream -- )
<PRIVATE
SLOT: i
: (stream-seek) ( n seek-type stream -- )
swap {
{ seek-absolute [ i<< ] }
{ seek-relative [ [ + ] change-i drop ] }
{ seek-end [ [ underlying>> length + ] [ i<< ] bi ] }
[ bad-seek-type ]
} case ;
PRIVATE>
: stream-print ( str stream -- ) [ stream-write ] [ stream-nl ] bi ; : stream-print ( str stream -- ) [ stream-write ] [ stream-nl ] bi ;
! Default streams ! Default streams
@ -76,12 +62,13 @@ SYMBOL: error-stream
[ with-output-stream* ] curry with-disposal ; inline [ with-output-stream* ] curry with-disposal ; inline
: with-streams* ( input output quot -- ) : with-streams* ( input output quot -- )
[ output-stream set input-stream set ] prepose with-scope ; inline swapd [ with-output-stream* ] curry with-input-stream* ; inline
: with-streams ( input output quot -- ) : with-streams ( input output quot -- )
[ [ with-streams* ] 3curry ] #! We have to dispose of the output stream first, so that
[ [ [ drop [ &dispose drop ] bi@ ] 3curry ] with-destructors ] 3bi #! if both streams point to the same FD, we get to flush the
[ ] cleanup ; inline #! buffer before closing the FD.
swapd [ with-output-stream ] curry with-input-stream ; inline
: print ( str -- ) output-stream get stream-print ; : print ( str -- ) output-stream get stream-print ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2009 Daniel Ehrenberg ! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: sequences io io.streams.plain kernel accessors math math.order USING: sequences io io.streams.plain kernel accessors math math.order
growable destructors ; growable destructors combinators ;
IN: io.streams.sequence IN: io.streams.sequence
! Readers ! Readers
@ -46,3 +46,12 @@ M: growable stream-write push-all ;
M: growable stream-flush drop ; M: growable stream-flush drop ;
INSTANCE: growable plain-writer INSTANCE: growable plain-writer
! Seeking
: (stream-seek) ( n seek-type stream -- )
swap {
{ seek-absolute [ i<< ] }
{ seek-relative [ [ + ] change-i drop ] }
{ seek-end [ [ underlying>> length + ] [ i<< ] bi ] }
[ bad-seek-type ]
} case ;