]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/windows/nt/nt.factor
afc81c784c70944f6a2ac1da034604fab0a64197
[factor.git] / basis / io / files / windows / nt / nt.factor
1 USING: continuations destructors io.buffers io.files io.backend
2 io.timeouts io.ports io.pathnames io.files.private
3 io.backend.windows io.files.windows io.encodings.utf16n windows
4 windows.kernel32 kernel libc math threads system environment
5 alien.c-types alien.arrays alien.strings sequences combinators
6 combinators.short-circuit ascii splitting alien strings assocs
7 namespaces make accessors tr windows.time windows.shell32 ;
8 IN: io.files.windows.nt
9
10 M: winnt cwd
11     MAX_UNICODE_PATH dup "ushort" <c-array>
12     [ GetCurrentDirectory win32-error=0/f ] keep
13     utf16n alien>string ;
14
15 M: winnt cd
16     SetCurrentDirectory win32-error=0/f ;
17
18 : unicode-prefix ( -- seq )
19     "\\\\?\\" ; inline
20
21 M: winnt root-directory? ( path -- ? )
22     {
23         { [ dup empty? ] [ drop f ] }
24         { [ dup [ path-separator? ] all? ] [ drop t ] }
25         { [ dup trim-tail-separators { [ length 2 = ]
26           [ second CHAR: : = ] } 1&& ] [ drop t ] }
27         { [ dup unicode-prefix head? ]
28           [ trim-tail-separators length unicode-prefix length 2 + = ] }
29         [ drop f ]
30     } cond ;
31
32 : prepend-prefix ( string -- string' )
33     dup unicode-prefix head? [
34         unicode-prefix prepend
35     ] unless ;
36
37 TR: normalize-separators "/" "\\" ;
38
39 M: winnt normalize-path ( string -- string' )
40     (normalize-path)
41     normalize-separators
42     prepend-prefix ;
43
44 M: winnt CreateFile-flags ( DWORD -- DWORD )
45     FILE_FLAG_OVERLAPPED bitor ;
46
47 <PRIVATE
48
49 : windows-file-size ( path -- size )
50     normalize-path 0 "WIN32_FILE_ATTRIBUTE_DATA" <c-object>
51     [ GetFileAttributesEx win32-error=0/f ] keep
52     [ WIN32_FILE_ATTRIBUTE_DATA-nFileSizeLow ]
53     [ WIN32_FILE_ATTRIBUTE_DATA-nFileSizeHigh ] bi >64bit ;
54
55 PRIVATE>
56
57 M: winnt open-append
58     [ dup windows-file-size ] [ drop 0 ] recover
59     [ (open-append) ] dip >>ptr ;
60
61 M: winnt home
62     {
63         [ "HOMEDRIVE" os-env "HOMEPATH" os-env append-path ]
64         [ "USERPROFILE" os-env ]
65         [ my-documents ]
66     } 0|| ;