factor/library/io/stdio.factor

44 lines
1.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-07-16 02:26:21 -04:00
IN: stdio
USING: errors kernel lists namespaces streams generic strings ;
2004-11-28 21:56:58 -05:00
2004-11-29 23:14:12 -05:00
SYMBOL: stdio
2005-02-14 22:15:02 -05:00
: flush ( -- ) stdio get stream-flush ;
: read-line ( -- string ) stdio get stream-readln ;
: read1 ( -- char ) stdio get stream-read1 ;
: read ( count -- string ) stdio get stream-read ;
: write ( string -- ) stdio get stream-write ;
: write-attr ( string style -- ) stdio get stream-write-attr ;
: print ( string -- ) stdio get stream-print ;
2004-11-28 21:56:58 -05:00
: terpri ( -- ) "\n" write ;
: crlf ( -- ) "\r\n" write ;
: bl ( -- ) " " write ;
2005-02-14 22:15:02 -05:00
: close ( -- ) stdio get stream-close ;
2004-07-16 02:26:21 -04:00
2004-08-30 20:24:19 -04:00
: write-icon ( resource -- )
#! Write an icon. Eg, /library/icons/File.png
2004-09-24 23:22:44 -04:00
"icon" swons unit "" swap write-attr ;
2004-08-30 20:24:19 -04:00
2004-07-16 02:26:21 -04:00
: with-stream ( stream quot -- )
#! Close the stream no matter what happends.
2004-11-29 23:14:12 -05:00
[ swap stdio set [ close rethrow ] catch ] with-scope ;
: with-stream* ( stream quot -- )
#! Close the stream if there is an error.
[
swap stdio set
[ [ close rethrow ] when* ] catch
] with-scope ;
: with-string ( quot -- str )
#! Execute a quotation, and push a string containing all
#! text printed by the quotation.
2005-04-19 20:28:01 -04:00
1024 <sbuf> [ call stdio get sbuf>string ] with-stream ;
TUPLE: stdio-stream ;
C: stdio-stream ( stream -- stream ) [ set-delegate ] keep ;
M: stdio-stream stream-auto-flush ( -- ) delegate stream-flush ;
M: stdio-stream stream-close ( -- ) drop ;