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
13 : open-file ( path access-mode create-mode flags -- handle )
15 [ share-mode default-security-attributes ] 2dip
16 CreateFile-flags f CreateFile opened-file
19 : open-r/w ( path -- win32-file )
20 flags{ GENERIC_READ GENERIC_WRITE }
21 OPEN_EXISTING 0 open-file ;
23 : open-read ( path -- win32-file )
24 GENERIC_READ OPEN_EXISTING 0 open-file 0 >>ptr ;
26 : open-write ( path -- win32-file )
27 GENERIC_WRITE CREATE_ALWAYS 0 open-file 0 >>ptr ;
29 : (open-append) ( path -- win32-file )
30 GENERIC_WRITE OPEN_ALWAYS 0 open-file ;
32 : open-existing ( path -- win32-file )
33 flags{ GENERIC_READ GENERIC_WRITE }
37 FILE_FLAG_BACKUP_SEMANTICS
38 f CreateFileW dup win32-error=0/f <win32-file> ;
40 : maybe-create-file ( path -- win32-file ? )
41 #! return true if file was just created
42 flags{ GENERIC_READ GENERIC_WRITE }
47 f CreateFileW dup win32-error=0/f <win32-file>
48 GetLastError ERROR_ALREADY_EXISTS = not ;
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 ;
54 HOOK: open-append os ( path -- win32-file )
57 hFile lpBuffer nNumberOfBytesToRead
58 lpNumberOfBytesRet lpOverlapped ;
60 C: <FileArgs> FileArgs
62 : make-FileArgs ( port -- <FileArgs> )
64 [ handle>> check-disposed ]
67 [ buffer>> buffer-length ]
68 [ drop DWORD <c-object> ]
69 [ FileArgs-overlapped ]
72 : setup-read ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToRead lpNumberOfBytesRead lpOverlapped )
75 [ lpBuffer>> buffer-end ]
76 [ lpBuffer>> buffer-capacity ]
77 [ lpNumberOfBytesRet>> ]
81 : setup-write ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToWrite lpNumberOfBytesWritten lpOverlapped )
84 [ lpBuffer>> buffer@ ]
85 [ lpBuffer>> buffer-length ]
86 [ lpNumberOfBytesRet>> ]
90 M: windows (file-reader) ( path -- stream )
91 open-read <input-port> ;
93 M: windows (file-writer) ( path -- stream )
94 open-write <output-port> ;
96 M: windows (file-appender) ( path -- stream )
97 open-append <output-port> ;
99 SYMBOLS: +read-only+ +hidden+ +system+
100 +archive+ +device+ +normal+ +temporary+
101 +sparse-file+ +reparse-point+ +compressed+ +offline+
102 +not-content-indexed+ +encrypted+ ;
104 : win32-file-attribute ( n attr symbol -- )
105 rot mask? [ , ] [ drop ] if ;
107 : win32-file-attributes ( n -- seq )
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 ]
127 : win32-file-type ( n -- symbol )
128 FILE_ATTRIBUTE_DIRECTORY mask? +directory+ +regular-file+ ? ;
130 : (set-file-times) ( handle timestamp/f timestamp/f timestamp/f -- )
131 [ timestamp>FILETIME ] tri@
132 SetFileTime win32-error=0/f ;