| 
									
										
										
										
											2009-01-13 18:12:43 -05:00
										 |  |  | ! Copyright (C) 2003, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | USING: hashtables generic kernel math namespaces make sequences | 
					
						
							| 
									
										
										
										
											2009-05-10 17:39:51 -04:00
										 |  |  | continuations destructors assocs combinators ;
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							|  |  |  | GENERIC: stream-read ( n stream -- seq )
 | 
					
						
							|  |  |  | GENERIC: stream-read-until ( seps stream -- seq sep/f )
 | 
					
						
							|  |  |  | GENERIC: stream-read-partial ( n stream -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-05-08 21:35:37 -04:00
										 |  |  | GENERIC: stream-readln ( stream -- str/f )
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: stream-write1 ( elt stream -- )
 | 
					
						
							|  |  |  | GENERIC: stream-write ( seq 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-02-07 11:30:51 -05:00
										 |  |  | GENERIC: stream-seek ( n seek-type stream -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-07 02:03:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | : stream-print ( str stream -- ) [ stream-write ] keep stream-nl ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | ! Default streams | 
					
						
							|  |  |  | SYMBOL: input-stream | 
					
						
							|  |  |  | SYMBOL: output-stream | 
					
						
							|  |  |  | SYMBOL: error-stream | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | : readln ( -- str/f ) input-stream get stream-readln ;
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | : read1 ( -- elt ) input-stream get stream-read1 ;
 | 
					
						
							|  |  |  | : read ( n -- seq ) input-stream get stream-read ;
 | 
					
						
							|  |  |  | : read-until ( seps -- seq sep/f ) input-stream get stream-read-until ;
 | 
					
						
							|  |  |  | : read-partial ( n -- seq ) input-stream get stream-read-partial ;
 | 
					
						
							| 
									
										
										
										
											2009-02-07 11:30:51 -05:00
										 |  |  | : seek-input ( n seek-type -- ) input-stream get stream-seek ;
 | 
					
						
							|  |  |  | : seek-output ( n seek-type -- ) output-stream get stream-seek ;
 | 
					
						
							| 
									
										
										
										
											2008-01-18 19:43:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | : write1 ( elt -- ) output-stream get stream-write1 ;
 | 
					
						
							|  |  |  | : write ( seq -- ) output-stream get stream-write ;
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | : flush ( -- ) output-stream get stream-flush ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | : nl ( -- ) output-stream get stream-nl ;
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-streams* ( input output quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-05 18:31:46 -04:00
										 |  |  |     [ output-stream set input-stream set ] prepose with-scope ; inline
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-streams ( input output quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-05 18:31:46 -04:00
										 |  |  |     [ [ with-streams* ] 3curry ] | 
					
						
							| 
									
										
										
										
											2008-12-02 20:43:07 -05:00
										 |  |  |     [ [ drop dispose dispose ] 3curry ] 3bi
 | 
					
						
							| 
									
										
										
										
											2008-05-05 18:31:46 -04:00
										 |  |  |     [ ] cleanup ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | : print ( str -- ) output-stream get stream-print ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : bl ( -- ) " " write ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : each-morsel ( handler: ( data -- ) reader: ( -- data ) -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-17 20:19:49 -05:00
										 |  |  |     [ dup ] compose swap while drop ; inline
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-10 17:39:51 -04:00
										 |  |  | : stream-element-exemplar ( type -- exemplar )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { +byte+ [ B{ } ] } | 
					
						
							|  |  |  |         { +character+ [ "" ] } | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : element-exemplar ( -- exemplar )
 | 
					
						
							|  |  |  |     input-stream get
 | 
					
						
							|  |  |  |     stream-element-type
 | 
					
						
							|  |  |  |     stream-element-exemplar ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-01 17:43:18 -05:00
										 |  |  | : each-line ( quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  |     [ readln ] each-morsel ; inline
 | 
					
						
							| 
									
										
										
										
											2008-12-01 17:43:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-10 17:39:51 -04:00
										 |  |  | : lines ( -- seq )
 | 
					
						
							|  |  |  |     [ ] accumulator [ each-line ] dip { } like ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : stream-lines ( stream -- seq )
 | 
					
						
							|  |  |  |     [ lines ] with-input-stream ;
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 11:41:27 -04:00
										 |  |  | : contents ( -- seq )
 | 
					
						
							| 
									
										
										
										
											2009-05-10 17:39:51 -04:00
										 |  |  |     [ 65536 read-partial dup ] [ ] produce nip
 | 
					
						
							|  |  |  |     element-exemplar concat-as ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : stream-contents ( stream -- seq )
 | 
					
						
							|  |  |  |     [ contents ] with-input-stream ;
 | 
					
						
							| 
									
										
										
										
											2009-05-01 11:41:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 20:08:20 -05:00
										 |  |  | : each-block ( quot: ( block -- ) -- )
 | 
					
						
							|  |  |  |     [ 8192 read-partial ] each-morsel ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : stream-copy ( in out -- )
 | 
					
						
							|  |  |  |     [ [ [ write ] each-block ] with-output-stream ] | 
					
						
							| 
									
										
										
										
											2009-02-07 02:03:12 -05:00
										 |  |  |     curry with-input-stream ;
 |