factor/core/io/files/files.factor

166 lines
4.4 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.
IN: io.files
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 ;
2007-09-20 18:09:08 -04:00
HOOK: cd io-backend ( path -- )
HOOK: cwd io-backend ( -- path )
2008-02-16 16:35:44 -05:00
HOOK: file-reader* io-backend ( path -- stream )
2007-09-20 18:09:08 -04:00
2008-02-16 16:35:44 -05:00
HOOK: file-writer* io-backend ( path -- stream )
2007-09-20 18:09:08 -04:00
2008-02-16 16:35:44 -05: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-02-21 16:22:49 -05:00
swap file-reader* swap <decoded> ;
2008-02-16 17:25:45 -05:00
: <file-writer> ( path encoding -- stream )
2008-02-21 16:22:49 -05:00
swap file-writer* swap <encoded> ;
2008-02-16 17:25:45 -05:00
: <file-appender> ( path encoding -- stream )
2008-02-21 16:22:49 -05:00
swap file-appender* swap <encoded> ;
2008-02-16 17:25:45 -05:00
2007-09-20 18:09:08 -04:00
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
2008-02-16 17:25:45 -05:00
binary <file-writer> [
swap binary <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 ;
2008-02-16 16:35:44 -05:00
: file-lines ( path encoding -- seq ) <file-reader> lines ;
: file-contents ( path encoding -- str )
dupd <file-reader> swap file-length <sbuf> [ stream-copy ] keep >string ;
2008-02-16 16:35:44 -05:00
: with-file-writer ( path encoding quot -- )
>r <file-writer> r> with-stream ; inline
2008-02-16 16:35:44 -05:00
: with-file-reader ( path encoding quot -- )
>r <file-reader> r> with-stream ; inline
: with-file-appender ( path encoding quot -- )
>r <file-appender> r> with-stream ; inline