factor/library/io/win32-stream.factor

186 lines
5.3 KiB
Factor
Raw Normal View History

2004-12-23 06:51:42 -05:00
! :folding=indent:collapseFolds=1:
! $Id$
!
2004-12-29 02:16:03 -05:00
! Copyright (C) 2004 Mackenzie Straight.
2004-12-23 06:51:42 -05:00
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: win32-stream
USE: alien
2004-12-25 05:49:30 -05:00
USE: continuations
2004-12-23 06:51:42 -05:00
USE: generic
USE: kernel
2004-12-25 05:49:30 -05:00
USE: kernel-internals
2004-12-23 06:51:42 -05:00
USE: lists
USE: math
USE: namespaces
2004-12-25 05:49:30 -05:00
USE: prettyprint
2004-12-23 06:51:42 -05:00
USE: stdio
USE: streams
2004-12-25 05:49:30 -05:00
USE: strings
USE: threads
2004-12-23 06:51:42 -05:00
USE: win32-api
USE: win32-io-internals
TRAITS: win32-stream
2004-12-26 21:40:45 -05:00
GENERIC: win32-stream-handle
2005-01-02 16:14:34 -05:00
GENERIC: do-write
2004-12-25 05:49:30 -05:00
SYMBOL: handle
SYMBOL: in-buffer
SYMBOL: out-buffer
SYMBOL: fileptr
SYMBOL: file-size
: init-overlapped ( overlapped -- overlapped )
0 over set-overlapped-ext-internal
0 over set-overlapped-ext-internal-high
2004-12-27 06:56:05 -05:00
fileptr get dup 0 ? over set-overlapped-ext-offset
2004-12-25 05:49:30 -05:00
0 over set-overlapped-ext-offset-high
0 over set-overlapped-ext-event ;
: update-file-pointer ( whence -- )
2004-12-26 21:40:45 -05:00
file-size get [ fileptr [ + ] change ] [ drop ] ifte ;
2004-12-25 05:49:30 -05:00
: flush-output ( -- )
[
alloc-io-task init-overlapped >r
handle get out-buffer get [ buffer-pos ] keep buffer-length
2004-12-26 21:40:45 -05:00
NULL r> WriteFile [ handle-io-error ] unless (yield)
2004-12-25 05:49:30 -05:00
] callcc1
2004-12-26 21:40:45 -05:00
dup update-file-pointer
out-buffer get [ buffer-consume ] keep
2004-12-25 05:49:30 -05:00
buffer-length 0 > [ flush-output ] when ;
2004-12-26 21:40:45 -05:00
: maybe-flush-output ( -- )
out-buffer get buffer-length 0 > [ flush-output ] when ;
2005-01-02 16:14:34 -05:00
M: integer do-write ( int -- )
out-buffer get [ buffer-capacity 0 = [ flush-output ] when ] keep
buffer-append-char ;
M: string do-write ( str -- )
2004-12-25 05:49:30 -05:00
dup str-length out-buffer get buffer-capacity <= [
out-buffer get buffer-append
] [
dup str-length out-buffer get buffer-size > [
dup str-length out-buffer get buffer-extend do-write
] [ flush-output do-write ] ifte
] ifte ;
: fill-input ( -- )
[
alloc-io-task init-overlapped >r
handle get in-buffer get [ buffer-pos ] keep
buffer-capacity file-size get [ fileptr get - min ] when*
NULL r>
2004-12-26 21:40:45 -05:00
ReadFile [ handle-io-error ] unless (yield)
2004-12-25 05:49:30 -05:00
] callcc1
2004-12-26 21:40:45 -05:00
dup in-buffer get buffer-fill update-file-pointer ;
2004-12-25 05:49:30 -05:00
: consume-input ( count -- str )
in-buffer get buffer-length 0 = [ fill-input ] when
in-buffer get buffer-size min
dup in-buffer get buffer-first-n
swap in-buffer get buffer-consume ;
2004-12-26 21:40:45 -05:00
: sbuf>str-or-f ( sbuf -- str-or-? )
dup sbuf-length 0 > [ sbuf>str ] [ drop f ] ifte ;
2004-12-25 05:49:30 -05:00
: do-read-count ( sbuf count -- str )
dup 0 = [
drop sbuf>str
] [
dup consume-input
dup str-length dup 0 = [
2004-12-26 21:40:45 -05:00
3drop sbuf>str-or-f
2004-12-25 05:49:30 -05:00
] [
>r swap r> - >r swap [ sbuf-append ] keep r> do-read-count
] ifte
] ifte ;
2004-12-23 06:51:42 -05:00
2004-12-26 21:40:45 -05:00
: peek-input ( -- str )
1 in-buffer get buffer-first-n ;
: do-read-line ( sbuf -- str )
1 consume-input dup str-length 0 = [ drop sbuf>str-or-f ] [
dup "\r" = [
peek-input "\n" = [ 1 consume-input drop ] when
drop sbuf>str
] [
dup "\n" = [
peek-input "\r" = [ 1 consume-input drop ] when
drop sbuf>str
] [
over sbuf-append do-read-line
] ifte
] ifte
] ifte ;
2004-12-23 06:51:42 -05:00
M: win32-stream fwrite-attr ( str style stream -- )
2004-12-25 05:49:30 -05:00
nip [ do-write ] bind ;
2004-12-23 06:51:42 -05:00
M: win32-stream freadln ( stream -- str )
2004-12-26 21:40:45 -05:00
[ 80 <sbuf> do-read-line ] bind ;
2004-12-23 06:51:42 -05:00
M: win32-stream fread# ( count stream -- str )
2004-12-25 05:49:30 -05:00
[ dup <sbuf> swap do-read-count ] bind ;
2004-12-23 06:51:42 -05:00
M: win32-stream fflush ( stream -- )
2004-12-26 21:40:45 -05:00
[ maybe-flush-output ] bind ;
M: win32-stream fauto-flush ( stream -- )
drop ;
2004-12-23 06:51:42 -05:00
M: win32-stream fclose ( stream -- )
2004-12-25 05:49:30 -05:00
[
2004-12-26 21:40:45 -05:00
maybe-flush-output
2004-12-25 05:49:30 -05:00
handle get CloseHandle drop
in-buffer get buffer-free
out-buffer get buffer-free
] bind ;
2004-12-23 06:51:42 -05:00
2004-12-26 21:40:45 -05:00
M: win32-stream win32-stream-handle ( stream -- handle )
[ handle get ] bind ;
2004-12-23 06:51:42 -05:00
C: win32-stream ( handle -- stream )
2004-12-25 05:49:30 -05:00
[
2004-12-26 21:40:45 -05:00
dup NULL GetFileSize dup -1 = not [
2004-12-25 05:49:30 -05:00
file-size set
] [ drop f file-size set ] ifte
handle set
4096 <buffer> in-buffer set
4096 <buffer> out-buffer set
0 fileptr set
] extend ;
2004-12-23 06:51:42 -05:00
: <win32-filecr> ( path -- stream )
t f win32-open-file <win32-stream> ;
2004-12-25 05:49:30 -05:00
: <win32-filecw> ( path -- stream )
f t win32-open-file <win32-stream> ;
2004-12-26 21:40:45 -05:00