]> gitweb.factorcode.org Git - factor.git/blob - core/io/files/files.factor
Update actions, because Node.js 16 actions are deprecated, to Node.js 20
[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-writer-secure) io-backend ( path -- stream )
34
35 HOOK: (file-appender) io-backend ( path -- stream )
36
37 : <file-reader> ( path encoding -- stream )
38     [ normalize-path (file-reader) { file-reader } declare ] dip <decoder> ; inline
39
40 : <file-writer> ( path encoding -- stream )
41     [ normalize-path (file-writer) { file-writer } declare ] dip <encoder> ; inline
42
43 : <file-writer-secure> ( path encoding -- stream )
44     [ normalize-path (file-writer-secure) { file-writer } declare ] dip <encoder> ; inline
45
46 : <file-appender> ( path encoding -- stream )
47     [ normalize-path (file-appender) { file-writer } declare ] dip <encoder> ; inline
48
49 : file-lines ( path encoding -- seq )
50     <file-reader> stream-lines ;
51
52 : with-file-reader ( path encoding quot -- )
53     [ <file-reader> ] dip with-input-stream ; inline
54
55 : file-contents ( path encoding -- seq )
56     <file-reader> stream-contents ;
57
58 : with-file-writer ( path encoding quot -- )
59     [ <file-writer> ] dip with-output-stream ; inline
60
61 : with-file-writer-secure ( path encoding quot -- )
62     [ <file-writer-secure> ] dip with-output-stream ; inline
63
64 : set-file-lines ( seq path encoding -- )
65     [ [ print ] each ] with-file-writer ;
66
67 : change-file-lines ( ..a path encoding quot: ( ..a seq -- ..b seq' ) -- ..b )
68     '[ file-lines @ ] [ set-file-lines ] 2bi ; inline
69
70 : set-file-contents ( seq path encoding -- )
71     [ write ] with-file-writer ;
72
73 : change-file-contents ( ..a path encoding quot: ( ..a seq -- ..b seq' ) -- ..b )
74     '[ file-contents @ ] [ set-file-contents ] 2bi ; inline
75
76 : with-file-appender ( path encoding quot -- )
77     [ <file-appender> ] dip with-output-stream ; inline
78
79 : file-exists? ( path -- ? )
80     normalize-path native-string>alien (file-exists?) ;
81
82 ERROR: no-such-file path ;
83
84 : check-file-exists ( path -- path )
85     dup file-exists? [ no-such-file ] unless ;
86
87 : if-file-exists ( ..a path true: ( ..a path -- ..b ) false: ( ..a path -- ..b ) -- ..b )
88     [ dup file-exists? ] 2dip if ; inline
89
90 : when-file-exists ( ... path quot: ( ... path -- ... ) -- ... )
91     [ drop ] if-file-exists ; inline
92
93 : unless-file-exists ( ... path quot: ( ... path -- ... ) -- ... )
94     [ drop ] swap if-file-exists ; inline
95
96 ! Current directory
97 <PRIVATE
98
99 HOOK: cd io-backend ( path -- )
100
101 HOOK: cwd io-backend ( -- path )
102
103 M: object cwd "." ;
104
105 PRIVATE>
106
107 : init-resource-path ( -- )
108     OBJ-ARGS special-object [
109         alien>native-string "-resource-path=" ?head [ drop f ] unless
110     ] map-find drop
111     [ image-path parent-directory ] unless* "resource-path" set-global ;
112
113 STARTUP-HOOK: [
114     cwd current-directory set-global
115     OBJ-IMAGE special-object alien>native-string \ image-path set-global
116     OBJ-EXECUTABLE special-object alien>native-string \ vm-path set-global
117     init-resource-path
118 ]