1 ! Copyright (C) 2004, 2008 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators destructors io io.backend
4 io.encodings.binary io.files io.files.types io.pathnames
5 kernel namespaces sequences system vocabs.loader fry
9 : set-current-directory ( path -- )
10 absolute-path current-directory set ;
12 : with-directory ( path quot -- )
13 [ absolute-path current-directory ] dip with-variable ; inline
15 ! Creating directories
16 HOOK: make-directory io-backend ( path -- )
18 : make-directories ( path -- )
19 normalize-path trim-tail-separators {
21 { [ dup root-directory? ] [ ] }
22 { [ dup empty? ] [ ] }
23 { [ dup exists? ] [ ] }
25 dup parent-directory make-directories
31 TUPLE: directory-entry name type ;
33 HOOK: >directory-entry os ( byte-array -- directory-entry )
35 HOOK: (directory-entries) os ( path -- seq )
37 : directory-entries ( path -- seq )
40 [ name>> { "." ".." } member? not ] filter ;
42 : directory-files ( path -- seq )
43 directory-entries [ name>> ] map ;
45 : with-directory-entries ( path quot -- )
46 '[ "" directory-entries @ ] with-directory ; inline
48 : with-directory-files ( path quot -- )
49 '[ "" directory-files @ ] with-directory ; inline
52 HOOK: touch-file io-backend ( path -- )
55 HOOK: delete-file io-backend ( path -- )
57 HOOK: delete-directory io-backend ( path -- )
59 : to-directory ( from to -- from to' )
60 over file-name append-path ;
62 ! Moving and renaming files
63 HOOK: move-file io-backend ( from to -- )
65 : move-file-into ( from to -- )
66 to-directory move-file ;
68 : move-files-into ( files to -- )
69 '[ _ move-file-into ] each ;
72 HOOK: copy-file io-backend ( from to -- )
75 dup parent-directory make-directories
76 binary <file-writer> [
77 swap binary <file-reader> [
82 : copy-file-into ( from to -- )
83 to-directory copy-file ;
85 : copy-files-into ( files to -- )
86 '[ _ copy-file-into ] each ;
89 { [ os unix? ] [ "io.directories.unix" require ] }
90 { [ os windows? ] [ "io.directories.windows" require ] }