| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2003, 2007 Slava Pestov. | 
					
						
							|  |  |  | ! 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 | 
					
						
							| 
									
										
										
										
											2008-07-28 23:28:13 -04:00
										 |  |  | continuations destructors assocs ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: io | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-08 21:35:37 -04:00
										 |  |  | GENERIC: stream-readln ( stream -- str/f )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | GENERIC: stream-read1 ( stream -- ch/f )
 | 
					
						
							|  |  |  | GENERIC: stream-read ( n stream -- str/f )
 | 
					
						
							|  |  |  | GENERIC: stream-read-until ( seps stream -- str/f sep/f )
 | 
					
						
							| 
									
										
										
										
											2008-09-17 00:52:04 -04:00
										 |  |  | GENERIC: stream-read-partial ( n stream -- str/f )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | GENERIC: stream-write1 ( ch stream -- )
 | 
					
						
							|  |  |  | GENERIC: stream-write ( str stream -- )
 | 
					
						
							|  |  |  | GENERIC: stream-flush ( stream -- )
 | 
					
						
							|  |  |  | GENERIC: stream-nl ( stream -- )
 | 
					
						
							|  |  |  | GENERIC: stream-format ( str style stream -- )
 | 
					
						
							|  |  |  | GENERIC: make-span-stream ( style stream -- stream' )
 | 
					
						
							|  |  |  | GENERIC: make-block-stream ( style stream -- stream' )
 | 
					
						
							|  |  |  | GENERIC: make-cell-stream ( style stream -- stream' )
 | 
					
						
							|  |  |  | GENERIC: stream-write-table ( table-cells style stream -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : stream-print ( str stream -- )
 | 
					
						
							|  |  |  |     [ stream-write ] keep stream-nl ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (stream-copy) ( in out -- )
 | 
					
						
							| 
									
										
										
										
											2007-11-18 01:10:58 -05:00
										 |  |  |     64 1024 * pick stream-read-partial
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     [ over stream-write (stream-copy) ] [ 2drop ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : stream-copy ( in out -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-31 01:52:06 -05:00
										 |  |  |     [ 2dup (stream-copy) ] [ dispose dispose ] [ ] | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     cleanup ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 ;
 | 
					
						
							|  |  |  | : read1 ( -- ch/f ) input-stream get stream-read1 ;
 | 
					
						
							|  |  |  | : read ( n -- str/f ) input-stream get stream-read ;
 | 
					
						
							|  |  |  | : read-until ( seps -- str/f sep/f ) input-stream get stream-read-until ;
 | 
					
						
							| 
									
										
										
										
											2008-05-11 18:43:17 -04:00
										 |  |  | : read-partial ( n -- str/f ) input-stream get stream-read-partial ;
 | 
					
						
							| 
									
										
										
										
											2008-01-18 19:43:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | : write1 ( ch -- ) output-stream get stream-write1 ;
 | 
					
						
							|  |  |  | : write ( str -- ) output-stream get stream-write ;
 | 
					
						
							|  |  |  | : 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 ;
 | 
					
						
							|  |  |  | : format ( str style -- ) output-stream get stream-format ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | : tabular-output ( style quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     swap [ { } make ] dip output-stream get stream-write-table ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-row ( quot -- )
 | 
					
						
							|  |  |  |     { } make , ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-cell ( quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  |     H{ } output-stream get make-cell-stream | 
					
						
							|  |  |  |     [ swap with-output-stream ] keep , ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : write-cell ( str -- )
 | 
					
						
							|  |  |  |     [ write ] with-cell ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-style ( style quot -- )
 | 
					
						
							|  |  |  |     swap dup assoc-empty? [ | 
					
						
							|  |  |  |         drop call
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  |         output-stream get make-span-stream swap with-output-stream
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-nesting ( style quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |     [ output-stream get make-block-stream ] dip
 | 
					
						
							|  |  |  |     with-output-stream ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  | : print ( string -- ) output-stream get stream-print ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : bl ( -- ) " " write ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : lines ( stream -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-07-10 02:00:27 -04:00
										 |  |  |     [ [ readln dup ] [ ] [ drop ] produce ] with-input-stream ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-01 17:43:18 -05:00
										 |  |  | : each-line ( quot -- )
 | 
					
						
							|  |  |  |     [ [ readln dup ] ] dip [ drop ] while ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : contents ( stream -- str )
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:21:51 -04:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-07-10 02:00:27 -04:00
										 |  |  |         [ 65536 read dup ] [ ] [ drop ] produce concat f like
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  |     ] with-input-stream ;
 |