| 
									
										
										
										
											2010-09-13 01:07:23 -04:00
										 |  |  | ! Copyright (C) 2003, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2013-03-05 13:34:47 -05:00
										 |  |  | USING: destructors kernel kernel.private math namespaces | 
					
						
							|  |  |  | sequences sequences.private ;
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:30:31 -04:00
										 |  |  | GENERIC: stream-read-unsafe ( n buf stream -- count )
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | GENERIC: stream-read-until ( seps stream -- seq sep/f )
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:30:31 -04:00
										 |  |  | GENERIC: stream-read-partial-unsafe ( n buf stream -- count )
 | 
					
						
							| 
									
										
										
										
											2008-05-08 21:35:37 -04:00
										 |  |  | GENERIC: stream-readln ( stream -- str/f )
 | 
					
						
							| 
									
										
										
										
											2012-08-25 20:03:51 -04:00
										 |  |  | GENERIC: stream-contents* ( stream -- seq )
 | 
					
						
							|  |  |  | : stream-contents ( stream -- seq ) [ stream-contents* ] with-disposal ;
 | 
					
						
							| 
									
										
										
										
											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 -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-14 21:07:38 -04:00
										 |  |  | GENERIC: stream-seekable? ( stream -- ? )
 | 
					
						
							|  |  |  | GENERIC: stream-length ( stream -- n/f )
 | 
					
						
							| 
									
										
										
										
											2009-02-07 02:03:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  | : stream-print ( str stream -- )
 | 
					
						
							|  |  |  |     [ stream-write ] [ stream-nl ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | ! Default streams | 
					
						
							| 
									
										
										
										
											2011-10-18 19:24:50 -04:00
										 |  |  | MIXIN: input-stream | 
					
						
							|  |  |  | MIXIN: output-stream | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | SYMBOL: error-stream | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:53:13 -04:00
										 |  |  | : readln ( -- str/f ) input-stream get stream-readln ; inline
 | 
					
						
							|  |  |  | : read1 ( -- elt ) input-stream get stream-read1 ; inline
 | 
					
						
							|  |  |  | : read-until ( seps -- seq sep/f ) input-stream get stream-read-until ; inline
 | 
					
						
							|  |  |  | : tell-input ( -- n ) input-stream get stream-tell ; inline
 | 
					
						
							|  |  |  | : tell-output ( -- n ) output-stream get stream-tell ; inline
 | 
					
						
							|  |  |  | : seek-input ( n seek-type -- ) input-stream get stream-seek ; inline
 | 
					
						
							|  |  |  | : seek-output ( n seek-type -- ) output-stream get stream-seek ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : write1 ( elt -- ) output-stream get stream-write1 ; inline
 | 
					
						
							|  |  |  | : write ( seq -- ) output-stream get stream-write ; inline
 | 
					
						
							|  |  |  | : flush ( -- ) output-stream get stream-flush ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : nl ( -- ) output-stream get stream-nl ; inline
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-18 17:28:41 -04:00
										 |  |  | : with-error-stream* ( stream quot -- )
 | 
					
						
							|  |  |  |     error-stream swap with-variable ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-error-stream ( stream quot -- )
 | 
					
						
							|  |  |  |     [ with-error-stream* ] curry with-disposal ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-output+error-stream* ( stream quot -- )
 | 
					
						
							|  |  |  |     [ dup ] dip [ with-error-stream* ] curry with-output-stream* ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-output+error-stream ( stream quot -- )
 | 
					
						
							|  |  |  |     [ with-output+error-stream* ] curry with-disposal ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-output>error ( quot -- )
 | 
					
						
							|  |  |  |     error-stream get swap with-output-stream* ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 03:56:11 -04:00
										 |  |  | : with-error>output ( quot -- )
 | 
					
						
							|  |  |  |     output-stream get swap with-error-stream* ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | : with-streams* ( input output quot -- )
 | 
					
						
							| 
									
										
										
										
											2010-09-13 01:07:23 -04:00
										 |  |  |     swapd [ with-output-stream* ] curry with-input-stream* ; inline
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-streams ( input output quot -- )
 | 
					
						
							| 
									
										
										
										
											2010-09-13 01:07:23 -04:00
										 |  |  |     #! We have to dispose of the output stream first, so that | 
					
						
							|  |  |  |     #! if both streams point to the same FD, we get to flush the | 
					
						
							|  |  |  |     #! buffer before closing the FD. | 
					
						
							|  |  |  |     swapd [ with-output-stream ] curry with-input-stream ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-18 17:28:41 -04:00
										 |  |  | : with-input-output+error-streams* ( input output+error quot -- )
 | 
					
						
							|  |  |  |     swapd [ with-output+error-stream* ] curry with-input-stream* ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-input-output+error-streams ( input output+error quot -- )
 | 
					
						
							|  |  |  |     swapd [ with-output+error-stream ] curry with-input-stream ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:53:13 -04:00
										 |  |  | : print ( str -- ) output-stream get stream-print ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-04 12:13:21 -04:00
										 |  |  | : stream-bl ( stream -- ) CHAR: \s swap stream-write1 ; inline
 | 
					
						
							| 
									
										
										
										
											2013-03-14 22:23:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : bl ( -- ) output-stream get stream-bl ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-12 16:50:38 -04:00
										 |  |  | : stream-exemplar ( stream -- exemplar )
 | 
					
						
							| 
									
										
										
										
											2012-07-16 22:30:10 -04:00
										 |  |  |     stream-element-type +byte+ = B{ } "" ? ; inline
 | 
					
						
							| 
									
										
										
										
											2009-10-23 01:07:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-12 16:50:38 -04:00
										 |  |  | : stream-exemplar-growable ( stream -- exemplar )
 | 
					
						
							| 
									
										
										
										
											2012-07-16 22:30:10 -04:00
										 |  |  |     stream-element-type +byte+ = BV{ } SBUF" " ? ; inline
 | 
					
						
							| 
									
										
										
										
											2009-05-10 17:39:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:30:31 -04:00
										 |  |  | : (new-sequence-for-stream) ( n stream -- seq )
 | 
					
						
							|  |  |  |     stream-exemplar new-sequence ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 17:29:12 -05:00
										 |  |  | : resize-if-necessary ( wanted-n got-n seq -- seq' )
 | 
					
						
							|  |  |  |     2over = [ [ 2drop ] dip ] [ resize nip ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-15 02:23:22 -04:00
										 |  |  | : (read-into-new) ( n stream quot -- seq/f )
 | 
					
						
							| 
									
										
										
										
											2011-11-09 17:29:12 -05:00
										 |  |  |     [ dup ] 2dip
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:30:31 -04:00
										 |  |  |     [ 2dup (new-sequence-for-stream) swap ] dip curry keep
 | 
					
						
							| 
									
										
										
										
											2011-11-09 17:29:12 -05:00
										 |  |  |     over 0 = [ 3drop f ] [ resize-if-necessary ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:30:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-15 02:23:22 -04:00
										 |  |  | : (read-into) ( buf stream quot -- buf-slice/f )
 | 
					
						
							| 
									
										
										
										
											2011-10-14 22:58:29 -04:00
										 |  |  |     [ dup length over ] 2dip call
 | 
					
						
							| 
									
										
										
										
											2011-11-09 20:21:44 -05:00
										 |  |  |     [ (head) <slice-unsafe> ] [ zero? not ] bi ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:30:31 -04:00
										 |  |  | : stream-read ( n stream -- seq/f )
 | 
					
						
							|  |  |  |     [ stream-read-unsafe ] (read-into-new) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : stream-read-partial ( n stream -- seq/f )
 | 
					
						
							|  |  |  |     [ stream-read-partial-unsafe ] (read-into-new) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 22:58:29 -04:00
										 |  |  | ERROR: invalid-read-buffer buf stream ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 20:21:44 -05:00
										 |  |  | : stream-read-into ( buf stream -- buf-slice more? )
 | 
					
						
							|  |  |  |     [ stream-read-unsafe { fixnum } declare ] (read-into) ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-18 00:12:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 20:21:44 -05:00
										 |  |  | : stream-read-partial-into ( buf stream -- buf-slice more? )
 | 
					
						
							|  |  |  |     [ stream-read-partial-unsafe { fixnum } declare ] (read-into) ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-14 22:58:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:53:13 -04:00
										 |  |  | : read ( n -- seq ) input-stream get stream-read ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-18 00:12:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:53:13 -04:00
										 |  |  | : read-partial ( n -- seq ) input-stream get stream-read-partial ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-18 00:12:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 20:21:44 -05:00
										 |  |  | : read-into ( buf -- buf-slice more? )
 | 
					
						
							| 
									
										
										
										
											2011-10-14 22:58:29 -04:00
										 |  |  |     input-stream get stream-read-into ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-18 00:12:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 20:21:44 -05:00
										 |  |  | : read-partial-into ( buf -- buf-slice more? )
 | 
					
						
							| 
									
										
										
										
											2011-10-14 22:58:29 -04:00
										 |  |  |     input-stream get stream-read-partial-into ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-13 17:30:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-25 21:30:52 -04:00
										 |  |  | : each-stream-line ( ... stream quot: ( ... line -- ... ) -- ... )
 | 
					
						
							| 
									
										
										
										
											2009-10-23 01:07:19 -04:00
										 |  |  |     swap [ stream-readln ] curry each-morsel ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-25 21:30:52 -04:00
										 |  |  | : each-line ( ... quot: ( ... line -- ... ) -- ... )
 | 
					
						
							| 
									
										
										
										
											2009-10-23 01:07:19 -04:00
										 |  |  |     input-stream get swap each-stream-line ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : stream-lines ( stream -- seq )
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         [ ] collector [ each-stream-line ] dip { } like
 | 
					
						
							|  |  |  |     ] with-disposal ; inline
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 22:11:04 -05:00
										 |  |  | CONSTANT: each-block-size 65536
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-19 16:46:39 -04:00
										 |  |  | : (each-stream-block-slice) ( ... stream quot: ( ... block-slice -- ... ) block-size -- ... )
 | 
					
						
							|  |  |  |     [ [ drop ] prepose swap ] dip
 | 
					
						
							|  |  |  |     [ swap (new-sequence-for-stream) ] curry keep
 | 
					
						
							| 
									
										
										
										
											2011-11-09 20:21:44 -05:00
										 |  |  |     [ stream-read-partial-into ] 2curry each-morsel drop ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-19 16:46:39 -04:00
										 |  |  | : each-stream-block-slice ( ... stream quot: ( ... block-slice -- ... ) -- ... )
 | 
					
						
							|  |  |  |     each-block-size (each-stream-block-slice) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (each-stream-block) ( ... stream quot: ( ... block -- ... ) block-size -- ... )
 | 
					
						
							|  |  |  |     rot [ stream-read-partial ] 2curry each-morsel ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-25 21:30:52 -04:00
										 |  |  | : each-stream-block ( ... stream quot: ( ... block -- ... ) -- ... )
 | 
					
						
							| 
									
										
										
										
											2013-09-19 16:46:39 -04:00
										 |  |  |     each-block-size (each-stream-block) ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-12 14:59:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-09 20:21:44 -05:00
										 |  |  | : each-block-slice ( ... quot: ( ... block -- ... ) -- ... )
 | 
					
						
							| 
									
										
										
										
											2011-11-09 21:04:46 -05:00
										 |  |  |     input-stream get swap each-stream-block-slice ; inline
 | 
					
						
							| 
									
										
										
										
											2011-11-09 20:21:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-25 21:30:52 -04:00
										 |  |  | : each-block ( ... quot: ( ... block -- ... ) -- ... )
 | 
					
						
							| 
									
										
										
										
											2011-03-25 16:58:16 -04:00
										 |  |  |     input-stream get swap each-stream-block ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 21:07:38 -04:00
										 |  |  | : (stream-contents-by-length) ( stream len -- seq )
 | 
					
						
							| 
									
										
										
										
											2012-08-25 20:03:51 -04:00
										 |  |  |     dup rot
 | 
					
						
							|  |  |  |     [ (new-sequence-for-stream) ] | 
					
						
							|  |  |  |     [ [ stream-read-unsafe ] curry keep resize ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 21:07:38 -04:00
										 |  |  | : (stream-contents-by-block) ( stream -- seq )
 | 
					
						
							| 
									
										
										
										
											2012-08-25 20:03:51 -04:00
										 |  |  |     [ [ ] collector [ each-stream-block ] dip { } like ] | 
					
						
							|  |  |  |     [ stream-exemplar concat-as ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 21:07:38 -04:00
										 |  |  | : (stream-contents-by-length-or-block) ( stream -- seq )
 | 
					
						
							|  |  |  |     dup stream-length
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  |     [ (stream-contents-by-length) ] | 
					
						
							| 
									
										
										
										
											2011-10-14 21:07:38 -04:00
										 |  |  |     [ (stream-contents-by-block)  ] if* ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 21:07:38 -04:00
										 |  |  | : (stream-contents-by-element) ( stream -- seq )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ [ stream-read1 dup ] curry [ ] ] | 
					
						
							|  |  |  |         [ stream-exemplar produce-as nip ] bi
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  |     ] with-disposal ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-12 14:59:10 -04: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 23:25:53 -04:00
										 |  |  | : stream-copy* ( in out -- )
 | 
					
						
							|  |  |  |     [ stream-write ] curry each-stream-block ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | : stream-copy ( in out -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-09 21:04:46 -05:00
										 |  |  |     [ [ stream-copy* ] with-disposal ] curry with-disposal ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-13 21:29:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Default implementations of stream operations in terms of read1/write1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2014-11-22 10:22:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 21:29:34 -04:00
										 |  |  | : read-loop ( buf stream n i -- count )
 | 
					
						
							|  |  |  |      2dup = [ nip nip nip ] [ | 
					
						
							|  |  |  |         pick stream-read1 [ | 
					
						
							|  |  |  |             over [ pick set-nth-unsafe ] 2curry 3dip
 | 
					
						
							|  |  |  |             1 + read-loop | 
					
						
							|  |  |  |         ] [ nip nip nip ] if*
 | 
					
						
							|  |  |  |      ] if ; inline recursive
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : finalize-read-until ( seq sep/f -- seq/f sep/f )
 | 
					
						
							|  |  |  |     2dup [ empty? ] [ not ] bi* and [ 2drop f f ] when ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : read-until-loop ( seps stream -- seq sep/f )
 | 
					
						
							|  |  |  |     [ [ stream-read1 dup [ rot member? not ] [ nip f ] if* ] 2curry [ ] ] | 
					
						
							|  |  |  |     [ stream-exemplar ] bi produce-as swap finalize-read-until ; inline
 | 
					
						
							| 
									
										
										
										
											2014-11-22 10:22:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 21:29:34 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  | M: input-stream stream-read-unsafe rot 0 read-loop ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-18 19:24:50 -04:00
										 |  |  | M: input-stream stream-read-partial-unsafe stream-read-unsafe ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-25 23:55:01 -04:00
										 |  |  | M: input-stream stream-read-until read-until-loop ; inline
 | 
					
						
							| 
									
										
										
										
											2014-11-22 10:22:17 -05:00
										 |  |  | M: input-stream stream-readln "\n" swap stream-read-until drop ; inline
 | 
					
						
							| 
									
										
										
										
											2012-08-25 20:03:51 -04:00
										 |  |  | M: input-stream stream-contents* (stream-contents-by-length-or-block) ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-18 19:24:50 -04:00
										 |  |  | M: input-stream stream-seekable? drop f ; inline
 | 
					
						
							|  |  |  | M: input-stream stream-length drop f ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: output-stream stream-write [ stream-write1 ] curry each ; inline
 | 
					
						
							|  |  |  | M: output-stream stream-flush drop ; inline
 | 
					
						
							|  |  |  | M: output-stream stream-nl CHAR: \n swap stream-write1 ; inline
 | 
					
						
							|  |  |  | M: output-stream stream-seekable? drop f ; inline
 | 
					
						
							|  |  |  | M: output-stream stream-length drop f ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-14 21:07:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 23:36:34 -05:00
										 |  |  | M: f stream-read1 drop f ; inline
 | 
					
						
							|  |  |  | M: f stream-read-unsafe 3drop 0 ; inline
 | 
					
						
							|  |  |  | M: f stream-read-until 2drop f f ; inline
 | 
					
						
							|  |  |  | M: f stream-read-partial-unsafe 3drop 0 ; inline
 | 
					
						
							|  |  |  | M: f stream-readln drop f ; inline
 | 
					
						
							| 
									
										
										
										
											2012-08-25 20:03:51 -04:00
										 |  |  | M: f stream-contents* drop f ; inline
 | 
					
						
							| 
									
										
										
										
											2011-11-28 23:36:34 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: f stream-write1 2drop ; inline
 | 
					
						
							|  |  |  | M: f stream-write 2drop ; inline
 | 
					
						
							|  |  |  | M: f stream-flush drop ; inline
 | 
					
						
							|  |  |  | M: f stream-nl drop ; inline
 |