factor/library/io/stream.factor

45 lines
1.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: io
2005-11-29 23:49:59 -05:00
USING: errors hashtables generic kernel math namespaces
sequences strings ;
2005-06-19 18:31:02 -04:00
SYMBOL: stdio
! Stream protocol.
GENERIC: stream-flush ( stream -- )
GENERIC: stream-finish ( stream -- )
GENERIC: stream-readln ( stream -- string )
GENERIC: stream-read1 ( stream -- char/f )
GENERIC: stream-read ( count stream -- string )
GENERIC: stream-write1 ( char stream -- )
GENERIC: stream-format ( string style stream -- )
GENERIC: stream-close ( stream -- )
GENERIC: set-timeout ( timeout stream -- )
: stream-write ( string stream -- )
2005-11-29 23:49:59 -05:00
H{ } swap stream-format ;
2005-02-14 22:15:02 -05:00
: stream-terpri ( stream -- )
"\n" over stream-write stream-finish ;
2005-02-14 22:15:02 -05:00
: stream-print ( string stream -- )
[ stream-write ] keep stream-terpri ;
2005-06-19 18:31:02 -04:00
: (stream-copy) ( in out -- )
4096 pick stream-read
2005-09-24 15:21:17 -04:00
[ over stream-write (stream-copy) ] [ 2drop ] if* ;
2005-06-19 18:31:02 -04:00
: stream-copy ( in out -- )
[ 2dup (stream-copy) ] [ stream-close stream-close ] cleanup ;
2005-06-19 18:31:02 -04:00
! Think '/dev/null'.
M: f stream-flush drop ;
M: f stream-finish drop ;
M: f stream-readln drop f ;
M: f stream-read 2drop f ;
M: f stream-read1 drop f ;
M: f stream-write1 2drop ;
M: f stream-format 3drop ;
M: f stream-close drop ;