diff --git a/core/io/io.factor b/core/io/io.factor index 085f8f3e70..ea37c13dd7 100644 --- a/core/io/io.factor +++ b/core/io/io.factor @@ -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 -- ) -> 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 ; diff --git a/core/io/streams/sequence/sequence.factor b/core/io/streams/sequence/sequence.factor index 5ecbc321ce..22882d6a24 100644 --- a/core/io/streams/sequence/sequence.factor +++ b/core/io/streams/sequence/sequence.factor @@ -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 ;