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 ;
8 SYMBOL: +retry+ ! just try the operation again without blocking
12 ! Returns an event to wait for which will ensure completion of
14 GENERIC: drain ( port handle -- event/f )
15 GENERIC: refill ( port handle -- event/f )
17 HOOK: wait-for-fd io-backend ( handle event -- )
22 M: file-reader stream-element-type drop +byte+ ; inline
23 M: file-writer stream-element-type drop +byte+ ; inline
25 HOOK: (file-reader) io-backend ( path -- stream )
27 HOOK: (file-writer) io-backend ( path -- stream )
29 HOOK: (file-appender) io-backend ( path -- stream )
31 : <file-reader> ( path encoding -- stream )
32 [ normalize-path (file-reader) { file-reader } declare ] dip <decoder> ; inline
34 : <file-writer> ( path encoding -- stream )
35 [ normalize-path (file-writer) { file-writer } declare ] dip <encoder> ; inline
37 : <file-appender> ( path encoding -- stream )
38 [ normalize-path (file-appender) { file-writer } declare ] dip <encoder> ; inline
40 : file-lines ( path encoding -- seq )
41 <file-reader> stream-lines ;
43 : with-file-reader ( path encoding quot -- )
44 [ <file-reader> ] dip with-input-stream ; inline
46 : file-contents ( path encoding -- seq )
47 <file-reader> stream-contents ;
49 : with-file-writer ( path encoding quot -- )
50 [ <file-writer> ] dip with-output-stream ; inline
52 : set-file-lines ( seq path encoding -- )
53 [ [ print ] each ] with-file-writer ;
55 : set-file-contents ( seq path encoding -- )
56 [ write ] with-file-writer ;
58 : with-file-appender ( path encoding quot -- )
59 [ <file-appender> ] dip with-output-stream ; inline
61 : exists? ( path -- ? )
62 normalize-path native-string>alien (exists?) ;
67 HOOK: cd io-backend ( path -- )
69 HOOK: cwd io-backend ( -- path )
71 M: object cwd ( -- path ) "." ;
75 : init-resource-path ( -- )
76 OBJ-ARGS special-object
77 [ utf8 alien>string "-resource-path=" ?head [ drop f ] unless ] map-find drop
78 [ image parent-directory ] unless* "resource-path" set-global ;
81 cwd current-directory set-global
82 OBJ-IMAGE special-object alien>native-string cwd prepend-path \ image set-global
83 OBJ-EXECUTABLE special-object alien>native-string cwd prepend-path \ vm set-global
85 ] "io.files" add-startup-hook