2019-10-18 09:05:06 -04:00
|
|
|
! Copyright (C) 2004, 2007 Slava Pestov.
|
2006-01-16 02:48:15 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2019-10-18 09:05:06 -04:00
|
|
|
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
|
2019-10-18 09:05:04 -04:00
|
|
|
! 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
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: c-stream stream-write1
|
2019-10-18 09:05:06 -04:00
|
|
|
>r 1string r> stream-write ;
|
2005-07-17 14:48:55 -04:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
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
|
|
|
|
2019-10-18 09:05:06 -04:00
|
|
|
M: c-stream stream-read
|
|
|
|
|
>r >fixnum r> c-stream-in dup [ fread ] [ 2drop f ] if ;
|
|
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: c-stream stream-read1
|
2005-06-19 00:23:01 -04:00
|
|
|
c-stream-in dup [ fgetc ] when ;
|
2005-05-04 15:51:38 -04:00
|
|
|
|
2019-10-18 09:05:06 -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 ;
|
|
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: c-stream stream-flush
|
2005-05-04 15:51:38 -04:00
|
|
|
c-stream-out [ fflush ] when* ;
|
|
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
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
|
|
|
|
2019-10-18 09:05:06 -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 ;
|
|
|
|
|
|
2005-06-19 17:50:35 -04:00
|
|
|
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 ;
|
2006-08-15 03:01:24 -04:00
|
|
|
: 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 ;
|