1 ! Copyright (C) 2004, 2009 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
8 HOOK: (file-reader) io-backend ( path -- stream )
10 HOOK: (file-writer) io-backend ( path -- stream )
12 HOOK: (file-appender) io-backend ( path -- stream )
14 : <file-reader> ( path encoding -- stream )
15 swap normalize-path (file-reader) swap <decoder> ;
17 : <file-writer> ( path encoding -- stream )
18 swap normalize-path (file-writer) swap <encoder> ;
20 : <file-appender> ( path encoding -- stream )
21 swap normalize-path (file-appender) swap <encoder> ;
23 : file-lines ( path encoding -- seq )
24 <file-reader> stream-lines ;
26 : with-file-reader ( path encoding quot -- )
27 [ <file-reader> ] dip with-input-stream ; inline
29 : file-contents ( path encoding -- seq )
30 <file-reader> stream-contents ;
32 : with-file-writer ( path encoding quot -- )
33 [ <file-writer> ] dip with-output-stream ; inline
35 : set-file-lines ( seq path encoding -- )
36 [ [ print ] each ] with-file-writer ;
38 : set-file-contents ( seq path encoding -- )
39 [ write ] with-file-writer ;
41 : with-file-appender ( path encoding quot -- )
42 [ <file-appender> ] dip with-output-stream ; inline
44 : exists? ( path -- ? )
45 normalize-path native-string>alien (exists?) ;
50 HOOK: cd io-backend ( path -- )
52 HOOK: cwd io-backend ( -- path )
54 M: object cwd ( -- path ) "." ;
59 cwd current-directory set-global
60 13 getenv alien>native-string cwd prepend-path \ image set-global
61 14 getenv alien>native-string cwd prepend-path \ vm set-global
62 image parent-directory "resource-path" set-global
63 ] "io.files" add-init-hook