factor/core/io/files/files.factor

127 lines
3.3 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2004, 2007 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: io.files
USING: io.backend io.files.private io hashtables kernel math
memory namespaces sequences strings arrays definitions system
2007-09-20 18:09:08 -04:00
combinators splitting ;
HOOK: <file-reader> io-backend ( path -- stream )
HOOK: <file-writer> io-backend ( path -- stream )
HOOK: <file-appender> io-backend ( path -- stream )
HOOK: delete-file io-backend ( path -- )
HOOK: rename-file io-backend ( from to -- )
HOOK: make-directory io-backend ( path -- )
HOOK: delete-directory io-backend ( path -- )
: path-separator? ( ch -- ? ) windows? "/\\" "/" ? member? ;
2007-09-20 18:09:08 -04:00
HOOK: root-directory? io-backend ( path -- ? )
2007-09-20 18:09:08 -04:00
M: object root-directory? ( path -- ? ) path-separator? ;
2007-09-20 18:09:08 -04:00
: trim-path-separators ( str -- newstr )
[ path-separator? ] right-trim ;
2007-09-20 18:09:08 -04:00
: path+ ( str1 str2 -- str )
>r trim-path-separators "/" r>
[ path-separator? ] left-trim 3append ;
2007-09-20 18:09:08 -04:00
: stat ( path -- directory? permissions length modified )
normalize-pathname (stat) ;
: exists? ( path -- ? ) stat >r 3drop r> >boolean ;
: directory? ( path -- ? ) stat 3drop ;
: special-directory? ( name -- ? )
{ "." ".." } member? ;
2007-09-20 18:09:08 -04:00
: fixup-directory ( path seq -- newseq )
[
dup string?
[ tuck path+ directory? 2array ] [ nip ] if
] curry* map
[ first special-directory? not ] subset ;
2007-09-20 18:09:08 -04:00
: directory ( path -- seq )
normalize-directory dup (directory) fixup-directory ;
: file-length ( path -- n ) stat 4array third ;
: file-modified ( path -- n ) stat >r 3drop r> ;
: last-path-separator ( path -- n ? )
[ length 2 [-] ] keep [ path-separator? ] find-last* ;
TUPLE: no-parent-directory path ;
: no-parent-directory ( path -- * )
\ no-parent-directory construct-boa throw ;
: parent-directory ( path -- parent )
2007-11-12 17:11:17 -05:00
trim-path-separators {
{ [ dup empty? ] [ drop "/" ] }
{ [ dup root-directory? ] [ ] }
{ [ dup [ path-separator? ] contains? not ] [ drop "." ] }
{ [ t ] [
last-path-separator drop 1+ cut
special-directory? [ no-parent-directory ] when
] }
} cond ;
2007-09-20 18:09:08 -04:00
: file-name ( path -- string )
dup last-path-separator [ 1+ tail ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: resource-path ( path -- newpath )
\ resource-path get [ image parent-directory ] unless*
2007-09-20 18:09:08 -04:00
swap path+ ;
: ?resource-path ( path -- newpath )
"resource:" ?head [ resource-path ] when ;
: make-directories ( path -- )
normalize-pathname trim-path-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 ;
: copy-file ( from to -- )
dup parent-directory make-directories
<file-writer> [
stdio get swap
<file-reader> [
stdio get swap stream-copy
] with-stream
] with-stream ;
: copy-directory ( from to -- )
dup make-directories
>r dup directory swap r> [
>r >r first r> over path+ r> rot path+ copy-file
] 2curry each ;
: home ( -- dir )
{
{ [ winnt? ] [ "USERPROFILE" os-env ] }
{ [ wince? ] [ "" resource-path ] }
{ [ unix? ] [ "HOME" os-env ] }
} cond ;
TUPLE: pathname string ;
C: <pathname> pathname
M: pathname <=> [ pathname-string ] compare ;