! Copyright (C) 2004, 2009 Slava Pestov, Daniel Ehrenberg. ! See http://factorcode.org/license.txt for BSD license. USING: alien.strings io io.backend io.encodings io.pathnames kernel kernel.private namespaces sequences splitting system ; IN: io.files SYMBOL: +retry+ ! just try the operation again without blocking SYMBOL: +input+ SYMBOL: +output+ ! Returns an event to wait for which will ensure completion of ! this request GENERIC: drain ( port handle -- event/f ) GENERIC: refill ( port handle -- event/f ) HOOK: wait-for-fd io-backend ( handle event -- ) 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 ) HOOK: (file-writer) io-backend ( path -- stream ) HOOK: (file-appender) io-backend ( path -- stream ) : ( path encoding -- stream ) [ normalize-path (file-reader) { file-reader } declare ] dip ; inline : ( path encoding -- stream ) [ normalize-path (file-writer) { file-writer } declare ] dip ; inline : ( path encoding -- stream ) [ normalize-path (file-appender) { file-writer } declare ] dip ; inline : file-lines ( path encoding -- seq ) stream-lines ; : with-file-reader ( path encoding quot -- ) [ ] dip with-input-stream ; inline : file-contents ( path encoding -- seq ) stream-contents ; : with-file-writer ( path encoding quot -- ) [ ] dip with-output-stream ; inline : set-file-lines ( seq path encoding -- ) [ [ print ] each ] with-file-writer ; : change-file-lines ( ..a path encoding quot: ( ..a seq -- ..b seq' ) -- ..b ) [ [ file-lines ] dip call ] [ drop set-file-lines ] 3bi ; inline : set-file-contents ( seq path encoding -- ) [ write ] with-file-writer ; : change-file-contents ( ..a path encoding quot: ( ..a seq -- ..b seq' ) -- ..b ) [ [ file-contents ] dip call ] [ drop set-file-contents ] 3bi ; inline : with-file-appender ( path encoding quot -- ) [ ] dip with-output-stream ; inline : file-exists? ( path -- ? ) normalize-path native-string>alien (file-exists?) ; ! Current directory : init-resource-path ( -- ) OBJ-ARGS special-object [ alien>native-string "-resource-path=" ?head [ drop f ] unless ] map-find drop [ image-path parent-directory ] unless* "resource-path" set-global ; STARTUP-HOOK: [ cwd current-directory set-global OBJ-IMAGE special-object alien>native-string \ image-path set-global OBJ-EXECUTABLE special-object alien>native-string \ vm-path set-global init-resource-path ]