From 28eb59824d9bba373559c6d967de15c1b5db67eb Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 12 Sep 2010 22:07:23 -0700 Subject: [PATCH] io: fix with-streams to dispose the output stream first, move (stream-seek) word to io.streams.sequence where it belongs --- core/io/io.factor | 25 ++++++------------------ core/io/streams/sequence/sequence.factor | 11 ++++++++++- 2 files changed, 16 insertions(+), 20 deletions(-) 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 ;