1 ! Copyright (C) 2003, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: hashtables generic kernel math namespaces make sequences
4 continuations destructors assocs ;
7 GENERIC: stream-readln ( stream -- str/f )
8 GENERIC: stream-read1 ( stream -- ch/f )
9 GENERIC: stream-read ( n stream -- str/f )
10 GENERIC: stream-read-until ( seps stream -- str/f sep/f )
11 GENERIC: stream-read-partial ( n stream -- str/f )
12 GENERIC: stream-write1 ( ch stream -- )
13 GENERIC: stream-write ( str stream -- )
14 GENERIC: stream-flush ( stream -- )
15 GENERIC: stream-nl ( stream -- )
17 : stream-print ( str stream -- )
18 [ stream-write ] keep stream-nl ;
20 : (stream-copy) ( in out -- )
21 64 1024 * pick stream-read-partial
22 [ over stream-write (stream-copy) ] [ 2drop ] if* ;
24 : stream-copy ( in out -- )
25 [ 2dup (stream-copy) ] [ dispose dispose ] [ ]
33 : readln ( -- str/f ) input-stream get stream-readln ;
34 : read1 ( -- ch/f ) input-stream get stream-read1 ;
35 : read ( n -- str/f ) input-stream get stream-read ;
36 : read-until ( seps -- str/f sep/f ) input-stream get stream-read-until ;
37 : read-partial ( n -- str/f ) input-stream get stream-read-partial ;
39 : write1 ( ch -- ) output-stream get stream-write1 ;
40 : write ( str -- ) output-stream get stream-write ;
41 : flush ( -- ) output-stream get stream-flush ;
43 : nl ( -- ) output-stream get stream-nl ;
45 : with-input-stream* ( stream quot -- )
46 input-stream swap with-variable ; inline
48 : with-input-stream ( stream quot -- )
49 [ with-input-stream* ] curry with-disposal ; inline
51 : with-output-stream* ( stream quot -- )
52 output-stream swap with-variable ; inline
54 : with-output-stream ( stream quot -- )
55 [ with-output-stream* ] curry with-disposal ; inline
57 : with-streams* ( input output quot -- )
58 [ output-stream set input-stream set ] prepose with-scope ; inline
60 : with-streams ( input output quot -- )
61 [ [ with-streams* ] 3curry ]
62 [ [ drop dispose dispose ] 3curry ] 3bi
65 : print ( string -- ) output-stream get stream-print ;
67 : bl ( -- ) " " write ;
69 : lines ( stream -- seq )
70 [ [ readln dup ] [ ] [ drop ] produce ] with-input-stream ;
72 : each-line ( quot -- )
73 [ [ readln dup ] ] dip [ drop ] while ; inline
75 : contents ( stream -- str )
77 [ 65536 read dup ] [ ] [ drop ] produce concat f like