1 ! Copyright (C) 2003, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators continuations destructors kernel
4 math namespaces sequences ;
7 SYMBOLS: +byte+ +character+ ;
9 GENERIC: stream-element-type ( stream -- type )
11 GENERIC: stream-read1 ( stream -- elt )
12 GENERIC: stream-read ( n stream -- seq )
13 GENERIC: stream-read-until ( seps stream -- seq sep/f )
14 GENERIC: stream-read-partial ( n stream -- seq )
15 GENERIC: stream-readln ( stream -- str/f )
17 GENERIC: stream-write1 ( elt stream -- )
18 GENERIC: stream-write ( data stream -- )
19 GENERIC: stream-flush ( stream -- )
20 GENERIC: stream-nl ( stream -- )
22 ERROR: bad-seek-type type ;
24 SINGLETONS: seek-absolute seek-relative seek-end ;
26 GENERIC: stream-tell ( stream -- n )
27 GENERIC: stream-seek ( n seek-type stream -- )
29 : stream-print ( str stream -- ) [ stream-write ] [ stream-nl ] bi ;
36 : readln ( -- str/f ) input-stream get stream-readln ;
37 : read1 ( -- elt ) input-stream get stream-read1 ;
38 : read ( n -- seq ) input-stream get stream-read ;
39 : read-until ( seps -- seq sep/f ) input-stream get stream-read-until ;
40 : read-partial ( n -- seq ) input-stream get stream-read-partial ;
41 : tell-input ( -- n ) input-stream get stream-tell ;
42 : tell-output ( -- n ) output-stream get stream-tell ;
43 : seek-input ( n seek-type -- ) input-stream get stream-seek ;
44 : seek-output ( n seek-type -- ) output-stream get stream-seek ;
46 : write1 ( elt -- ) output-stream get stream-write1 ;
47 : write ( seq -- ) output-stream get stream-write ;
48 : flush ( -- ) output-stream get stream-flush ;
50 : nl ( -- ) output-stream get stream-nl ;
52 : with-input-stream* ( stream quot -- )
53 input-stream swap with-variable ; inline
55 : with-input-stream ( stream quot -- )
56 [ with-input-stream* ] curry with-disposal ; inline
58 : with-output-stream* ( stream quot -- )
59 output-stream swap with-variable ; inline
61 : with-output-stream ( stream quot -- )
62 [ with-output-stream* ] curry with-disposal ; inline
64 : with-streams* ( input output quot -- )
65 swapd [ with-output-stream* ] curry with-input-stream* ; inline
67 : with-streams ( input output quot -- )
68 #! We have to dispose of the output stream first, so that
69 #! if both streams point to the same FD, we get to flush the
70 #! buffer before closing the FD.
71 swapd [ with-output-stream ] curry with-input-stream ; inline
73 : print ( str -- ) output-stream get stream-print ;
75 : bl ( -- ) " " write ;
77 : each-morsel ( ..a handler: ( ..a data -- ..b ) reader: ( ..b -- ..a data ) -- ..a )
78 [ dup ] compose swap while drop ; inline
82 : (stream-element-exemplar) ( type -- exemplar )
85 { +character+ [ "" ] }
88 : stream-element-exemplar ( stream -- exemplar )
89 stream-element-type (stream-element-exemplar) ; inline
93 : each-stream-line ( stream quot -- )
94 swap [ stream-readln ] curry each-morsel ; inline
96 : each-line ( quot -- )
97 input-stream get swap each-stream-line ; inline
99 : stream-lines ( stream -- seq )
100 [ [ ] collector [ each-stream-line ] dip { } like ] with-disposal ;
103 input-stream get stream-lines ; inline
105 : stream-contents ( stream -- seq )
107 [ [ 65536 swap stream-read-partial dup ] curry [ ] produce nip ]
108 [ stream-element-exemplar concat-as ] bi
111 : contents ( -- seq )
112 input-stream get stream-contents ; inline
114 : each-stream-block ( stream quot: ( block -- ) -- )
115 swap [ 8192 swap stream-read-partial ] curry each-morsel ; inline
117 : each-block ( quot: ( block -- ) -- )
118 input-stream get swap each-stream-block ; inline
120 : stream-copy ( in out -- )
121 [ [ [ write ] each-block ] with-output-stream ]
122 curry with-input-stream ;