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
9 PRIMITIVE: (file-exists?) ( path -- ? )
12 SYMBOL: +retry+ ! just try the operation again without blocking
16 ! Returns an event to wait for which will ensure completion of
18 GENERIC: drain ( port handle -- event/f )
19 GENERIC: refill ( port handle -- event/f )
21 HOOK: wait-for-fd io-backend ( handle event -- )
26 M: file-reader stream-element-type drop +byte+ ; inline
27 M: file-writer stream-element-type drop +byte+ ; inline
29 HOOK: (file-reader) io-backend ( path -- stream )
31 HOOK: (file-writer) io-backend ( path -- stream )
33 HOOK: (file-appender) io-backend ( path -- stream )
35 : <file-reader> ( path encoding -- stream )
36 [ normalize-path (file-reader) { file-reader } declare ] dip <decoder> ; inline
38 : <file-writer> ( path encoding -- stream )
39 [ normalize-path (file-writer) { file-writer } declare ] dip <encoder> ; inline
41 : <file-appender> ( path encoding -- stream )
42 [ normalize-path (file-appender) { file-writer } declare ] dip <encoder> ; inline
44 : file-lines ( path encoding -- seq )
45 <file-reader> stream-lines ;
47 : with-file-reader ( path encoding quot -- )
48 [ <file-reader> ] dip with-input-stream ; inline
50 : file-contents ( path encoding -- seq )
51 <file-reader> stream-contents ;
53 : with-file-writer ( path encoding quot -- )
54 [ <file-writer> ] dip with-output-stream ; inline
56 : set-file-lines ( seq path encoding -- )
57 [ [ print ] each ] with-file-writer ;
59 : change-file-lines ( ..a path encoding quot: ( ..a seq -- ..b seq' ) -- ..b )
60 '[ file-lines @ ] [ set-file-lines ] 2bi ; inline
62 : set-file-contents ( seq path encoding -- )
63 [ write ] with-file-writer ;
65 : change-file-contents ( ..a path encoding quot: ( ..a seq -- ..b seq' ) -- ..b )
66 '[ file-contents @ ] [ set-file-contents ] 2bi ; inline
68 : with-file-appender ( path encoding quot -- )
69 [ <file-appender> ] dip with-output-stream ; inline
71 : file-exists? ( path -- ? )
72 normalize-path native-string>alien (file-exists?) ;
74 ERROR: no-such-file path ;
76 : check-file-exists ( path -- path )
77 dup file-exists? [ no-such-file ] unless ;
79 : if-file-exists ( ..a path true: ( ..a path -- ..b ) false: ( ..a path -- ..b ) -- ..b )
80 [ dup file-exists? ] 2dip if ; inline
82 : when-file-exists ( ... path quot: ( ... path -- ... ) -- ... )
83 [ drop ] if-file-exists ; inline
85 : unless-file-exists ( ... path quot: ( ... path -- ... ) -- ... )
86 [ drop ] swap if-file-exists ; inline
91 HOOK: cd io-backend ( path -- )
93 HOOK: cwd io-backend ( -- path )
99 : init-resource-path ( -- )
100 OBJ-ARGS special-object [
101 alien>native-string "-resource-path=" ?head [ drop f ] unless
103 [ image-path parent-directory ] unless* "resource-path" set-global ;
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