]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/windows/windows.factor
Use flags{ instead of flags all over the place
[factor.git] / basis / io / files / windows / windows.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types io.binary io.backend io.files
4 io.files.types io.buffers io.encodings.utf16n io.ports
5 io.backend.windows kernel math splitting fry alien.strings
6 windows windows.kernel32 windows.time windows.types calendar
7 combinators math.functions sequences namespaces make words
8 system destructors accessors math.bitwise continuations
9 windows.errors arrays byte-arrays generalizations alien.data
10 literals ;
11 IN: io.files.windows
12
13 : open-file ( path access-mode create-mode flags -- handle )
14     [
15         [ share-mode default-security-attributes ] 2dip
16         CreateFile-flags f CreateFile opened-file
17     ] with-destructors ;
18
19 : open-r/w ( path -- win32-file )
20     flags{ GENERIC_READ GENERIC_WRITE }
21     OPEN_EXISTING 0 open-file ;
22
23 : open-read ( path -- win32-file )
24     GENERIC_READ OPEN_EXISTING 0 open-file 0 >>ptr ;
25
26 : open-write ( path -- win32-file )
27     GENERIC_WRITE CREATE_ALWAYS 0 open-file 0 >>ptr ;
28
29 : (open-append) ( path -- win32-file )
30     GENERIC_WRITE OPEN_ALWAYS 0 open-file ;
31
32 : open-existing ( path -- win32-file )
33     flags{ GENERIC_READ GENERIC_WRITE }
34     share-mode
35     f
36     OPEN_EXISTING
37     FILE_FLAG_BACKUP_SEMANTICS
38     f CreateFileW dup win32-error=0/f <win32-file> ;
39
40 : maybe-create-file ( path -- win32-file ? )
41     #! return true if file was just created
42     flags{ GENERIC_READ GENERIC_WRITE }
43     share-mode
44     f
45     OPEN_ALWAYS
46     0 CreateFile-flags
47     f CreateFileW dup win32-error=0/f <win32-file>
48     GetLastError ERROR_ALREADY_EXISTS = not ;
49
50 : set-file-pointer ( handle length method -- )
51     [ [ handle>> ] dip d>w/w <uint> ] dip SetFilePointer
52     INVALID_SET_FILE_POINTER = [ "SetFilePointer failed" throw ] when ;
53
54 HOOK: open-append os ( path -- win32-file )
55
56 TUPLE: FileArgs
57     hFile lpBuffer nNumberOfBytesToRead
58     lpNumberOfBytesRet lpOverlapped ;
59
60 C: <FileArgs> FileArgs
61
62 : make-FileArgs ( port -- <FileArgs> )
63     {
64         [ handle>> check-disposed ]
65         [ handle>> handle>> ]
66         [ buffer>> ]
67         [ buffer>> buffer-length ]
68         [ drop DWORD <c-object> ]
69         [ FileArgs-overlapped ]
70     } cleave <FileArgs> ;
71
72 : setup-read ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToRead lpNumberOfBytesRead lpOverlapped )
73     {
74         [ hFile>> ]
75         [ lpBuffer>> buffer-end ]
76         [ lpBuffer>> buffer-capacity ]
77         [ lpNumberOfBytesRet>> ]
78         [ lpOverlapped>> ]
79     } cleave ;
80
81 : setup-write ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToWrite lpNumberOfBytesWritten lpOverlapped )
82     {
83         [ hFile>> ]
84         [ lpBuffer>> buffer@ ]
85         [ lpBuffer>> buffer-length ]
86         [ lpNumberOfBytesRet>> ]
87         [ lpOverlapped>> ]
88     } cleave ;
89
90 M: windows (file-reader) ( path -- stream )
91     open-read <input-port> ;
92
93 M: windows (file-writer) ( path -- stream )
94     open-write <output-port> ;
95
96 M: windows (file-appender) ( path -- stream )
97     open-append <output-port> ;
98
99 SYMBOLS: +read-only+ +hidden+ +system+
100 +archive+ +device+ +normal+ +temporary+
101 +sparse-file+ +reparse-point+ +compressed+ +offline+
102 +not-content-indexed+ +encrypted+ ;
103
104 : win32-file-attribute ( n attr symbol -- )
105     rot mask? [ , ] [ drop ] if ;
106
107 : win32-file-attributes ( n -- seq )
108     [
109         {
110             [ +read-only+ FILE_ATTRIBUTE_READONLY win32-file-attribute ]
111             [ +hidden+ FILE_ATTRIBUTE_HIDDEN win32-file-attribute ]
112             [ +system+ FILE_ATTRIBUTE_SYSTEM win32-file-attribute ]
113             [ +directory+ FILE_ATTRIBUTE_DIRECTORY win32-file-attribute ]
114             [ +archive+ FILE_ATTRIBUTE_ARCHIVE win32-file-attribute ]
115             [ +device+ FILE_ATTRIBUTE_DEVICE win32-file-attribute ]
116             [ +normal+ FILE_ATTRIBUTE_NORMAL win32-file-attribute ]
117             [ +temporary+ FILE_ATTRIBUTE_TEMPORARY win32-file-attribute ]
118             [ +sparse-file+ FILE_ATTRIBUTE_SPARSE_FILE win32-file-attribute ]
119             [ +reparse-point+ FILE_ATTRIBUTE_REPARSE_POINT win32-file-attribute ]
120             [ +compressed+ FILE_ATTRIBUTE_COMPRESSED win32-file-attribute ]
121             [ +offline+ FILE_ATTRIBUTE_OFFLINE win32-file-attribute ]
122             [ +not-content-indexed+ FILE_ATTRIBUTE_NOT_CONTENT_INDEXED win32-file-attribute ]
123             [ +encrypted+ FILE_ATTRIBUTE_ENCRYPTED win32-file-attribute ]
124         } cleave
125     ] { } make ;
126
127 : win32-file-type ( n -- symbol )
128     FILE_ATTRIBUTE_DIRECTORY mask? +directory+ +regular-file+ ? ;
129
130 : (set-file-times) ( handle timestamp/f timestamp/f timestamp/f -- )
131     [ timestamp>FILETIME ] tri@
132     SetFileTime win32-error=0/f ;