factor/extra/io/windows/nt/files/files.factor

111 lines
2.9 KiB
Factor
Raw Normal View History

USING: continuations destructors io.buffers io.files io.backend
2008-05-13 19:24:46 -04:00
io.timeouts io.ports io.windows io.windows.nt.backend
2008-04-02 21:09:56 -04:00
kernel libc math threads windows windows.kernel32 system
2008-04-21 20:20:18 -04:00
alien.c-types alien.arrays alien.strings sequences combinators
combinators.lib sequences.lib ascii splitting alien strings
assocs namespaces io.files.private accessors ;
2007-09-20 18:09:08 -04:00
IN: io.windows.nt.files
2008-04-02 21:09:56 -04:00
M: winnt cwd
MAX_UNICODE_PATH dup "ushort" <c-array>
[ GetCurrentDirectory win32-error=0/f ] keep
2008-04-20 06:15:46 -04:00
utf16n alien>string ;
2008-04-02 21:09:56 -04:00
M: winnt cd
SetCurrentDirectory win32-error=0/f ;
: unicode-prefix ( -- seq )
"\\\\?\\" ; inline
2008-04-02 21:09:56 -04:00
M: winnt root-directory? ( path -- ? )
2008-03-27 17:22:24 -04:00
{
{ [ dup empty? ] [ f ] }
{ [ dup [ path-separator? ] all? ] [ t ] }
{ [ dup right-trim-separators
{ [ dup length 2 = ] [ dup second CHAR: : = ] } && nip ] [
t
] }
2008-04-11 13:56:11 -04:00
[ f ]
2008-03-27 17:22:24 -04:00
} cond nip ;
2008-03-25 20:52:07 -04:00
ERROR: not-absolute-path ;
: root-directory ( string -- string' )
{
[ dup length 2 >= ]
[ dup second CHAR: : = ]
[ dup first Letter? ]
2008-03-25 20:52:07 -04:00
} && [ 2 head ] [ not-absolute-path ] if ;
: prepend-prefix ( string -- string' )
dup unicode-prefix head? [
unicode-prefix prepend
] unless ;
2008-04-02 21:09:56 -04:00
M: winnt normalize-path ( string -- string' )
(normalize-path)
{ { CHAR: / CHAR: \\ } } substitute
prepend-prefix ;
2008-04-02 21:09:56 -04:00
M: winnt CreateFile-flags ( DWORD -- DWORD )
FILE_FLAG_OVERLAPPED bitor ;
2007-09-20 18:09:08 -04:00
2008-04-02 21:09:56 -04:00
M: winnt FileArgs-overlapped ( port -- overlapped )
2007-09-20 18:09:08 -04:00
make-overlapped ;
2008-05-06 05:26:46 -04:00
M: winnt open-append
[ dup file-info size>> ] [ drop 0 ] recover
>r (open-append) r> ;
2007-09-20 18:09:08 -04:00
: update-file-ptr ( n port -- )
port-handle
dup win32-file-ptr [
rot + swap set-win32-file-ptr
] [
2drop
] if* ;
2008-01-28 00:59:36 -05:00
: finish-flush ( overlapped port -- )
2007-09-20 18:09:08 -04:00
dup pending-error
2008-01-28 00:59:36 -05:00
tuck get-overlapped-result
2007-11-07 14:01:45 -05:00
dup pick update-file-ptr
swap buffer>> buffer-consume ;
2007-11-07 14:01:45 -05:00
2007-09-20 18:09:08 -04:00
: (flush-output) ( port -- )
dup make-FileArgs
2007-11-07 14:01:45 -05:00
tuck setup-write WriteFile
dupd overlapped-error? [
2008-01-28 00:59:36 -05:00
>r FileArgs-lpOverlapped r>
[ save-callback ] 2keep
2007-11-07 14:01:45 -05:00
[ finish-flush ] keep
dup buffer>> buffer-empty? [ drop ] [ (flush-output) ] if
2007-09-20 18:09:08 -04:00
] [
2drop
] if ;
2007-11-09 03:01:45 -05:00
: flush-output ( port -- )
2008-02-09 22:34:42 -05:00
[ [ (flush-output) ] with-timeout ] with-destructors ;
2007-09-20 18:09:08 -04:00
2008-05-11 18:44:39 -04:00
M: winnt flush-port
dup buffer>> buffer-empty? [ dup flush-output ] unless drop ;
2007-11-09 03:01:45 -05:00
2008-01-28 00:59:36 -05:00
: finish-read ( overlapped port -- )
2007-09-20 18:09:08 -04:00
dup pending-error
2008-01-28 00:59:36 -05:00
tuck get-overlapped-result dup zero? [
drop t >>eof drop
2007-09-20 18:09:08 -04:00
] [
dup pick buffer>> n>buffer
2007-09-20 18:09:08 -04:00
swap update-file-ptr
] if ;
: ((wait-to-read)) ( port -- )
dup make-FileArgs
2007-11-07 14:01:45 -05:00
tuck setup-read ReadFile
dupd overlapped-error? [
2008-01-28 00:59:36 -05:00
>r FileArgs-lpOverlapped r>
[ save-callback ] 2keep
2007-09-20 18:09:08 -04:00
finish-read
2008-01-31 13:27:37 -05:00
] [ 2drop ] if ;
2007-09-20 18:09:08 -04:00
2008-05-11 18:44:39 -04:00
M: winnt (wait-to-read) ( port -- )
2008-02-09 22:34:42 -05:00
[ [ ((wait-to-read)) ] with-timeout ] with-destructors ;