]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/windows/nt/nt.factor
Specialized array overhaul
[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 windows.errors specialized-arrays classes.struct ;
9 SPECIALIZED-ARRAY: ushort
10 IN: io.files.windows.nt
11
12 M: winnt cwd
13     MAX_UNICODE_PATH dup <ushort-array>
14     [ GetCurrentDirectory win32-error=0/f ] keep
15     utf16n alien>string ;
16
17 M: winnt cd
18     SetCurrentDirectory win32-error=0/f ;
19
20 CONSTANT: unicode-prefix "\\\\?\\"
21
22 M: winnt root-directory? ( path -- ? )
23     {
24         { [ dup empty? ] [ drop f ] }
25         { [ dup [ path-separator? ] all? ] [ drop t ] }
26         { [ dup trim-tail-separators { [ length 2 = ]
27           [ second CHAR: : = ] } 1&& ] [ drop t ] }
28         { [ dup unicode-prefix head? ]
29           [ trim-tail-separators length unicode-prefix length 2 + = ] }
30         [ drop f ]
31     } cond ;
32
33 : prepend-prefix ( string -- string' )
34     dup unicode-prefix head? [
35         unicode-prefix prepend
36     ] unless ;
37
38 TR: normalize-separators "/" "\\" ;
39
40 M: winnt normalize-path ( string -- string' )
41     (normalize-path)
42     normalize-separators
43     prepend-prefix ;
44
45 M: winnt CreateFile-flags ( DWORD -- DWORD )
46     FILE_FLAG_OVERLAPPED bitor ;
47
48 <PRIVATE
49
50 : windows-file-size ( path -- size )
51     normalize-path 0 WIN32_FILE_ATTRIBUTE_DATA <struct>
52     [ GetFileAttributesEx win32-error=0/f ] keep
53     [ nFileSizeLow>> ] [ 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|| ;