factor/library/io/c-streams.factor

53 lines
1.3 KiB
Factor
Raw Normal View History

2005-05-04 15:51:38 -04:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
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 ( char stream -- )
2005-12-16 21:12:35 -05:00
>r ch>string r> stream-write ;
2005-12-16 21:12:35 -05:00
M: c-stream stream-write ( str stream -- )
c-stream-out fwrite ;
2005-05-04 15:51:38 -04:00
M: c-stream stream-read1 ( stream -- char/f )
c-stream-in dup [ fgetc ] when ;
2005-05-04 15:51:38 -04:00
M: c-stream stream-flush ( stream -- )
c-stream-out [ fflush ] when* ;
M: c-stream stream-close ( stream -- )
dup c-stream-in [ fclose ] when*
c-stream-out [ fclose ] when* ;
: init-io ( -- )
2005-12-16 21:12:35 -05:00
13 getenv f <c-stream> <line-reader>
f 14 getenv <c-stream> <plain-writer>
<duplex-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 ;
: c-stream-error
"C-streams I/O does not support this feature" throw ;
: <client> c-stream-error ;
: <server> c-stream-error ;
: accept c-stream-error ;