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.
|
2008-12-14 21:03:00 -05:00
|
|
|
USING: kernel kernel.private sequences init namespaces system io
|
|
|
|
io.backend io.pathnames io.encodings io.files.private ;
|
2008-02-29 00:46:27 -05:00
|
|
|
IN: io.files
|
|
|
|
|
2008-02-24 02:37:05 -05:00
|
|
|
HOOK: (file-reader) io-backend ( path -- stream )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-02-24 02:37:05 -05:00
|
|
|
HOOK: (file-writer) io-backend ( path -- stream )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-02-24 02:37:05 -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-04-01 20:51:49 -04:00
|
|
|
swap normalize-path (file-reader) swap <decoder> ;
|
2008-02-16 17:25:45 -05:00
|
|
|
|
|
|
|
: <file-writer> ( path encoding -- stream )
|
2008-04-01 20:51:49 -04:00
|
|
|
swap normalize-path (file-writer) swap <encoder> ;
|
2008-02-16 17:25:45 -05:00
|
|
|
|
|
|
|
: <file-appender> ( path encoding -- stream )
|
2008-04-01 20:51:49 -04:00
|
|
|
swap normalize-path (file-appender) swap <encoder> ;
|
2008-02-16 17:25:45 -05:00
|
|
|
|
2008-03-25 20:50:39 -04:00
|
|
|
: file-lines ( path encoding -- seq )
|
|
|
|
<file-reader> lines ;
|
|
|
|
|
|
|
|
: with-file-reader ( path encoding quot -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ <file-reader> ] dip with-input-stream ; inline
|
2008-03-25 20:50:39 -04:00
|
|
|
|
2009-01-29 01:08:40 -05:00
|
|
|
: file-contents ( path encoding -- seq )
|
2008-03-25 20:50:39 -04:00
|
|
|
<file-reader> contents ;
|
|
|
|
|
|
|
|
: with-file-writer ( path encoding quot -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ <file-writer> ] dip with-output-stream ; inline
|
2008-03-25 20:50:39 -04:00
|
|
|
|
|
|
|
: set-file-lines ( seq path encoding -- )
|
|
|
|
[ [ print ] each ] with-file-writer ;
|
|
|
|
|
2009-01-29 01:08:40 -05:00
|
|
|
: set-file-contents ( seq path encoding -- )
|
2008-03-25 20:50:39 -04:00
|
|
|
[ write ] with-file-writer ;
|
|
|
|
|
|
|
|
: with-file-appender ( path encoding quot -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ <file-appender> ] dip with-output-stream ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-15 00:23:12 -04:00
|
|
|
: exists? ( path -- ? ) normalize-path (exists?) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-14 21:03:00 -05:00
|
|
|
! Current directory
|
2008-04-03 19:34:47 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2008-02-27 15:59:15 -05:00
|
|
|
HOOK: cd io-backend ( path -- )
|
|
|
|
|
|
|
|
HOOK: cwd io-backend ( -- path )
|
|
|
|
|
2008-03-26 16:24:54 -04:00
|
|
|
M: object cwd ( -- path ) "." ;
|
|
|
|
|
2008-04-03 19:34:47 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2008-09-17 23:40:51 -04:00
|
|
|
[
|
|
|
|
cwd current-directory set-global
|
2008-09-18 23:08:12 -04:00
|
|
|
13 getenv cwd prepend-path \ image set-global
|
|
|
|
14 getenv cwd prepend-path \ vm set-global
|
|
|
|
image parent-directory "resource-path" set-global
|
2009-01-29 01:08:40 -05:00
|
|
|
] "io.files" add-init-hook
|