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

126 lines
3.4 KiB
Factor
Raw Normal View History

USING: continuations destructors io.buffers io.files io.backend
2008-02-09 22:34:42 -05:00
io.timeouts io.nonblocking io.windows io.windows.nt.backend
2008-02-18 06:07:40 -05:00
kernel libc math concurrency.threads windows windows.kernel32
alien.c-types alien.arrays sequences combinators combinators.lib
sequences.lib ascii splitting alien strings assocs ;
2007-09-20 18:09:08 -04:00
IN: io.windows.nt.files
M: windows-nt-io cwd
MAX_UNICODE_PATH dup "ushort" <c-array>
[ GetCurrentDirectory win32-error=0/f ] keep
alien>u16-string ;
M: windows-nt-io cd
SetCurrentDirectory win32-error=0/f ;
: unicode-prefix ( -- seq )
"\\\\?\\" ; inline
M: windows-nt-io root-directory? ( path -- ? )
dup length 2 = [
dup first Letter?
swap second CHAR: : = and
] [
drop f
] if ;
: root-directory ( string -- string' )
{
[ dup length 2 >= ]
[ dup second CHAR: : = ]
[ dup first Letter? ]
} && [ 2 head ] [ "Not an absolute path" throw ] if ;
: prepend-prefix ( string -- string' )
unicode-prefix swap append ;
: windows-path+ ( cwd path -- newpath )
{
! empty
2008-02-05 20:16:22 -05:00
{ [ dup empty? ] [ drop ] }
! ..
{ [ dup ".." = ] [ drop parent-directory prepend-prefix ] }
! \\\\?\\c:\\foo
{ [ dup unicode-prefix head? ] [ nip ] }
! ..\\foo
2008-02-05 20:16:22 -05:00
{ [ dup "..\\" head? ] [ >r parent-directory r> 3 tail windows-path+ ] }
! .\\foo
{ [ dup ".\\" head? ] [ 1 tail append prepend-prefix ] }
! \\foo
{ [ dup "\\" head? ] [ >r root-directory r> append prepend-prefix ] }
! c:\\foo
2008-02-05 17:35:57 -05:00
{ [ dup ?second CHAR: : = ] [ nip prepend-prefix ] }
! foo.txt
2008-02-05 20:16:22 -05:00
{ [ t ] [
>r right-trim-separators "\\" r>
left-trim-separators
3append prepend-prefix
] }
} cond ;
M: windows-nt-io normalize-pathname ( string -- string )
dup string? [ "pathname must be a string" throw ] unless
2008-02-15 20:32:29 -05:00
{ { CHAR: / CHAR: \\ } } substitute
cwd swap windows-path+
[ "/\\." member? ] right-trim
dup peek CHAR: : = [ "\\" append ] when ;
M: windows-nt-io CreateFile-flags ( DWORD -- DWORD )
FILE_FLAG_OVERLAPPED bitor ;
2007-09-20 18:09:08 -04:00
M: windows-nt-io FileArgs-overlapped ( port -- overlapped )
make-overlapped ;
: 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-consume ;
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-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
2007-11-09 03:01:45 -05:00
M: port port-flush
dup buffer-empty? [ dup flush-output ] unless drop ;
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? [
2007-09-20 18:09:08 -04:00
drop t swap set-port-eof?
] [
2007-11-07 14:01:45 -05:00
dup pick 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
M: input-port (wait-to-read) ( port -- )
2008-02-09 22:34:42 -05:00
[ [ ((wait-to-read)) ] with-timeout ] with-destructors ;