factor/core/io/c-streams.factor

74 lines
1.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2007 Slava Pestov.
2006-01-16 02:48:15 -05:00
! See http://factorcode.org/license.txt for BSD license.
IN: c-streams
USING: errors kernel kernel-internals namespaces io strings
sequences math ;
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 core/unix and core/win32.
2005-05-04 15:51:38 -04:00
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
>r 1string 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-read
>r >fixnum r> c-stream-in dup [ fread ] [ 2drop f ] if ;
M: c-stream stream-read1
c-stream-in dup [ fgetc ] when ;
2005-05-04 15:51:38 -04:00
: read-until-loop ( stream delim -- ch )
over stream-read1 dup [
dup pick memq? [ 2nip ] [ , read-until-loop ] if
] [
2nip
] if ;
M: c-stream stream-read-until
[ swap read-until-loop ] "" make swap
over empty? over not and [ 2drop f f ] when ;
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> ;
2006-10-18 17:17:56 -04:00
: init-c-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
IN: io-internals
2006-10-18 17:17:56 -04:00
: init-io init-c-io ;
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
2006-08-16 21:55:53 -04:00
: <client> ( host port -- stream ) c-stream-error ;
: <server> ( port -- server ) c-stream-error ;
: accept ( server -- stream ) c-stream-error ;