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
11 M: file-reader stream-element-type drop +byte+ ; inline
12 M: file-writer stream-element-type drop +byte+ ; inline
14 HOOK: (file-reader) io-backend ( path -- stream )
16 HOOK: (file-writer) io-backend ( path -- stream )
18 HOOK: (file-appender) io-backend ( path -- stream )
20 : <file-reader> ( path encoding -- stream )
21 [ normalize-path (file-reader) { file-reader } declare ] dip <decoder> ; inline
23 : <file-writer> ( path encoding -- stream )
24 [ normalize-path (file-writer) { file-writer } declare ] dip <encoder> ; inline
26 : <file-appender> ( path encoding -- stream )
27 [ normalize-path (file-appender) { file-writer } declare ] dip <encoder> ; inline
29 : file-lines ( path encoding -- seq )
30 <file-reader> stream-lines ;
32 : with-file-reader ( path encoding quot -- )
33 [ <file-reader> ] dip with-input-stream ; inline
35 : file-contents ( path encoding -- seq )
36 <file-reader> stream-contents ;
38 : with-file-writer ( path encoding quot -- )
39 [ <file-writer> ] dip with-output-stream ; inline
41 : set-file-lines ( seq path encoding -- )
42 [ [ print ] each ] with-file-writer ;
44 : set-file-contents ( seq path encoding -- )
45 [ write ] with-file-writer ;
47 : with-file-appender ( path encoding quot -- )
48 [ <file-appender> ] dip with-output-stream ; inline
50 : exists? ( path -- ? )
51 normalize-path native-string>alien (exists?) ;
56 HOOK: cd io-backend ( path -- )
58 HOOK: cwd io-backend ( -- path )
60 M: object cwd ( -- path ) "." ;
65 cwd current-directory set-global
66 OBJ-IMAGE special-object alien>native-string cwd prepend-path \ image set-global
67 OBJ-EXECUTABLE special-object alien>native-string cwd prepend-path \ vm set-global
68 image parent-directory "resource-path" set-global
69 ] "io.files" add-startup-hook