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-01-04 19:56:04 -05:00
|
|
|
system combinators splitting sbufs ;
|
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
|
|
|
|
2007-11-12 01:41:13 -05:00
|
|
|
: trim-path-separators ( str -- newstr )
|
|
|
|
[ path-separator? ] right-trim ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: path+ ( str1 str2 -- str )
|
2007-11-12 01:41:13 -05:00
|
|
|
>r trim-path-separators "/" r>
|
2007-10-21 17:37:50 -04:00
|
|
|
[ path-separator? ] left-trim 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 ? )
|
|
|
|
[ length 2 [-] ] keep [ path-separator? ] find-last* ;
|
|
|
|
|
|
|
|
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 )
|
2007-11-12 17:11:17 -05:00
|
|
|
trim-path-separators {
|
|
|
|
{ [ 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 )
|
2007-11-12 01:41:13 -05:00
|
|
|
dup last-path-separator [ 1+ tail ] [ drop ] if ;
|
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 ;
|
|
|
|
|
|
|
|
: make-directories ( path -- )
|
2007-11-12 01:41:13 -05:00
|
|
|
normalize-pathname trim-path-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> [
|
|
|
|
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 ;
|
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
|
|
|
|
|
|
|
HOOK: library-roots io-backend ( -- seq )
|
|
|
|
HOOK: binary-roots io-backend ( -- seq )
|
|
|
|
|
|
|
|
: find-file ( seq str -- path/f )
|
|
|
|
[
|
|
|
|
[ path+ exists? ] curry find nip
|
|
|
|
] keep over [ path+ ] [ drop ] if ;
|
|
|
|
|
|
|
|
: find-library ( str -- path/f )
|
|
|
|
library-roots swap find-file ;
|
|
|
|
|
|
|
|
: find-binary ( str -- path/f )
|
|
|
|
binary-roots swap find-file ;
|
2007-12-09 12:43:00 -05:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
: append-path ( path files -- paths )
|
2008-01-09 17:36:30 -05:00
|
|
|
[ path+ ] with map ;
|
2007-12-09 12:43:00 -05:00
|
|
|
|
|
|
|
: get-paths ( dir -- paths )
|
|
|
|
dup directory keys append-path ;
|
|
|
|
|
|
|
|
: (walk-dir) ( path -- )
|
|
|
|
dup directory? [
|
|
|
|
get-paths dup % [ (walk-dir) ] each
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] if ;
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: walk-dir ( path -- seq ) [ (walk-dir) ] { } make ;
|
2008-01-04 19:56:04 -05:00
|
|
|
|
|
|
|
: file-lines ( path -- seq ) <file-reader> lines ;
|
|
|
|
|
|
|
|
: file-contents ( path -- str )
|
|
|
|
dup <file-reader> swap file-length <sbuf> [ stream-copy ] keep >string ;
|