factor/core/io/files/files.factor

308 lines
7.6 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 init ;
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-03-26 23:39:16 -04:00
swap normalize-pathname (file-reader) swap <decoder> ;
2008-02-16 17:25:45 -05:00
: <file-writer> ( path encoding -- stream )
2008-03-26 23:39:16 -04:00
swap normalize-pathname (file-writer) swap <encoder> ;
2008-02-16 17:25:45 -05:00
: <file-appender> ( path encoding -- stream )
2008-03-26 23:39:16 -04:00
swap normalize-pathname (file-appender) swap <encoder> ;
2008-02-16 17:25:45 -05:00
2008-03-25 20:50:39 -04:00
: file-lines ( path encoding -- seq )
<file-reader> lines ;
: with-file-reader ( path encoding quot -- )
>r <file-reader> r> with-stream ; inline
: file-contents ( path encoding -- str )
<file-reader> contents ;
: with-file-writer ( path encoding quot -- )
>r <file-writer> r> with-stream ; inline
: set-file-lines ( seq path encoding -- )
[ [ print ] each ] with-file-writer ;
: set-file-contents ( str path encoding -- )
[ write ] with-file-writer ;
: with-file-appender ( path encoding quot -- )
>r <file-appender> r> with-stream ; inline
2007-09-20 18:09:08 -04:00
2008-02-27 15:59:15 -05:00
! Pathnames
: path-separator? ( ch -- ? ) windows? "/\\" "/" ? member? ;
2007-09-20 18:09:08 -04:00
2008-03-26 15:57:35 -04:00
: path-separator ( -- string ) windows? "\\" "/" ? ;
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 ;
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-03-25 20:50:39 -04:00
M: object root-directory? ( path -- ? )
dup empty? [ drop f ] [ [ path-separator? ] all? ] if ;
2007-09-20 18:09:08 -04:00
2008-03-20 16:00:49 -04:00
ERROR: no-parent-directory path ;
: parent-directory ( path -- parent )
2008-03-25 20:50:39 -04:00
dup root-directory? [
right-trim-separators
dup last-path-separator [
1+ cut
2008-03-26 16:24:54 -04:00
] [
drop "." swap
] if
{ "" "." ".." } member? [
no-parent-directory
] when
2008-03-25 20:50:39 -04:00
] unless ;
<PRIVATE
: head-path-separator? ( path1 ? -- ?' )
[
dup empty? [ drop t ] [ first path-separator? ] if
] [
drop f
] if ;
: head.? ( path -- ? ) "." ?head head-path-separator? ;
: head..? ( path -- ? ) ".." ?head head-path-separator? ;
: append-path-empty ( path1 path2 -- path' )
{
{ [ dup head.? ] [
1 tail left-trim-separators append-path-empty
] }
{ [ dup head..? ] [ drop no-parent-directory ] }
{ [ t ] [ nip ] }
} cond ;
PRIVATE>
: windows-absolute-path? ( path -- path ? )
{
{ [ dup length 2 < ] [ f ] }
{ [ dup second CHAR: : = ] [ t ] }
{ [ t ] [ f ] }
} cond ;
2008-03-25 20:50:39 -04:00
: absolute-path? ( path -- ? )
{
{ [ dup empty? ] [ f ] }
{ [ dup "resource:" head? ] [ t ] }
{ [ dup first path-separator? ] [ t ] }
{ [ windows? ] [ windows-absolute-path? ] }
{ [ t ] [ f ] }
} cond nip ;
2008-03-25 20:50:39 -04:00
: append-path ( str1 str2 -- str )
{
{ [ over empty? ] [ append-path-empty ] }
{ [ dup empty? ] [ drop ] }
{ [ dup absolute-path? ] [ nip ] }
{ [ dup head.? ] [ 1 tail left-trim-separators append-path ] }
{ [ dup head..? ] [
2 tail left-trim-separators
>r parent-directory r> append-path
] }
2007-11-12 17:11:17 -05:00
{ [ t ] [
2008-03-25 20:50:39 -04:00
>r right-trim-separators "/" r>
left-trim-separators 3append
2007-11-12 17:11:17 -05:00
] }
} cond ;
2007-09-20 18:09:08 -04:00
2008-03-25 20:50:39 -04:00
: prepend-path ( str1 str2 -- str )
swap append-path ; inline
2007-09-20 18:09:08 -04:00
: file-name ( path -- string )
2008-03-25 20:50:39 -04:00
dup root-directory? [
right-trim-separators
dup last-path-separator [ 1+ tail ] [ drop ] if
] unless ;
2007-09-20 18:09:08 -04:00
2008-03-25 20:50:39 -04:00
! File info
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
2008-03-20 00:29:19 -04:00
: exists? ( path -- ? )
normalize-pathname (exists?) ;
2007-09-20 18:09:08 -04:00
2008-03-20 00:29:19 -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 )
2008-03-25 20:50:39 -04:00
SYMBOL: current-directory
2008-03-26 16:24:54 -04:00
M: object cwd ( -- path ) "." ;
2008-03-27 18:12:39 -04:00
[ cwd current-directory set-global ] "io.files" add-init-hook
2008-03-25 20:50:39 -04:00
2008-02-27 15:59:15 -05:00
: with-directory ( path quot -- )
2008-03-27 17:22:24 -04:00
>r normalize-pathname r>
2008-03-25 20:50:39 -04:00
current-directory swap with-variable ; inline
2008-02-27 15:59:15 -05:00
2008-03-27 17:22:24 -04:00
: set-current-directory ( path -- )
normalize-pathname current-directory set ;
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?
2008-03-19 20:15:43 -04:00
[ tuck append-path directory? 2array ] [ nip ] if
2008-02-27 15:59:15 -05:00
] with map
2008-03-25 20:50:39 -04:00
[ first { "." ".." } member? not ] subset ;
2008-02-27 15:59:15 -05:00
: directory ( path -- seq )
normalize-directory dup (directory) fixup-directory ;
: directory* ( path -- seq )
2008-03-19 20:15:43 -04:00
dup directory [ first2 >r append-path r> 2array ] with map ;
2008-02-27 15:59:15 -05:00
! 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) ;
2008-03-19 20:15:43 -04:00
: to-directory over file-name append-path ;
2008-02-27 15:59:15 -05:00
! 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> [
2008-03-19 20:15:43 -04:00
>r swap first append-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*
2008-03-19 20:15:43 -04:00
prepend-path ;
2008-02-27 15:59:15 -05:00
2008-02-22 02:01:14 -05:00
: temp-directory ( -- path )
2008-03-23 12:38:26 -04:00
"temp" resource-path dup make-directories ;
2008-02-21 23:08:03 -05:00
2008-03-27 03:12:15 -04:00
: temp-file ( name -- path )
temp-directory prepend-path ;
M: object normalize-pathname ( path -- path' )
"resource:" ?head [
left-trim-separators resource-path
normalize-pathname
] [
current-directory get prepend-path
] if ;
2008-03-25 20:50:39 -04:00
! Pathname presentations
TUPLE: pathname string ;
C: <pathname> pathname
M: pathname <=> [ pathname-string ] compare ;
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 ;