]> gitweb.factorcode.org Git - factor.git/blob - core/io/files/files.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / core / io / files / files.factor
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 ;
5 IN: io.files
6
7 HOOK: (file-reader) io-backend ( path -- stream )
8
9 HOOK: (file-writer) io-backend ( path -- stream )
10
11 HOOK: (file-appender) io-backend ( path -- stream )
12
13 : <file-reader> ( path encoding -- stream )
14     swap normalize-path (file-reader) swap <decoder> ;
15
16 : <file-writer> ( path encoding -- stream )
17     swap normalize-path (file-writer) swap <encoder> ;
18
19 : <file-appender> ( path encoding -- stream )
20     swap normalize-path (file-appender) swap <encoder> ;
21
22 : file-lines ( path encoding -- seq )
23     <file-reader> stream-lines ;
24
25 : with-file-reader ( path encoding quot -- )
26     [ <file-reader> ] dip with-input-stream ; inline
27
28 : file-contents ( path encoding -- seq )
29     <file-reader> stream-contents ;
30
31 : with-file-writer ( path encoding quot -- )
32     [ <file-writer> ] dip with-output-stream ; inline
33
34 : set-file-lines ( seq path encoding -- )
35     [ [ print ] each ] with-file-writer ;
36
37 : set-file-contents ( seq path encoding -- )
38     [ write ] with-file-writer ;
39
40 : with-file-appender ( path encoding quot -- )
41     [ <file-appender> ] dip with-output-stream ; inline
42
43 : exists? ( path -- ? ) normalize-path (exists?) ;
44
45 ! Current directory
46 <PRIVATE
47
48 HOOK: cd io-backend ( path -- )
49
50 HOOK: cwd io-backend ( -- path )
51
52 M: object cwd ( -- path ) "." ;
53
54 PRIVATE>
55
56 [
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