2005-03-29 19:58:22 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
IN: io-internals
|
2005-04-02 00:56:00 -05:00
|
|
|
USING: errors kernel math strings ;
|
2005-03-29 19:58:22 -05:00
|
|
|
|
|
|
|
: file-mode OCT: 0600 ;
|
|
|
|
|
|
|
|
: io-error ( n -- ) 0 < [ errno strerror throw ] when ;
|
|
|
|
|
|
|
|
: open-read ( path -- fd )
|
|
|
|
O_RDONLY file-mode sys-open dup io-error ;
|
|
|
|
|
|
|
|
: open-write ( path -- fd )
|
|
|
|
O_WRONLY O_CREAT bitor O_TRUNC bitor file-mode sys-open
|
|
|
|
dup io-error ;
|
2005-04-01 12:42:14 -05:00
|
|
|
|
2005-04-02 00:56:00 -05:00
|
|
|
: read-step ( fd buffer -- ? )
|
|
|
|
tuck dup buffer@ swap buffer-capacity sys-read
|
|
|
|
dup 0 >= [
|
|
|
|
swap buffer-inc-fill t
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: read-count-step ( sbuf count buffer -- ? )
|
|
|
|
2dup buffer-fill <= [
|
|
|
|
buffer> swap sbuf-append t
|
|
|
|
] [
|
|
|
|
buffer>> nip swap sbuf-append f
|
|
|
|
] ifte ;
|