]> gitweb.factorcode.org Git - factor.git/blob - basis/io/directories/windows/windows.factor
3a69dbfedbddcd32fa903ddba1cc67ad01a0672c
[factor.git] / basis / io / directories / windows / windows.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: system io.directories io.encodings.utf16n alien.strings
4 io.pathnames io.backend io.files.windows destructors
5 kernel accessors calendar windows windows.errors
6 windows.kernel32 alien.c-types sequences splitting
7 fry continuations classes.struct ;
8 IN: io.directories.windows
9
10 M: windows touch-file ( path -- )
11     [
12         normalize-path
13         maybe-create-file [ &dispose ] dip
14         [ drop ] [ handle>> f now dup (set-file-times) ] if
15     ] with-destructors ;
16
17 M: windows move-file ( from to -- )
18     [ normalize-path ] bi@ MoveFile win32-error=0/f ;
19
20 M: windows delete-file ( path -- )
21     normalize-path DeleteFile win32-error=0/f ;
22
23 M: windows copy-file ( from to -- )
24     dup parent-directory make-directories
25     [ normalize-path ] bi@ 0 CopyFile win32-error=0/f ;
26
27 M: windows make-directory ( path -- )
28     normalize-path
29     f CreateDirectory win32-error=0/f ;
30
31 M: windows delete-directory ( path -- )
32     normalize-path
33     RemoveDirectory win32-error=0/f ;
34
35 : find-first-file ( path -- WIN32_FIND_DATA handle )
36     WIN32_FIND_DATA <struct>
37     [ nip ] [ FindFirstFile ] 2bi
38     [ INVALID_HANDLE_VALUE = [ win32-error-string throw ] when ] keep ;
39
40 : find-next-file ( path -- WIN32_FIND_DATA/f )
41     WIN32_FIND_DATA <struct>
42     [ nip ] [ FindNextFile ] 2bi 0 = [
43         GetLastError ERROR_NO_MORE_FILES = [
44             win32-error
45         ] unless drop f
46     ] when ;
47
48 TUPLE: windows-directory-entry < directory-entry attributes ;
49
50 M: windows >directory-entry ( byte-array -- directory-entry )
51     [ cFileName>> utf16n alien>string ]
52     [
53         dwFileAttributes>>
54         [ win32-file-type ] [ win32-file-attributes ] bi
55     ] bi
56     dupd remove windows-directory-entry boa ;
57
58 M: windows (directory-entries) ( path -- seq )
59     "\\" ?tail drop "\\*" append
60     find-first-file [ >directory-entry ] dip
61     [
62         '[
63             [ _ find-next-file dup ]
64             [ >directory-entry ]
65             produce nip
66             over name>> "." = [ nip ] [ swap prefix ] if
67         ]
68     ] [ '[ _ FindClose win32-error=0/f ] ] bi [ ] cleanup ;
69