factor/core/io/files/files.factor

75 lines
2.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2009 Slava Pestov, Daniel Ehrenberg.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2013-03-05 13:34:47 -05:00
USING: alien.strings init io io.backend io.encodings
io.encodings.utf8 io.files.private io.pathnames kernel
kernel.private namespaces sequences splitting system ;
2008-02-29 00:46:27 -05:00
IN: io.files
MIXIN: file-reader
MIXIN: file-writer
M: file-reader stream-element-type drop +byte+ ; inline
M: file-writer stream-element-type drop +byte+ ; inline
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 )
[ normalize-path (file-reader) { file-reader } declare ] dip <decoder> ; inline
2008-02-16 17:25:45 -05:00
: <file-writer> ( path encoding -- stream )
[ normalize-path (file-writer) { file-writer } declare ] dip <encoder> ; inline
2008-02-16 17:25:45 -05:00
: <file-appender> ( path encoding -- stream )
[ normalize-path (file-appender) { file-writer } declare ] dip <encoder> ; inline
2008-02-16 17:25:45 -05:00
2008-03-25 20:50:39 -04:00
: file-lines ( path encoding -- seq )
<file-reader> stream-lines ;
2008-03-25 20:50:39 -04:00
: 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 )
<file-reader> stream-contents ;
2008-03-25 20:50:39 -04:00
: 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
: exists? ( path -- ? )
normalize-path native-string>alien (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>
: init-resource-path ( -- )
OBJ-ARGS special-object
[ utf8 alien>string "-resource-path=" ?head [ drop f ] unless ] map-find drop
[ image parent-directory ] unless* "resource-path" set-global ;
[
cwd current-directory set-global
OBJ-IMAGE special-object alien>native-string cwd prepend-path \ image set-global
OBJ-EXECUTABLE special-object alien>native-string cwd prepend-path \ vm set-global
init-resource-path
] "io.files" add-startup-hook