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