factor/library/io/stream.factor

56 lines
1.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: stdio
DEFER: stdio
2004-07-16 02:26:21 -04:00
IN: streams
USING: errors kernel namespaces strings generic lists ;
2004-07-16 02:26:21 -04:00
2005-02-14 22:15:02 -05:00
GENERIC: stream-flush ( stream -- )
GENERIC: stream-auto-flush ( stream -- )
GENERIC: stream-readln ( stream -- string )
GENERIC: stream-read ( count stream -- string )
GENERIC: stream-write-attr ( string style stream -- )
GENERIC: stream-close ( stream -- )
: stream-read1 ( stream -- char/f )
1 swap stream-read
dup f-or-"" [ drop f ] [ 0 swap str-nth ] ifte ;
2004-07-16 02:26:21 -04:00
2005-02-14 22:15:02 -05:00
: stream-write ( string stream -- )
f swap stream-write-attr ;
2004-07-16 02:26:21 -04:00
2005-02-14 22:15:02 -05:00
: stream-print ( string stream -- )
[ stream-write ] keep
[ "\n" swap stream-write ] keep
stream-auto-flush ;
! A stream that builds a string of all text written to it.
TUPLE: string-output buf ;
2004-07-16 02:26:21 -04:00
2005-02-14 22:15:02 -05:00
M: string-output stream-write-attr ( string style stream -- )
nip string-output-buf sbuf-append ;
2004-11-28 21:56:58 -05:00
2005-02-14 22:15:02 -05:00
M: string-output stream-close ( stream -- ) drop ;
M: string-output stream-flush ( stream -- ) drop ;
M: string-output stream-auto-flush ( stream -- ) drop ;
2004-08-10 23:48:08 -04:00
: stream>str ( stream -- string )
#! Returns the string written to the given string output
#! stream.
string-output-buf sbuf>str ;
2004-11-28 21:56:58 -05:00
C: string-output ( size -- stream )
2004-11-28 21:56:58 -05:00
#! Creates a new stream for writing to a string buffer.
[ >r <sbuf> r> set-string-output-buf ] keep ;
2004-12-25 21:28:47 -05:00
! Sometimes, we want to have a delegating stream that uses stdio
! words.
TUPLE: wrapper-stream delegate scope ;
2004-12-25 21:28:47 -05:00
C: wrapper-stream ( stream -- stream )
2dup set-wrapper-stream-delegate
2004-12-25 21:28:47 -05:00
[
>r <namespace> [ stdio set ] extend r>
set-wrapper-stream-scope
] keep ;