factor/core/io/files/files.factor

164 lines
4.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! 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 assocs arrays definitions
2008-02-16 01:42:16 -05:00
system combinators splitting sbufs continuations ;
2007-09-20 18:09:08 -04:00
HOOK: cd io-backend ( path -- )
HOOK: cwd io-backend ( -- path )
2007-09-20 18:09:08 -04:00
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
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
: stat ( path -- directory? permissions length modified )
normalize-pathname (stat) ;
2008-01-12 12:42:47 -05:00
: file-length ( path -- n ) stat 4array third ;
: file-modified ( path -- n ) stat >r 3drop r> ; inline
: exists? ( path -- ? ) file-modified >boolean ;
2007-09-20 18:09:08 -04:00
: 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
2008-01-09 17:36:30 -05:00
] with map
[ first special-directory? not ] subset ;
2007-09-20 18:09:08 -04:00
: directory ( path -- seq )
normalize-directory dup (directory) fixup-directory ;
: last-path-separator ( path -- n ? )
2008-02-06 20:23:39 -05:00
[ length 1- ] keep [ path-separator? ] find-last* ;
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
: 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 ;
: resource-exists? ( path -- ? )
?resource-path exists? ;
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 ;
2007-11-24 16:38:20 -05:00
HOOK: copy-file io-backend ( from to -- )
M: object copy-file
dup parent-directory make-directories
<file-writer> [
swap <file-reader> [
swap stream-copy
] with-disposal
] with-disposal ;
: 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 ;
: file-lines ( path -- seq ) <file-reader> lines ;
: file-contents ( path -- str )
2008-02-16 16:24:41 -05:00
dup <file-reader> swap file-length <sbuf>
[ stream-copy ] keep >string ;
2008-02-16 16:24:41 -05:00
: with-file-reader ( path quot -- )
>r <file-reader> r> with-stream ; inline
2008-02-16 16:24:41 -05:00
: with-file-writer ( path quot -- )
>r <file-writer> r> with-stream ; inline
: with-file-appender ( path quot -- )
>r <file-appender> r> with-stream ; inline
2008-02-21 23:08:03 -05:00
: temp-dir ( -- path )
"temp" resource-path
dup exists? not
[ dup make-directory ]
when ;
: temp-file ( name -- path ) temp-dir swap path+ ;