factor/basis/io/pipes/pipes.factor

61 lines
1.5 KiB
Factor
Raw Normal View History

2009-03-16 07:17:18 -04:00
! Copyright (C) 2008, 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-05-13 19:24:46 -04:00
USING: io.encodings io.backend io.ports io.streams.duplex
io splitting grouping sequences namespaces kernel
2009-03-16 21:11:36 -04:00
destructors math concurrency.combinators accessors fry
2008-07-02 22:52:28 -04:00
arrays continuations quotations system vocabs.loader combinators ;
IN: io.pipes
TUPLE: pipe in out ;
2008-05-05 03:32:35 -04:00
M: pipe dispose ( pipe -- )
2008-05-15 00:23:12 -04:00
[ in>> dispose ] [ out>> dispose ] bi ;
HOOK: (pipe) io-backend ( -- pipe )
2008-05-05 04:15:24 -04:00
: <pipe> ( encoding -- stream )
[
2008-11-30 19:28:15 -05:00
[
(pipe) |dispose
[ in>> <input-port> ] [ out>> <output-port> ] bi
] dip <encoder-duplex>
] with-destructors ;
<PRIVATE
2008-06-08 16:32:55 -04:00
: ?reader ( handle/f -- stream )
[ <input-port> &dispose ] [ input-stream get ] if* ;
: ?writer ( handle/f -- stream )
[ <output-port> &dispose ] [ output-stream get ] if* ;
2009-03-16 07:17:18 -04:00
GENERIC: run-pipeline-element ( input-fd output-fd obj -- result )
M: callable run-pipeline-element
[
2009-03-16 07:17:18 -04:00
[ [ ?reader ] [ ?writer ] bi* ] dip
'[ _ call( -- result ) ] with-streams*
] with-destructors ;
: <pipes> ( n -- pipes )
[
2008-05-14 20:03:07 -04:00
[ (pipe) |dispose ] replicate
T{ pipe } [ prefix ] [ suffix ] bi
2 <clumps>
] with-destructors ;
2008-05-05 04:15:24 -04:00
PRIVATE>
2008-05-05 04:15:24 -04:00
2008-05-05 20:12:22 -04:00
: run-pipeline ( seq -- results )
[ length dup zero? [ drop { } ] [ 1 - <pipes> ] if ] keep
[
2008-11-30 19:28:15 -05:00
[ [ first in>> ] [ second out>> ] bi ] dip
run-pipeline-element
] 2parallel-map ;
2008-07-02 22:52:28 -04:00
{
{ [ os unix? ] [ "io.pipes.unix" require ] }
{ [ os winnt? ] [ "io.pipes.windows.nt" require ] }
2008-07-02 22:52:28 -04:00
[ ]
} cond