1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data combinators
4 destructors io.backend.windows io.binary io.buffers io.files
5 io.files.types io.ports kernel literals make
6 math.bitwise system windows.errors windows.handles
7 windows.kernel32 windows.time windows.types vocabs.loader ;
10 : open-file ( path access-mode create-mode flags -- handle )
12 [ share-mode default-security-attributes ] 2dip
13 CreateFile-flags f CreateFile opened-file
16 : open-r/w ( path -- win32-file )
17 flags{ GENERIC_READ GENERIC_WRITE }
18 OPEN_EXISTING 0 open-file ;
20 : open-read ( path -- win32-file )
21 GENERIC_READ OPEN_EXISTING 0 open-file 0 >>ptr ;
23 : open-write ( path -- win32-file )
24 GENERIC_WRITE CREATE_ALWAYS 0 open-file 0 >>ptr ;
26 : (open-append) ( path -- win32-file )
27 GENERIC_WRITE OPEN_ALWAYS 0 open-file ;
29 : open-existing ( path -- win32-file )
30 flags{ GENERIC_READ GENERIC_WRITE }
34 FILE_FLAG_BACKUP_SEMANTICS
35 f CreateFileW dup win32-error=0/f <win32-file> ;
37 : maybe-create-file ( path -- win32-file ? )
38 #! return true if file was just created
39 flags{ GENERIC_READ GENERIC_WRITE }
44 f CreateFileW dup win32-error=0/f <win32-file>
45 GetLastError ERROR_ALREADY_EXISTS = not ;
47 : set-file-pointer ( handle length method -- )
48 [ [ handle>> ] dip d>w/w <uint> ] dip SetFilePointer
49 INVALID_SET_FILE_POINTER = [ "SetFilePointer failed" throw ] when ;
51 HOOK: open-append os ( path -- win32-file )
54 hFile lpBuffer nNumberOfBytesToRead
55 lpNumberOfBytesRet lpOverlapped ;
57 C: <FileArgs> FileArgs
59 : make-FileArgs ( port -- <FileArgs> )
61 [ handle>> check-disposed ]
64 [ buffer>> buffer-length ]
65 [ drop DWORD <c-object> ]
66 [ FileArgs-overlapped ]
69 : setup-read ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToRead lpNumberOfBytesRead lpOverlapped )
72 [ lpBuffer>> buffer-end ]
73 [ lpBuffer>> buffer-capacity ]
74 [ lpNumberOfBytesRet>> ]
78 : setup-write ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToWrite lpNumberOfBytesWritten lpOverlapped )
81 [ lpBuffer>> buffer@ ]
82 [ lpBuffer>> buffer-length ]
83 [ lpNumberOfBytesRet>> ]
87 M: windows (file-reader) ( path -- stream )
88 open-read <input-port> ;
90 M: windows (file-writer) ( path -- stream )
91 open-write <output-port> ;
93 M: windows (file-appender) ( path -- stream )
94 open-append <output-port> ;
96 SYMBOLS: +read-only+ +hidden+ +system+
97 +archive+ +device+ +normal+ +temporary+
98 +sparse-file+ +reparse-point+ +compressed+ +offline+
99 +not-content-indexed+ +encrypted+ ;
101 : win32-file-attribute ( n symbol attr -- )
102 rot mask? [ , ] [ drop ] if ;
104 : win32-file-attributes ( n -- seq )
107 [ +read-only+ FILE_ATTRIBUTE_READONLY win32-file-attribute ]
108 [ +hidden+ FILE_ATTRIBUTE_HIDDEN win32-file-attribute ]
109 [ +system+ FILE_ATTRIBUTE_SYSTEM win32-file-attribute ]
110 [ +directory+ FILE_ATTRIBUTE_DIRECTORY win32-file-attribute ]
111 [ +archive+ FILE_ATTRIBUTE_ARCHIVE win32-file-attribute ]
112 [ +device+ FILE_ATTRIBUTE_DEVICE win32-file-attribute ]
113 [ +normal+ FILE_ATTRIBUTE_NORMAL win32-file-attribute ]
114 [ +temporary+ FILE_ATTRIBUTE_TEMPORARY win32-file-attribute ]
115 [ +sparse-file+ FILE_ATTRIBUTE_SPARSE_FILE win32-file-attribute ]
116 [ +reparse-point+ FILE_ATTRIBUTE_REPARSE_POINT win32-file-attribute ]
117 [ +compressed+ FILE_ATTRIBUTE_COMPRESSED win32-file-attribute ]
118 [ +offline+ FILE_ATTRIBUTE_OFFLINE win32-file-attribute ]
119 [ +not-content-indexed+ FILE_ATTRIBUTE_NOT_CONTENT_INDEXED win32-file-attribute ]
120 [ +encrypted+ FILE_ATTRIBUTE_ENCRYPTED win32-file-attribute ]
124 : win32-file-type ( n -- symbol )
125 FILE_ATTRIBUTE_DIRECTORY mask? +directory+ +regular-file+ ? ;
127 : (set-file-times) ( handle timestamp/f timestamp/f timestamp/f -- )
128 [ timestamp>FILETIME ] tri@
129 SetFileTime win32-error=0/f ;
131 "io.files.windows.nt" require