factor/library/win32/win32-stream.factor

177 lines
5.6 KiB
Factor
Raw Normal View History

2004-12-23 06:51:42 -05:00
! $Id$
!
2005-02-18 03:48:56 -05:00
! Copyright (C) 2004, 2005 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
USING: alien continuations generic io-internals kernel
kernel-internals lists math namespaces prettyprint sequences
io strings threads win32-api win32-io-internals ;
2004-12-23 06:51:42 -05:00
2005-02-12 02:23:38 -05:00
TUPLE: win32-stream this ; ! FIXME: rewrite using tuples
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
2005-07-23 02:11:07 -04:00
SYMBOL: stream
SYMBOL: timeout
SYMBOL: cutoff
2004-12-25 05:49:30 -05:00
2005-02-12 02:23:38 -05:00
: pending-error ( len/status -- len/status )
dup [ win32-throw-error ] unless ;
2004-12-25 05:49:30 -05:00
: 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
2005-05-01 18:56:31 -04:00
NULL over set-overlapped-ext-event ;
2004-12-25 05:49:30 -05:00
: 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
2005-07-23 02:11:07 -04:00
: update-timeout ( -- )
timeout get [ millis + cutoff set ] when* ;
2004-12-25 05:49:30 -05:00
: flush-output ( -- )
2005-07-23 02:11:07 -04:00
update-timeout [
stream get alloc-io-callback init-overlapped >r
handle get out-buffer get [ buffer@ ] keep buffer-length
2005-04-22 00:22:36 -04:00
NULL r> WriteFile [ handle-io-error ] unless stop
2005-02-12 02:23:38 -05:00
] callcc1 pending-error
2004-12-25 05:49:30 -05:00
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
2005-04-03 17:38:57 -04:00
>r ch>string r> >buffer ;
2005-01-02 16:14:34 -05:00
M: string do-write ( str -- )
dup length out-buffer get buffer-capacity <= [
out-buffer get >buffer
2004-12-25 05:49:30 -05:00
] [
dup length out-buffer get buffer-size > [
dup length out-buffer get buffer-extend do-write
2004-12-25 05:49:30 -05:00
] [ flush-output do-write ] ifte
] ifte ;
: fill-input ( -- )
2005-07-23 02:11:07 -04:00
update-timeout [
stream get alloc-io-callback init-overlapped >r
handle get in-buffer get [ buffer@ ] keep
2004-12-25 05:49:30 -05:00
buffer-capacity file-size get [ fileptr get - min ] when*
NULL r>
2005-04-22 00:22:36 -04:00
ReadFile [ handle-io-error ] unless stop
2005-02-12 02:23:38 -05:00
] callcc1 pending-error
2004-12-25 05:49:30 -05:00
2005-05-01 18:56:31 -04:00
dup in-buffer get n>buffer 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 ;
2005-05-05 16:51:38 -04:00
: >string-or-f ( sbuf -- str-or-? )
dup length 0 > [ >string ] [ drop f ] ifte ;
2004-12-26 21:40:45 -05:00
2004-12-25 05:49:30 -05:00
: do-read-count ( sbuf count -- str )
dup 0 = [
drop >string
2004-12-25 05:49:30 -05:00
] [
dup consume-input
dup length dup 0 = [
2005-05-05 16:51:38 -04:00
3drop >string-or-f
2004-12-25 05:49:30 -05:00
] [
>r swap r> - >r swap [ swap nappend ] keep r> do-read-count
2004-12-25 05:49:30 -05:00
] 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 ;
2005-07-23 02:11:07 -04:00
M: win32-stream stream-format ( str style stream -- )
win32-stream-this nip [ do-write ] bind ;
2004-12-23 06:51:42 -05:00
2005-02-14 22:15:02 -05:00
M: win32-stream stream-read ( count stream -- str )
win32-stream-this [ dup <sbuf> swap do-read-count ] bind ;
2004-12-23 06:51:42 -05:00
2005-07-23 02:11:07 -04:00
M: win32-stream stream-read1 ( stream -- str )
win32-stream-this [
1 consume-input dup length 0 = [ drop f ] when first
] bind ;
2005-02-14 22:15:02 -05:00
M: win32-stream stream-flush ( stream -- )
win32-stream-this [ maybe-flush-output ] bind ;
2004-12-26 21:40:45 -05:00
2005-07-23 02:11:07 -04:00
M: win32-stream stream-finish ( stream -- )
2004-12-26 21:40:45 -05:00
drop ;
2004-12-23 06:51:42 -05:00
2005-02-14 22:15:02 -05:00
M: win32-stream stream-close ( stream -- )
win32-stream-this [
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 )
win32-stream-this [ handle get ] bind ;
2004-12-26 21:40:45 -05:00
2005-07-23 02:11:07 -04:00
M: win32-stream set-timeout ( timeout stream -- )
win32-stream-this [ timeout set ] bind ;
M: win32-stream expire ( stream -- )
win32-stream-this [
timeout get [ millis cutoff get > [ handle get CancelIo ] when ] when
] bind ;
2004-12-23 06:51:42 -05:00
C: win32-stream ( handle -- stream )
swap [
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
2005-07-23 02:11:07 -04:00
dup stream set
] make-hash over set-win32-stream-this ;
2004-12-23 06:51:42 -05:00
: <win32-file-reader> ( path -- stream )
2005-07-23 02:11:07 -04:00
t f win32-open-file <win32-stream> <line-reader> ;
2004-12-23 06:51:42 -05:00
: <win32-file-writer> ( path -- stream )
2004-12-25 05:49:30 -05:00
f t win32-open-file <win32-stream> ;
2004-12-26 21:40:45 -05:00