1 ! Copyright (C) 2003, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: destructors kernel kernel.private math namespaces
4 sequences sequences.private ;
7 SYMBOLS: +byte+ +character+ ;
9 GENERIC: stream-element-type ( stream -- type )
11 GENERIC: stream-read1 ( stream -- elt )
12 GENERIC: stream-read-unsafe ( n buf stream -- count )
13 GENERIC: stream-read-until ( seps stream -- seq sep/f )
14 GENERIC: stream-read-partial-unsafe ( n buf stream -- count )
15 GENERIC: stream-readln ( stream -- str/f )
16 GENERIC: stream-contents* ( stream -- seq )
17 : stream-contents ( stream -- seq ) [ stream-contents* ] with-disposal ;
19 GENERIC: stream-write1 ( elt stream -- )
20 GENERIC: stream-write ( data stream -- )
21 GENERIC: stream-flush ( stream -- )
22 GENERIC: stream-nl ( stream -- )
24 ERROR: bad-seek-type type ;
26 SINGLETONS: seek-absolute seek-relative seek-end ;
28 GENERIC: stream-tell ( stream -- n )
29 GENERIC: stream-seek ( n seek-type stream -- )
30 GENERIC: stream-seekable? ( stream -- ? )
31 GENERIC: stream-length ( stream -- n/f )
33 : stream-print ( str stream -- )
34 [ stream-write ] [ stream-nl ] bi ; inline
41 : readln ( -- str/f ) input-stream get stream-readln ; inline
42 : read1 ( -- elt ) input-stream get stream-read1 ; inline
43 : read-until ( seps -- seq sep/f ) input-stream get stream-read-until ; inline
44 : tell-input ( -- n ) input-stream get stream-tell ; inline
45 : tell-output ( -- n ) output-stream get stream-tell ; inline
46 : seek-input ( n seek-type -- ) input-stream get stream-seek ; inline
47 : seek-output ( n seek-type -- ) output-stream get stream-seek ; inline
49 : write1 ( elt -- ) output-stream get stream-write1 ; inline
50 : write ( seq -- ) output-stream get stream-write ; inline
51 : flush ( -- ) output-stream get stream-flush ; inline
53 : nl ( -- ) output-stream get stream-nl ; inline
55 : with-input-stream* ( stream quot -- )
56 input-stream swap with-variable ; inline
58 : with-input-stream ( stream quot -- )
59 [ with-input-stream* ] curry with-disposal ; inline
61 : with-output-stream* ( stream quot -- )
62 output-stream swap with-variable ; inline
64 : with-output-stream ( stream quot -- )
65 [ with-output-stream* ] curry with-disposal ; inline
67 : with-error-stream* ( stream quot -- )
68 error-stream swap with-variable ; inline
70 : with-error-stream ( stream quot -- )
71 [ with-error-stream* ] curry with-disposal ; inline
73 : with-output+error-stream* ( stream quot -- )
74 [ dup ] dip [ with-error-stream* ] curry with-output-stream* ; inline
76 : with-output+error-stream ( stream quot -- )
77 [ with-output+error-stream* ] curry with-disposal ; inline
79 : with-output>error ( quot -- )
80 error-stream get swap with-output-stream* ; inline
82 : with-error>output ( quot -- )
83 output-stream get swap with-error-stream* ; inline
85 : with-streams* ( input output quot -- )
86 swapd [ with-output-stream* ] curry with-input-stream* ; inline
88 : with-streams ( input output quot -- )
89 ! We have to dispose of the output stream first, so that
90 ! if both streams point to the same FD, we get to flush the
91 ! buffer before closing the FD.
92 swapd [ with-output-stream ] curry with-input-stream ; inline
94 : with-input-output+error-streams* ( input output+error quot -- )
95 swapd [ with-output+error-stream* ] curry with-input-stream* ; inline
97 : with-input-output+error-streams ( input output+error quot -- )
98 swapd [ with-output+error-stream ] curry with-input-stream ; inline
100 : print ( str -- ) output-stream get stream-print ; inline
102 : stream-bl ( stream -- ) CHAR: \s swap stream-write1 ; inline
104 : bl ( -- ) output-stream get stream-bl ;
108 : stream-exemplar ( stream -- exemplar )
109 stream-element-type +byte+ = B{ } "" ? ; inline
111 : stream-exemplar-growable ( stream -- exemplar )
112 stream-element-type +byte+ = BV{ } SBUF" " ? ; inline
114 : (new-sequence-for-stream) ( n stream -- seq )
115 stream-exemplar new-sequence ; inline
117 : resize-if-necessary ( wanted-n got-n seq -- seq' )
118 2over = [ 2nip ] [ resize nip ] if ; inline
120 : (read-into-new) ( n stream quot -- seq/f )
122 [ 2dup (new-sequence-for-stream) swap ] dip keepd
123 over 0 = [ 3drop f ] [ resize-if-necessary ] if ; inline
125 : (read-into) ( buf stream quot -- buf-slice/f )
126 [ dup length over ] 2dip call
127 [ (head) <slice-unsafe> ] [ zero? not ] bi ; inline
131 : stream-read ( n stream -- seq/f )
132 [ stream-read-unsafe ] (read-into-new) ; inline
134 : stream-read-partial ( n stream -- seq/f )
135 [ stream-read-partial-unsafe ] (read-into-new) ; inline
137 ERROR: invalid-read-buffer buf stream ;
139 : stream-read-into ( buf stream -- buf-slice more? )
140 [ stream-read-unsafe { fixnum } declare ] (read-into) ; inline
142 : stream-read-partial-into ( buf stream -- buf-slice more? )
143 [ stream-read-partial-unsafe { fixnum } declare ] (read-into) ; inline
145 : read ( n -- seq ) input-stream get stream-read ; inline
147 : read-partial ( n -- seq ) input-stream get stream-read-partial ; inline
149 : read-into ( buf -- buf-slice more? )
150 input-stream get stream-read-into ; inline
152 : read-partial-into ( buf -- buf-slice more? )
153 input-stream get stream-read-partial-into ; inline
155 : each-stream-line ( ... stream quot: ( ... line -- ... ) -- ... )
156 [ [ stream-readln ] curry ] dip while* ; inline
158 : each-line ( ... quot: ( ... line -- ... ) -- ... )
159 input-stream get swap each-stream-line ; inline
161 : stream-lines ( stream -- seq )
163 [ ] collector [ each-stream-line ] dip { } like
164 ] with-disposal ; inline
167 input-stream get stream-lines ; inline
169 CONSTANT: each-block-size 65536
171 : (each-stream-block-slice) ( ... stream quot: ( ... block-slice -- ... ) block-size -- ... )
173 [ (new-sequence-for-stream) ] keep
174 [ stream-read-partial-into ] 2curry
175 ] dip while drop ; inline
177 : each-stream-block-slice ( ... stream quot: ( ... block-slice -- ... ) -- ... )
178 each-block-size (each-stream-block-slice) ; inline
180 : (each-stream-block) ( ... stream quot: ( ... block -- ... ) block-size -- ... )
181 -rot [ [ stream-read-partial ] 2curry ] dip while* ; inline
183 : each-stream-block ( ... stream quot: ( ... block -- ... ) -- ... )
184 each-block-size (each-stream-block) ; inline
186 : each-block-slice ( ... quot: ( ... block -- ... ) -- ... )
187 input-stream get swap each-stream-block-slice ; inline
189 : each-block ( ... quot: ( ... block -- ... ) -- ... )
190 input-stream get swap each-stream-block ; inline
192 : (stream-contents-by-length) ( stream len -- seq )
194 [ (new-sequence-for-stream) ]
195 [ [ stream-read-unsafe ] keepd resize ] bi ; inline
197 : (stream-contents-by-block) ( stream -- seq )
198 [ [ ] collector [ each-stream-block ] dip { } like ]
199 [ stream-exemplar concat-as ] bi ; inline
201 : (stream-contents-by-length-or-block) ( stream -- seq )
203 [ (stream-contents-by-length) ]
204 [ (stream-contents-by-block) ] if* ; inline
206 : (stream-contents-by-element) ( stream -- seq )
208 [ [ stream-read1 dup ] curry [ ] ]
209 [ stream-exemplar produce-as nip ] bi
210 ] with-disposal ; inline
212 : contents ( -- seq )
213 input-stream get stream-contents ; inline
215 : stream-copy* ( in out -- )
216 [ stream-write ] curry each-stream-block ; inline
218 : stream-copy ( in out -- )
219 [ [ stream-copy* ] with-disposal ] curry with-disposal ; inline
221 ! Default implementations of stream operations in terms of read1/write1
225 : read-loop ( buf stream n i -- count )
228 over [ pick set-nth-unsafe ] 2curry 3dip
231 ] if ; inline recursive
233 : finalize-read-until ( seq sep/f -- seq/f sep/f )
234 2dup [ empty? ] [ not ] bi* and [ 2drop f f ] when ; inline
236 : read-until-loop ( seps stream -- seq sep/f )
237 [ [ stream-read1 dup [ rot member? not ] [ nip f ] if* ] 2curry [ ] ]
238 [ stream-exemplar ] bi produce-as swap finalize-read-until ; inline
242 M: input-stream stream-read-unsafe rot 0 read-loop ; inline
243 M: input-stream stream-read-partial-unsafe stream-read-unsafe ; inline
244 M: input-stream stream-read-until read-until-loop ; inline
245 M: input-stream stream-readln "\n" swap stream-read-until drop ; inline
246 M: input-stream stream-contents* (stream-contents-by-length-or-block) ; inline
247 M: input-stream stream-seekable? drop f ; inline
248 M: input-stream stream-length drop f ; inline
250 M: output-stream stream-write [ stream-write1 ] curry each ; inline
251 M: output-stream stream-flush drop ; inline
252 M: output-stream stream-nl CHAR: \n swap stream-write1 ; inline
253 M: output-stream stream-seekable? drop f ; inline
254 M: output-stream stream-length drop f ; inline
256 M: f stream-read1 drop f ; inline
257 M: f stream-read-unsafe 3drop 0 ; inline
258 M: f stream-read-until 2drop f f ; inline
259 M: f stream-read-partial-unsafe 3drop 0 ; inline
260 M: f stream-readln drop f ; inline
261 M: f stream-contents* drop f ; inline
263 M: f stream-write1 2drop ; inline
264 M: f stream-write 2drop ; inline
265 M: f stream-flush drop ; inline
266 M: f stream-nl drop ; inline