io: fix with-streams to dispose the output stream first, move (stream-seek) word to io.streams.sequence where it belongs
parent
d00cc766a4
commit
28eb59824d
|
@ -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.
|
||||
USING: accessors combinators continuations destructors kernel
|
||||
math namespaces sequences ;
|
||||
|
@ -26,20 +26,6 @@ SINGLETONS: seek-absolute seek-relative seek-end ;
|
|||
GENERIC: stream-tell ( stream -- n )
|
||||
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 ;
|
||||
|
||||
! Default streams
|
||||
|
@ -76,12 +62,13 @@ SYMBOL: error-stream
|
|||
[ with-output-stream* ] curry with-disposal ; inline
|
||||
|
||||
: 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* ] 3curry ]
|
||||
[ [ [ drop [ &dispose drop ] bi@ ] 3curry ] with-destructors ] 3bi
|
||||
[ ] cleanup ; inline
|
||||
#! We have to dispose of the output stream first, so that
|
||||
#! if both streams point to the same FD, we get to flush the
|
||||
#! buffer before closing the FD.
|
||||
swapd [ with-output-stream ] curry with-input-stream ; inline
|
||||
|
||||
: print ( str -- ) output-stream get stream-print ;
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
! Copyright (C) 2009 Daniel Ehrenberg
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: sequences io io.streams.plain kernel accessors math math.order
|
||||
growable destructors ;
|
||||
growable destructors combinators ;
|
||||
IN: io.streams.sequence
|
||||
|
||||
! Readers
|
||||
|
@ -46,3 +46,12 @@ M: growable stream-write push-all ;
|
|||
M: growable stream-flush drop ;
|
||||
|
||||
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 ;
|
||||
|
|
Loading…
Reference in New Issue