1 ! Copyright (C) 2004, 2008 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel kernel.private sequences init namespaces system io
4 io.backend io.pathnames io.encodings io.files.private ;
7 HOOK: (file-reader) io-backend ( path -- stream )
9 HOOK: (file-writer) io-backend ( path -- stream )
11 HOOK: (file-appender) io-backend ( path -- stream )
13 : <file-reader> ( path encoding -- stream )
14 swap normalize-path (file-reader) swap <decoder> ;
16 : <file-writer> ( path encoding -- stream )
17 swap normalize-path (file-writer) swap <encoder> ;
19 : <file-appender> ( path encoding -- stream )
20 swap normalize-path (file-appender) swap <encoder> ;
22 : file-lines ( path encoding -- seq )
25 : with-file-reader ( path encoding quot -- )
26 [ <file-reader> ] dip with-input-stream ; inline
28 : file-contents ( path encoding -- str )
29 <file-reader> contents ;
31 : with-file-writer ( path encoding quot -- )
32 [ <file-writer> ] dip with-output-stream ; inline
34 : set-file-lines ( seq path encoding -- )
35 [ [ print ] each ] with-file-writer ;
37 : set-file-contents ( str path encoding -- )
38 [ write ] with-file-writer ;
40 : with-file-appender ( path encoding quot -- )
41 [ <file-appender> ] dip with-output-stream ; inline
43 : exists? ( path -- ? ) normalize-path (exists?) ;
48 HOOK: cd io-backend ( path -- )
50 HOOK: cwd io-backend ( -- path )
52 M: object cwd ( -- path ) "." ;
57 cwd current-directory set-global
58 13 getenv cwd prepend-path \ image set-global
59 14 getenv cwd prepend-path \ vm set-global
60 image parent-directory "resource-path" set-global
61 ] "io.files" add-init-hook