factor/core/io/files/files.factor

62 lines
1.7 KiB
Factor
Raw Normal View History

2008-02-21 16:22:49 -05:00
! Copyright (C) 2004, 2008 Slava Pestov, Daniel Ehrenberg.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: kernel kernel.private sequences init namespaces system io
io.backend io.pathnames io.encodings io.files.private ;
2008-02-29 00:46:27 -05:00
IN: io.files
HOOK: (file-reader) io-backend ( path -- stream )
2007-09-20 18:09:08 -04:00
HOOK: (file-writer) io-backend ( path -- stream )
2007-09-20 18:09:08 -04:00
HOOK: (file-appender) io-backend ( path -- stream )
2007-09-20 18:09:08 -04:00
2008-02-16 17:25:45 -05:00
: <file-reader> ( path encoding -- stream )
swap normalize-path (file-reader) swap <decoder> ;
2008-02-16 17:25:45 -05:00
: <file-writer> ( path encoding -- stream )
swap normalize-path (file-writer) swap <encoder> ;
2008-02-16 17:25:45 -05:00
: <file-appender> ( path encoding -- stream )
swap normalize-path (file-appender) swap <encoder> ;
2008-02-16 17:25:45 -05:00
2008-03-25 20:50:39 -04:00
: file-lines ( path encoding -- seq )
<file-reader> lines ;
: with-file-reader ( path encoding quot -- )
[ <file-reader> ] dip with-input-stream ; inline
2008-03-25 20:50:39 -04:00
: file-contents ( path encoding -- seq )
2008-03-25 20:50:39 -04:00
<file-reader> contents ;
: with-file-writer ( path encoding quot -- )
[ <file-writer> ] dip with-output-stream ; inline
2008-03-25 20:50:39 -04:00
: set-file-lines ( seq path encoding -- )
[ [ print ] each ] with-file-writer ;
: set-file-contents ( seq path encoding -- )
2008-03-25 20:50:39 -04:00
[ write ] with-file-writer ;
: with-file-appender ( path encoding quot -- )
[ <file-appender> ] dip with-output-stream ; inline
2007-09-20 18:09:08 -04:00
2008-05-15 00:23:12 -04:00
: exists? ( path -- ? ) normalize-path (exists?) ;
2007-09-20 18:09:08 -04:00
! Current directory
2008-04-03 19:34:47 -04:00
<PRIVATE
2008-02-27 15:59:15 -05:00
HOOK: cd io-backend ( path -- )
HOOK: cwd io-backend ( -- path )
2008-03-26 16:24:54 -04:00
M: object cwd ( -- path ) "." ;
2008-04-03 19:34:47 -04:00
PRIVATE>
[
cwd current-directory set-global
2008-09-18 23:08:12 -04:00
13 getenv cwd prepend-path \ image set-global
14 getenv cwd prepend-path \ vm set-global
image parent-directory "resource-path" set-global
] "io.files" add-init-hook