factor/library/io/stream.factor

100 lines
3.1 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 generic kernel lists math namespaces strings ;
2004-07-16 02:26:21 -04:00
! Stream protocol.
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 string-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 ;
! Think '/dev/null'.
TUPLE: null-stream ;
M: null-stream stream-flush drop ;
M: null-stream stream-auto-flush drop ;
M: null-stream stream-readln drop f ;
M: null-stream stream-read 2drop f ;
M: null-stream stream-write-attr 3drop ;
M: null-stream stream-close drop ;
! 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>string ;
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 scope ;
2004-12-25 21:28:47 -05:00
C: wrapper-stream ( stream -- stream )
2dup set-delegate
2004-12-25 21:28:47 -05:00
[
>r <namespace> [ stdio set ] extend r>
set-wrapper-stream-scope
] keep ;
! Combine an input and output stream into one, and flush the
! stream more often.
TUPLE: talk-stream in out ;
M: talk-stream stream-flush talk-stream-out stream-flush ;
M: talk-stream stream-auto-flush talk-stream-out stream-flush ;
M: talk-stream stream-readln talk-stream-in stream-readln ;
M: talk-stream stream-read talk-stream-in stream-read ;
M: talk-stream stream-write-attr talk-stream-out stream-write-attr ;
M: talk-stream stream-close talk-stream-out stream-close ;
! Reading lines and counting line numbers.
SYMBOL: line-number
SYMBOL: parser-stream
: next-line ( -- str )
parser-stream get stream-readln
line-number [ 1 + ] change ;
: read-lines ( stream quot -- )
#! Apply a quotation to each line as its read. Close the
#! stream.
swap [
parser-stream set 0 line-number set [ next-line ] while
] [
parser-stream get stream-close rethrow
] catch ;
2005-03-18 19:38:27 -05:00
! Standard actions protocol for presentations output to
! attributed streams.
: <actions> ( path alist -- alist )
#! For each element of the alist, change the value to
#! path " " value
[ uncons >r over " " r> cat3 cons ] map nip ;