factor/library/io/c-streams.factor

55 lines
1.3 KiB
Factor
Raw Normal View History

2005-05-04 15:51:38 -04:00
! Copyright (C) 2004, 2005 Slava Pestov.
2006-01-16 02:48:15 -05:00
! See http://factorcode.org/license.txt for BSD license.
2005-05-04 15:51:38 -04:00
IN: io-internals
USING: errors kernel kernel-internals namespaces io strings ;
2005-05-04 15:51:38 -04:00
! Simple wrappers for ANSI C I/O functions, used for
! bootstrapping only.
! More elaborate platform-specific I/O code is used on Unix and
! Windows; see library/unix and library/win32.
2005-12-16 21:12:35 -05:00
TUPLE: c-stream in out ;
2005-05-04 15:51:38 -04:00
M: c-stream stream-write1
2005-12-16 21:12:35 -05:00
>r ch>string r> stream-write ;
M: c-stream stream-write
2005-12-16 21:12:35 -05:00
c-stream-out fwrite ;
2005-05-04 15:51:38 -04:00
M: c-stream stream-read1
c-stream-in dup [ fgetc ] when ;
2005-05-04 15:51:38 -04:00
M: c-stream stream-flush
2005-05-04 15:51:38 -04:00
c-stream-out [ fflush ] when* ;
M: c-stream stream-close
2005-05-04 15:51:38 -04:00
dup c-stream-in [ fclose ] when*
c-stream-out [ fclose ] when* ;
2006-02-03 20:01:31 -05:00
: <duplex-c-stream> ( in out -- stream )
>r f <c-stream> <line-reader> f r> <c-stream> <plain-writer>
<duplex-stream> ;
2005-05-04 15:51:38 -04:00
: init-io ( -- )
2006-02-03 20:01:31 -05:00
13 getenv 14 getenv <duplex-c-stream> stdio set ;
2005-05-04 15:51:38 -04:00
2005-08-23 15:50:32 -04:00
: io-multiplex ( ms -- ) drop ;
IN: io
2005-05-04 15:51:38 -04:00
: <file-reader> ( path -- stream )
2005-12-16 21:12:35 -05:00
"rb" fopen f <c-stream> <line-reader> ;
2005-05-04 15:51:38 -04:00
: <file-writer> ( path -- stream )
2005-12-16 21:12:35 -05:00
"wb" fopen f swap <c-stream> <plain-writer> ;
2005-05-04 15:51:38 -04:00
TUPLE: client-stream host port ;
2006-08-01 04:45:05 -04:00
TUPLE: c-stream-error ;
: c-stream-error ( -- * ) <c-stream-error> throw ;
2005-05-04 15:51:38 -04:00
: <client> c-stream-error ;
: <server> c-stream-error ;
: accept c-stream-error ;