]> gitweb.factorcode.org Git - factor.git/blob - core/io/files/files.factor
c67c8e2e65c78d9c07562ad7d11fd47ad37e059c
[factor.git] / core / io / files / files.factor
1 ! Copyright (C) 2004, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: alien.strings io io.backend io.encodings
4 io.pathnames kernel kernel.private namespaces sequences
5 splitting system ;
6 IN: io.files
7
8 <PRIVATE
9 PRIMITIVE: (file-exists?) ( path -- ? )
10 PRIVATE>
11
12 SYMBOL: +retry+ ! just try the operation again without blocking
13 SYMBOL: +input+
14 SYMBOL: +output+
15
16 ! Returns an event to wait for which will ensure completion of
17 ! this request
18 GENERIC: drain ( port handle -- event/f )
19 GENERIC: refill ( port handle -- event/f )
20
21 HOOK: wait-for-fd io-backend ( handle event -- )
22
23 MIXIN: file-reader
24 MIXIN: file-writer
25
26 M: file-reader stream-element-type drop +byte+ ; inline
27 M: file-writer stream-element-type drop +byte+ ; inline
28
29 HOOK: (file-reader) io-backend ( path -- stream )
30
31 HOOK: (file-writer) io-backend ( path -- stream )
32
33 HOOK: (file-appender) io-backend ( path -- stream )
34
35 : <file-reader> ( path encoding -- stream )
36     [ normalize-path (file-reader) { file-reader } declare ] dip <decoder> ; inline
37
38 : <file-writer> ( path encoding -- stream )
39     [ normalize-path (file-writer) { file-writer } declare ] dip <encoder> ; inline
40
41 : <file-appender> ( path encoding -- stream )
42     [ normalize-path (file-appender) { file-writer } declare ] dip <encoder> ; inline
43
44 : file-lines ( path encoding -- seq )
45     <file-reader> stream-lines ;
46
47 : with-file-reader ( path encoding quot -- )
48     [ <file-reader> ] dip with-input-stream ; inline
49
50 : file-contents ( path encoding -- seq )
51     <file-reader> stream-contents ;
52
53 : with-file-writer ( path encoding quot -- )
54     [ <file-writer> ] dip with-output-stream ; inline
55
56 : set-file-lines ( seq path encoding -- )
57     [ [ print ] each ] with-file-writer ;
58
59 : change-file-lines ( ..a path encoding quot: ( ..a seq -- ..b seq' ) -- ..b )
60     '[ file-lines @ ] [ set-file-lines ] 2bi ; inline
61
62 : set-file-contents ( seq path encoding -- )
63     [ write ] with-file-writer ;
64
65 : change-file-contents ( ..a path encoding quot: ( ..a seq -- ..b seq' ) -- ..b )
66     '[ file-contents @ ] [ set-file-contents ] 2bi ; inline
67
68 : with-file-appender ( path encoding quot -- )
69     [ <file-appender> ] dip with-output-stream ; inline
70
71 : file-exists? ( path -- ? )
72     normalize-path native-string>alien (file-exists?) ;
73
74 ERROR: no-such-file path ;
75
76 : check-file-exists ( path -- path )
77     dup file-exists? [ no-such-file ] unless ;
78
79 : if-file-exists ( ..a path true: ( ..a path -- ..b ) false: ( ..a path -- ..b ) -- ..b )
80     [ dup file-exists? ] 2dip if ; inline
81
82 : when-file-exists ( ... path quot: ( ... path -- ... ) -- ... )
83     [ drop ] if-file-exists ; inline
84
85 : unless-file-exists ( ... path quot: ( ... path -- ... ) -- ... )
86     [ drop ] swap if-file-exists ; inline
87
88 ! Current directory
89 <PRIVATE
90
91 HOOK: cd io-backend ( path -- )
92
93 HOOK: cwd io-backend ( -- path )
94
95 M: object cwd "." ;
96
97 PRIVATE>
98
99 : init-resource-path ( -- )
100     OBJ-ARGS special-object [
101         alien>native-string "-resource-path=" ?head [ drop f ] unless
102     ] map-find drop
103     [ image-path parent-directory ] unless* "resource-path" set-global ;
104
105 STARTUP-HOOK: [
106     cwd current-directory set-global
107     OBJ-IMAGE special-object alien>native-string \ image-path set-global
108     OBJ-EXECUTABLE special-object alien>native-string \ vm-path set-global
109     init-resource-path
110 ]