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-02-24 20:52:17 -05:00
|
|
|
USING: errors generic kernel lists math namespaces strings ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-04-15 22:28:37 -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
|
2005-03-05 16:33:40 -05:00
|
|
|
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 ;
|
2004-12-15 16:57:29 -05:00
|
|
|
|
2005-04-15 22:28:37 -04:00
|
|
|
! 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 ;
|
|
|
|
|
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-03-05 16:33:40 -05:00
|
|
|
string-output-buf sbuf>string ;
|
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.
|
2005-03-08 22:54:59 -05:00
|
|
|
TUPLE: wrapper-stream scope ;
|
2004-12-25 21:28:47 -05:00
|
|
|
|
2005-01-30 15:57:25 -05:00
|
|
|
C: wrapper-stream ( stream -- stream )
|
2005-03-08 22:54:59 -05:00
|
|
|
2dup set-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 ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2005-04-15 22:28:37 -04:00
|
|
|
! 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.
|
2005-02-24 20:52:17 -05:00
|
|
|
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 ;
|