1 ! Copyright (C) 2004, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.strings continuations init io io.backend
4 io.encodings io.encodings.utf8 io.files.private io.pathnames
5 kernel kernel.private namespaces sequences splitting system ;
9 PRIMITIVE: (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 : set-file-contents ( seq path encoding -- )
60 [ write ] with-file-writer ;
62 : with-file-appender ( path encoding quot -- )
63 [ <file-appender> ] dip with-output-stream ; inline
65 : exists? ( path -- ? )
66 normalize-path native-string>alien (exists?) ;
71 HOOK: cd io-backend ( path -- )
73 HOOK: cwd io-backend ( -- path )
75 M: object cwd ( -- path ) "." ;
79 : init-resource-path ( -- )
80 OBJ-ARGS special-object
81 [ utf8 alien>string "-resource-path=" ?head [ drop f ] unless ] map-find drop
82 [ image parent-directory ] unless* "resource-path" set-global ;
85 cwd current-directory set-global
86 OBJ-IMAGE special-object alien>native-string cwd prepend-path \ image set-global
87 OBJ-EXECUTABLE special-object alien>native-string cwd prepend-path \ vm set-global
89 ] "io.files" add-startup-hook