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 combinators ;
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 ( seq 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-seek ( n seek-type stream -- )
28 : stream-print ( str stream -- ) [ stream-write ] keep stream-nl ;
35 : readln ( -- str/f ) input-stream get stream-readln ;
36 : read1 ( -- elt ) input-stream get stream-read1 ;
37 : read ( n -- seq ) input-stream get stream-read ;
38 : read-until ( seps -- seq sep/f ) input-stream get stream-read-until ;
39 : read-partial ( n -- seq ) input-stream get stream-read-partial ;
40 : seek-input ( n seek-type -- ) input-stream get stream-seek ;
41 : seek-output ( n seek-type -- ) output-stream get stream-seek ;
43 : write1 ( elt -- ) output-stream get stream-write1 ;
44 : write ( seq -- ) output-stream get stream-write ;
45 : flush ( -- ) output-stream get stream-flush ;
47 : nl ( -- ) output-stream get stream-nl ;
49 : with-input-stream* ( stream quot -- )
50 input-stream swap with-variable ; inline
52 : with-input-stream ( stream quot -- )
53 [ with-input-stream* ] curry with-disposal ; inline
55 : with-output-stream* ( stream quot -- )
56 output-stream swap with-variable ; inline
58 : with-output-stream ( stream quot -- )
59 [ with-output-stream* ] curry with-disposal ; inline
61 : with-streams* ( input output quot -- )
62 [ output-stream set input-stream set ] prepose with-scope ; inline
64 : with-streams ( input output quot -- )
65 [ [ with-streams* ] 3curry ]
66 [ [ drop dispose dispose ] 3curry ] 3bi
69 : print ( str -- ) output-stream get stream-print ;
71 : bl ( -- ) " " write ;
75 : each-morsel ( handler: ( data -- ) reader: ( -- data ) -- )
76 [ dup ] compose swap while drop ; inline
78 : stream-element-exemplar ( type -- exemplar )
81 { +character+ [ "" ] }
84 : element-exemplar ( -- exemplar )
87 stream-element-exemplar ;
91 : each-line ( quot -- )
92 [ readln ] each-morsel ; inline
95 [ ] accumulator [ each-line ] dip { } like ;
97 : stream-lines ( stream -- seq )
98 [ lines ] with-input-stream ;
100 : contents ( -- seq )
101 [ 65536 read-partial dup ] [ ] produce nip
102 element-exemplar concat-as ;
104 : stream-contents ( stream -- seq )
105 [ contents ] with-input-stream ;
107 : each-block ( quot: ( block -- ) -- )
108 [ 8192 read-partial ] each-morsel ; inline
110 : stream-copy ( in out -- )
111 [ [ [ write ] each-block ] with-output-stream ]
112 curry with-input-stream ;