]> gitweb.factorcode.org Git - factor.git/blob - basis/io/directories/windows/windows.factor
core, basis, extra: Remove DOS line endings from files.
[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 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 ERROR: file-delete-failed path error ;
21
22 : delete-file-throws ( path -- )
23     DeleteFile win32-error=0/f ;
24
25 : delete-read-only-file ( path -- )
26     [ set-file-normal-attribute ] [ delete-file-throws ] bi ;
27
28 : (delete-file) ( path -- )
29     dup DeleteFile 0 = [
30         GetLastError ERROR_ACCESS_DENIED =
31         [ delete-read-only-file ] [ throw-win32-error ] if
32     ] [ drop ] if ;
33
34 M: windows delete-file ( path -- )
35     absolute-path
36     [ (delete-file) ]
37     [ \ file-delete-failed boa rethrow ] recover ;
38
39 M: windows copy-file ( from to -- )
40     dup parent-directory make-directories
41     [ normalize-path ] bi@ 0 CopyFile win32-error=0/f ;
42
43 M: windows make-directory ( path -- )
44     normalize-path
45     f CreateDirectory win32-error=0/f ;
46
47 M: windows delete-directory ( path -- )
48     normalize-path
49     RemoveDirectory win32-error=0/f ;
50
51 : find-first-file ( path WIN32_FIND_DATA -- WIN32_FIND_DATA HANDLE )
52     [ nip ] [ FindFirstFile ] 2bi
53     [ INVALID_HANDLE_VALUE = [ win32-error-string throw ] when ] keep ;
54
55 : find-next-file ( HANDLE WIN32_FIND_DATA -- WIN32_FIND_DATA/f )
56     [ nip ] [ FindNextFile ] 2bi 0 = [
57         GetLastError ERROR_NO_MORE_FILES = [
58             win32-error
59         ] unless drop f
60     ] when ;
61
62 TUPLE: windows-directory-entry < directory-entry attributes ;
63
64 C: <windows-directory-entry> windows-directory-entry
65
66 : >windows-directory-entry ( WIN32_FIND_DATA -- directory-entry )
67     [ cFileName>> alien>native-string ]
68     [
69         dwFileAttributes>>
70         [ win32-file-type ] [ win32-file-attributes ] bi
71     ] bi
72     dupd remove <windows-directory-entry> ; inline
73
74 M: windows (directory-entries) ( path -- seq )
75     "\\" ?tail drop "\\*" append
76     WIN32_FIND_DATA <struct>
77     find-first-file over
78     [ >windows-directory-entry ] 2dip
79     [
80         '[
81             [ _ _ find-next-file dup ]
82             [ >windows-directory-entry ]
83             produce nip
84             over name>> "." = [ nip ] [ swap prefix ] if
85         ]
86     ] [ drop '[ _ FindClose win32-error=0/f ] ] 2bi [ ] cleanup ;