2005-01-30 15:57:25 -05:00
|
|
|
! 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
|
2005-01-30 15:57:25 -05:00
|
|
|
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
|
2004-12-22 22:16:46 -05:00
|
|
|
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 ;
|
2004-12-15 16:57:29 -05:00
|
|
|
|
2005-01-30 15:57:25 -05:00
|
|
|
! 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 -- )
|
2005-01-30 15:57:25 -05:00
|
|
|
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.
|
2005-01-30 15:57:25 -05:00
|
|
|
string-output-buf sbuf>str ;
|
2004-11-28 21:56:58 -05:00
|
|
|
|
2005-01-30 15:57:25 -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.
|
2005-01-30 15:57:25 -05:00
|
|
|
[ >r <sbuf> r> set-string-output-buf ] keep ;
|
2004-12-25 21:28:47 -05:00
|
|
|
|
2005-01-30 15:57:25 -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
|
|
|
|
2005-01-30 15:57:25 -05:00
|
|
|
C: wrapper-stream ( stream -- stream )
|
|
|
|
2dup set-wrapper-stream-delegate
|
2004-12-25 21:28:47 -05:00
|
|
|
[
|
2005-01-30 15:57:25 -05:00
|
|
|
>r <namespace> [ stdio set ] extend r>
|
|
|
|
set-wrapper-stream-scope
|
|
|
|
] keep ;
|