factor/library/io/c-streams.factor

55 lines
1.4 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
2005-05-04 15:51:38 -04:00
strings threads ;
! 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.
TUPLE: c-stream in out flush? ;
M: c-stream stream-write1 ( char stream -- )
>r ch>string r> c-stream-out fwrite ;
M: c-stream stream-format ( str style stream -- )
nip 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-finish ( stream -- )
2005-09-24 15:21:17 -04:00
dup c-stream-flush? [ stream-flush ] [ drop ] if ;
2005-05-04 15:51:38 -04:00
M: c-stream stream-close ( stream -- )
dup c-stream-in [ fclose ] when*
c-stream-out [ fclose ] when* ;
: init-io ( -- )
13 getenv 14 getenv t <c-stream> <line-reader> 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 )
"rb" fopen f f <c-stream> <line-reader> ;
2005-05-04 15:51:38 -04:00
: <file-writer> ( path -- stream )
"wb" fopen f swap f <c-stream> ;
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 ;