]> gitweb.factorcode.org Git - factor.git/blob - core/io/files/files.factor
factor: rename system:vm -> vm-path to differentiate it from vm:vm (which is a STRUCT:)
[factor.git] / core / io / files / files.factor
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 ;
6 IN: io.files
7
8 <PRIVATE
9 PRIMITIVE: (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 : set-file-contents ( seq path encoding -- )
60     [ write ] with-file-writer ;
61
62 : with-file-appender ( path encoding quot -- )
63     [ <file-appender> ] dip with-output-stream ; inline
64
65 : exists? ( path -- ? )
66     normalize-path native-string>alien (exists?) ;
67
68 ! Current directory
69 <PRIVATE
70
71 HOOK: cd io-backend ( path -- )
72
73 HOOK: cwd io-backend ( -- path )
74
75 M: object cwd ( -- path ) "." ;
76
77 PRIVATE>
78
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 ;
83
84 [
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-path set-global
88     init-resource-path
89 ] "io.files" add-startup-hook