2008-02-05 14:11:36 -05:00
|
|
|
! 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
|
2007-11-05 00:45:02 -05:00
|
|
|
USING: io.backend io.files.private io hashtables kernel math
|
2007-12-09 12:43:00 -05:00
|
|
|
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
|
|
|
|
2008-02-05 14:11:36 -05: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 -- )
|
|
|
|
|
2007-11-12 01:41:13 -05:00
|
|
|
: path-separator? ( ch -- ? ) windows? "/\\" "/" ? member? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-11-12 01:41:13 -05:00
|
|
|
HOOK: root-directory? io-backend ( path -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-11-12 01:41:13 -05: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 )
|
2007-11-12 01:41:13 -05:00
|
|
|
[ 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 ;
|
|
|
|
|
2007-11-12 01:41:13 -05:00
|
|
|
: 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
|
2007-11-12 01:41:13 -05:00
|
|
|
[ 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 ;
|
|
|
|
|
2007-11-05 00:45:02 -05:00
|
|
|
: 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 )
|
2007-11-05 00:45:02 -05:00
|
|
|
\ 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 ;
|
|
|
|
|
2008-02-11 00:03:54 -05:00
|
|
|
: 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 ] [
|
2007-11-05 00:45:02 -05:00
|
|
|
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
|
2007-11-05 00:45:02 -05:00
|
|
|
dup parent-directory make-directories
|
|
|
|
<file-writer> [
|
2008-02-15 23:20:31 -05:00
|
|
|
swap <file-reader> [
|
|
|
|
swap stream-copy
|
|
|
|
] with-disposal
|
|
|
|
] with-disposal ;
|
2007-11-05 00:45:02 -05:00
|
|
|
|
|
|
|
: 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 ;
|
2007-11-12 01:41:13 -05:00
|
|
|
|
|
|
|
: 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 ;
|
2007-12-05 19:09:08 -05:00
|
|
|
|
2008-01-04 19:56:04 -05:00
|
|
|
: 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-06 20:36:53 -05:00
|
|
|
|
2008-02-16 16:24:41 -05:00
|
|
|
: with-file-reader ( path quot -- )
|
2008-02-06 20:36:53 -05:00
|
|
|
>r <file-reader> r> with-stream ; inline
|
|
|
|
|
2008-02-16 16:24:41 -05:00
|
|
|
: with-file-writer ( path quot -- )
|
2008-02-06 20:36:53 -05:00
|
|
|
>r <file-writer> r> with-stream ; inline
|
|
|
|
|
|
|
|
: with-file-appender ( path quot -- )
|
|
|
|
>r <file-appender> r> with-stream ; inline
|