factor/core/io/files/files.factor

248 lines
6.2 KiB
Factor
Raw Normal View History

2008-02-21 16:22:49 -05:00
! Copyright (C) 2004, 2008 Slava Pestov, Daniel Ehrenberg.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: io.backend io.files.private io hashtables kernel math
memory namespaces sequences strings assocs arrays definitions
2008-02-21 16:22:49 -05:00
system combinators splitting sbufs continuations io.encodings
io.encodings.binary ;
2008-02-29 00:46:27 -05:00
IN: io.files
HOOK: (file-reader) io-backend ( path -- stream )
2007-09-20 18:09:08 -04:00
HOOK: (file-writer) io-backend ( path -- stream )
2007-09-20 18:09:08 -04:00
HOOK: (file-appender) io-backend ( path -- stream )
2007-09-20 18:09:08 -04:00
2008-02-16 17:25:45 -05:00
: <file-reader> ( path encoding -- stream )
2008-02-24 20:58:34 -05:00
swap (file-reader) swap <decoder> ;
2008-02-16 17:25:45 -05:00
: <file-writer> ( path encoding -- stream )
2008-02-24 20:58:34 -05:00
swap (file-writer) swap <encoder> ;
2008-02-16 17:25:45 -05:00
: <file-appender> ( path encoding -- stream )
2008-02-24 20:58:34 -05:00
swap (file-appender) swap <encoder> ;
2008-02-16 17:25:45 -05:00
2007-09-20 18:09:08 -04:00
HOOK: rename-file io-backend ( from to -- )
2008-02-27 15:59:15 -05:00
! Pathnames
: path-separator? ( ch -- ? ) windows? "/\\" "/" ? member? ;
2007-09-20 18:09:08 -04:00
2008-02-05 20:16:22 -05:00
: right-trim-separators ( str -- newstr )
[ path-separator? ] right-trim ;
2007-09-20 18:09:08 -04:00
2008-02-05 20:16:22 -05:00
: left-trim-separators ( str -- newstr )
[ path-separator? ] left-trim ;
2007-09-20 18:09:08 -04:00
: path+ ( str1 str2 -- str )
2008-02-05 20:16:22 -05:00
>r right-trim-separators "/" r>
left-trim-separators 3append ;
2007-09-20 18:09:08 -04:00
2008-02-27 15:59:15 -05:00
: last-path-separator ( path -- n ? )
[ length 1- ] keep [ path-separator? ] find-last* ;
2008-02-27 15:59:15 -05:00
HOOK: root-directory? io-backend ( path -- ? )
2007-09-20 18:09:08 -04:00
2008-02-27 15:59:15 -05:00
M: object root-directory? ( path -- ? ) path-separator? ;
2007-09-20 18:09:08 -04:00
2008-02-27 15:59:15 -05:00
: special-directory? ( name -- ? ) { "." ".." } member? ;
2007-09-20 18:09:08 -04:00
TUPLE: no-parent-directory path ;
: no-parent-directory ( path -- * )
\ no-parent-directory construct-boa throw ;
: parent-directory ( path -- parent )
2008-02-05 20:16:22 -05:00
right-trim-separators {
2007-11-12 17:11:17 -05:00
{ [ dup empty? ] [ drop "/" ] }
{ [ dup root-directory? ] [ ] }
{ [ dup [ path-separator? ] contains? not ] [ drop "." ] }
{ [ t ] [
2007-11-12 17:13:59 -05:00
dup last-path-separator drop 1+ cut
2007-11-12 17:11:17 -05:00
special-directory? [ no-parent-directory ] when
] }
} cond ;
2007-09-20 18:09:08 -04:00
: file-name ( path -- string )
2008-02-06 20:23:39 -05:00
right-trim-separators {
{ [ dup empty? ] [ drop "/" ] }
{ [ dup last-path-separator ] [ 1+ tail ] }
{ [ t ] [ drop ] }
} cond ;
2007-09-20 18:09:08 -04:00
2008-02-29 00:46:27 -05:00
TUPLE: file-info type size permissions modified ;
HOOK: file-info io-backend ( path -- info )
2008-03-06 13:04:54 -05:00
HOOK: link-info io-backend ( path -- info )
2008-02-29 00:46:27 -05:00
SYMBOL: +regular-file+
SYMBOL: +directory+
SYMBOL: +character-device+
SYMBOL: +block-device+
SYMBOL: +fifo+
SYMBOL: +symbolic-link+
SYMBOL: +socket+
SYMBOL: +unknown+
2008-02-27 15:59:15 -05:00
! File metadata
: stat ( path -- directory? permissions length modified )
normalize-pathname (stat) ;
2007-09-20 18:09:08 -04:00
2008-02-27 15:59:15 -05:00
: file-modified ( path -- n ) stat >r 3drop r> ;
: exists? ( path -- ? ) file-modified >boolean ;
2008-03-14 14:58:10 -04:00
: directory? ( path -- ? ) file-info file-info-type +directory+ = ;
2008-02-27 15:59:15 -05:00
! Current working directory
HOOK: cd io-backend ( path -- )
HOOK: cwd io-backend ( -- path )
: with-directory ( path quot -- )
2008-02-28 17:37:04 -05:00
cwd [ cd ] curry rot cd [ ] cleanup ; inline
2008-02-27 15:59:15 -05:00
! Creating directories
HOOK: make-directory io-backend ( path -- )
2007-09-20 18:09:08 -04:00
: make-directories ( path -- )
2008-02-05 20:16:22 -05:00
normalize-pathname right-trim-separators {
2007-09-20 18:09:08 -04:00
{ [ dup "." = ] [ ] }
{ [ dup root-directory? ] [ ] }
{ [ dup empty? ] [ ] }
{ [ dup exists? ] [ ] }
{ [ t ] [
dup parent-directory make-directories
2007-09-20 18:09:08 -04:00
dup make-directory
] }
} cond drop ;
2008-02-27 15:59:15 -05:00
! Directory listings
: fixup-directory ( path seq -- newseq )
[
dup string?
[ tuck path+ directory? 2array ] [ nip ] if
] with map
[ first special-directory? not ] subset ;
: directory ( path -- seq )
normalize-directory dup (directory) fixup-directory ;
: directory* ( path -- seq )
dup directory [ first2 >r path+ r> 2array ] with map ;
! Touching files
HOOK: touch-file io-backend ( path -- )
! Deleting files
HOOK: delete-file io-backend ( path -- )
HOOK: delete-directory io-backend ( path -- )
: (delete-tree) ( path dir? -- )
[
dup directory* [ (delete-tree) ] assoc-each
delete-directory
] [ delete-file ] if ;
: delete-tree ( path -- )
dup directory? (delete-tree) ;
: to-directory over file-name path+ ;
! Moving and renaming files
HOOK: move-file io-backend ( from to -- )
: move-file-into ( from to -- )
2008-02-27 15:59:15 -05:00
to-directory move-file ;
: move-files-into ( files to -- )
[ move-file-into ] curry each ;
2008-02-27 15:59:15 -05:00
! Copying files
2007-11-24 16:38:20 -05:00
HOOK: copy-file io-backend ( from to -- )
M: object copy-file
dup parent-directory make-directories
2008-02-16 17:25:45 -05:00
binary <file-writer> [
swap binary <file-reader> [
swap stream-copy
] with-disposal
] with-disposal ;
: copy-file-into ( from to -- )
2008-02-27 15:59:15 -05:00
to-directory copy-file ;
: copy-files-into ( files to -- )
[ copy-file-into ] curry each ;
2008-02-27 17:31:13 -05:00
DEFER: copy-tree-into
2008-02-27 15:59:15 -05:00
: copy-tree ( from to -- )
over directory? [
>r dup directory swap r> [
>r swap first path+ r> copy-tree-into
2008-02-27 15:59:15 -05:00
] 2curry each
] [
copy-file
] if ;
: copy-tree-into ( from to -- )
2008-02-27 15:59:15 -05:00
to-directory copy-tree ;
: copy-trees-into ( files to -- )
[ copy-tree-into ] curry each ;
2008-02-27 17:31:13 -05:00
2008-02-27 15:59:15 -05:00
! Special paths
: resource-path ( path -- newpath )
\ resource-path get [ image parent-directory ] unless*
swap path+ ;
: ?resource-path ( path -- newpath )
"resource:" ?head [ resource-path ] when ;
: resource-exists? ( path -- ? )
?resource-path exists? ;
2008-02-27 15:59:15 -05:00
! Pathname presentations
TUPLE: pathname string ;
C: <pathname> pathname
M: pathname <=> [ pathname-string ] compare ;
2008-03-07 22:26:35 -05:00
: file-lines ( path encoding -- seq )
<file-reader> lines ;
2008-03-05 20:14:58 -05:00
: with-file-reader ( path encoding quot -- )
>r <file-reader> r> with-stream ; inline
2008-03-07 22:26:35 -05:00
: file-contents ( path encoding -- str )
2008-03-16 03:21:51 -04:00
<file-reader> contents ;
2008-03-07 22:26:35 -05:00
2008-03-05 20:14:58 -05:00
: with-file-writer ( path encoding quot -- )
>r <file-writer> r> with-stream ; inline
2008-03-07 18:53:20 -05:00
: set-file-lines ( seq path encoding -- )
[ [ print ] each ] with-file-writer ;
: set-file-contents ( str path encoding -- )
[ write ] with-file-writer ;
2008-02-16 16:35:44 -05:00
: with-file-appender ( path encoding quot -- )
>r <file-appender> r> with-stream ; inline
2008-02-21 23:08:03 -05:00
2008-02-22 02:01:14 -05:00
: temp-directory ( -- path )
2008-02-21 23:08:03 -05:00
"temp" resource-path
dup exists? not
[ dup make-directory ]
when ;
2008-02-24 20:58:34 -05:00
: temp-file ( name -- path ) temp-directory swap path+ ;
2008-02-27 15:59:15 -05:00
! Home directory
: home ( -- dir )
{
{ [ winnt? ] [ "USERPROFILE" os-env ] }
{ [ wince? ] [ "" resource-path ] }
{ [ unix? ] [ "HOME" os-env ] }
2008-02-28 17:37:04 -05:00
} cond ;