2009-01-13 18:12:43 -05:00
|
|
|
! Copyright (C) 2003, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-10-03 19:27:09 -04:00
|
|
|
USING: accessors combinators continuations destructors kernel
|
|
|
|
math namespaces sequences ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: io
|
|
|
|
|
2009-03-15 18:11:18 -04:00
|
|
|
SYMBOLS: +byte+ +character+ ;
|
|
|
|
|
|
|
|
GENERIC: stream-element-type ( stream -- type )
|
|
|
|
|
2009-01-22 20:08:20 -05:00
|
|
|
GENERIC: stream-read1 ( stream -- elt )
|
|
|
|
GENERIC: stream-read ( n stream -- seq )
|
|
|
|
GENERIC: stream-read-until ( seps stream -- seq sep/f )
|
|
|
|
GENERIC: stream-read-partial ( n stream -- seq )
|
2008-05-08 21:35:37 -04:00
|
|
|
GENERIC: stream-readln ( stream -- str/f )
|
2009-01-22 20:08:20 -05:00
|
|
|
|
|
|
|
GENERIC: stream-write1 ( elt stream -- )
|
2010-02-24 02:18:41 -05:00
|
|
|
GENERIC: stream-write ( data stream -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
GENERIC: stream-flush ( stream -- )
|
|
|
|
GENERIC: stream-nl ( stream -- )
|
|
|
|
|
2009-02-07 12:23:00 -05:00
|
|
|
ERROR: bad-seek-type type ;
|
2009-05-10 17:39:51 -04:00
|
|
|
|
2009-02-07 11:30:51 -05:00
|
|
|
SINGLETONS: seek-absolute seek-relative seek-end ;
|
2009-05-10 17:39:51 -04:00
|
|
|
|
2009-10-03 19:27:09 -04:00
|
|
|
GENERIC: stream-tell ( stream -- n )
|
2009-02-07 11:30:51 -05:00
|
|
|
GENERIC: stream-seek ( n seek-type stream -- )
|
2009-02-07 02:03:12 -05:00
|
|
|
|
2009-10-03 19:27:09 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
SLOT: i
|
|
|
|
|
|
|
|
: (stream-seek) ( n seek-type stream -- )
|
|
|
|
swap {
|
2010-05-05 16:52:54 -04:00
|
|
|
{ seek-absolute [ i<< ] }
|
2009-10-03 19:27:09 -04:00
|
|
|
{ seek-relative [ [ + ] change-i drop ] }
|
2010-05-05 16:52:54 -04:00
|
|
|
{ seek-end [ [ underlying>> length + ] [ i<< ] bi ] }
|
2009-10-03 19:27:09 -04:00
|
|
|
[ bad-seek-type ]
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: stream-print ( str stream -- ) [ stream-write ] [ stream-nl ] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-05 03:19:25 -04:00
|
|
|
! Default streams
|
|
|
|
SYMBOL: input-stream
|
|
|
|
SYMBOL: output-stream
|
|
|
|
SYMBOL: error-stream
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-05 03:19:25 -04:00
|
|
|
: readln ( -- str/f ) input-stream get stream-readln ;
|
2009-01-22 20:08:20 -05:00
|
|
|
: read1 ( -- elt ) input-stream get stream-read1 ;
|
|
|
|
: read ( n -- seq ) input-stream get stream-read ;
|
|
|
|
: read-until ( seps -- seq sep/f ) input-stream get stream-read-until ;
|
|
|
|
: read-partial ( n -- seq ) input-stream get stream-read-partial ;
|
2009-10-03 19:27:09 -04:00
|
|
|
: tell-input ( -- n ) input-stream get stream-tell ;
|
|
|
|
: tell-output ( -- n ) output-stream get stream-tell ;
|
2009-02-07 11:30:51 -05:00
|
|
|
: seek-input ( n seek-type -- ) input-stream get stream-seek ;
|
|
|
|
: seek-output ( n seek-type -- ) output-stream get stream-seek ;
|
2008-01-18 19:43:14 -05:00
|
|
|
|
2009-01-22 20:08:20 -05:00
|
|
|
: write1 ( elt -- ) output-stream get stream-write1 ;
|
|
|
|
: write ( seq -- ) output-stream get stream-write ;
|
2008-05-05 03:19:25 -04:00
|
|
|
: flush ( -- ) output-stream get stream-flush ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-05 03:19:25 -04:00
|
|
|
: nl ( -- ) output-stream get stream-nl ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-05 03:19:25 -04:00
|
|
|
: with-input-stream* ( stream quot -- )
|
|
|
|
input-stream swap with-variable ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-05 03:19:25 -04:00
|
|
|
: with-input-stream ( stream quot -- )
|
|
|
|
[ with-input-stream* ] curry with-disposal ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-05 03:19:25 -04:00
|
|
|
: with-output-stream* ( stream quot -- )
|
|
|
|
output-stream swap with-variable ; inline
|
|
|
|
|
|
|
|
: with-output-stream ( stream quot -- )
|
|
|
|
[ with-output-stream* ] curry with-disposal ; inline
|
|
|
|
|
|
|
|
: with-streams* ( input output quot -- )
|
2008-05-05 18:31:46 -04:00
|
|
|
[ output-stream set input-stream set ] prepose with-scope ; inline
|
2008-05-05 03:19:25 -04:00
|
|
|
|
|
|
|
: with-streams ( input output quot -- )
|
2008-05-05 18:31:46 -04:00
|
|
|
[ [ with-streams* ] 3curry ]
|
2008-12-02 20:43:07 -05:00
|
|
|
[ [ drop dispose dispose ] 3curry ] 3bi
|
2008-05-05 18:31:46 -04:00
|
|
|
[ ] cleanup ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-22 20:08:20 -05:00
|
|
|
: print ( str -- ) output-stream get stream-print ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: bl ( -- ) " " write ;
|
|
|
|
|
2010-03-09 03:56:07 -05:00
|
|
|
: each-morsel ( ..a handler: ( ..a data -- ..b ) reader: ( ..b -- ..a data ) -- ..a )
|
2009-02-17 20:19:49 -05:00
|
|
|
[ dup ] compose swap while drop ; inline
|
2009-01-22 20:08:20 -05:00
|
|
|
|
2009-10-23 01:07:19 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: (stream-element-exemplar) ( type -- exemplar )
|
2009-05-10 17:39:51 -04:00
|
|
|
{
|
|
|
|
{ +byte+ [ B{ } ] }
|
|
|
|
{ +character+ [ "" ] }
|
2009-10-23 01:07:19 -04:00
|
|
|
} case ; inline
|
|
|
|
|
|
|
|
: stream-element-exemplar ( stream -- exemplar )
|
2009-12-13 03:03:06 -05:00
|
|
|
stream-element-type (stream-element-exemplar) ; inline
|
2009-05-10 17:39:51 -04:00
|
|
|
|
2009-01-22 20:08:20 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2009-10-23 01:07:19 -04:00
|
|
|
: each-stream-line ( stream quot -- )
|
|
|
|
swap [ stream-readln ] curry each-morsel ; inline
|
|
|
|
|
2008-12-01 17:43:18 -05:00
|
|
|
: each-line ( quot -- )
|
2009-10-23 01:07:19 -04:00
|
|
|
input-stream get swap each-stream-line ; inline
|
|
|
|
|
|
|
|
: stream-lines ( stream -- seq )
|
2010-01-22 16:00:53 -05:00
|
|
|
[ [ ] collector [ each-stream-line ] dip { } like ] with-disposal ;
|
2008-12-01 17:43:18 -05:00
|
|
|
|
2009-05-10 17:39:51 -04:00
|
|
|
: lines ( -- seq )
|
2009-10-23 01:07:19 -04:00
|
|
|
input-stream get stream-lines ; inline
|
2009-05-10 17:39:51 -04:00
|
|
|
|
2009-10-23 01:07:19 -04:00
|
|
|
: stream-contents ( stream -- seq )
|
|
|
|
[
|
|
|
|
[ [ 65536 swap stream-read-partial dup ] curry [ ] produce nip ]
|
|
|
|
[ stream-element-exemplar concat-as ] bi
|
|
|
|
] with-disposal ;
|
2009-01-22 20:08:20 -05:00
|
|
|
|
2009-05-01 11:41:27 -04:00
|
|
|
: contents ( -- seq )
|
2009-10-23 01:07:19 -04:00
|
|
|
input-stream get stream-contents ; inline
|
2009-05-10 17:39:51 -04:00
|
|
|
|
2009-10-23 01:07:19 -04:00
|
|
|
: each-stream-block ( stream quot: ( block -- ) -- )
|
|
|
|
swap [ 8192 swap stream-read-partial ] curry each-morsel ; inline
|
2009-05-01 11:41:27 -04:00
|
|
|
|
2009-01-22 20:08:20 -05:00
|
|
|
: each-block ( quot: ( block -- ) -- )
|
2009-10-23 01:07:19 -04:00
|
|
|
input-stream get swap each-stream-block ; inline
|
2009-01-22 20:08:20 -05:00
|
|
|
|
|
|
|
: stream-copy ( in out -- )
|
|
|
|
[ [ [ write ] each-block ] with-output-stream ]
|
2009-02-07 02:03:12 -05:00
|
|
|
curry with-input-stream ;
|