]> gitweb.factorcode.org Git - factor.git/blobdiff - core/io/files/files.factor
core: trim using lists with lint.vocabs tool
[factor.git] / core / io / files / files.factor
index 0f3041e67025e6b34621c894bd0427959c2084f1..9189473f74c91f79927cffee8c17dccc5aa34134 100644 (file)
@@ -1,9 +1,31 @@
-! Copyright (C) 2004, 2008 Slava Pestov, Daniel Ehrenberg.
+! Copyright (C) 2004, 2009 Slava Pestov, Daniel Ehrenberg.
 ! 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 ;
+USING: alien.strings io io.backend io.encodings
+io.pathnames kernel kernel.private namespaces sequences
+splitting system ;
 IN: io.files
 
+<PRIVATE
+PRIMITIVE: (file-exists?) ( path -- ? )
+PRIVATE>
+
+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 )
@@ -11,13 +33,13 @@ HOOK: (file-writer) io-backend ( path -- stream )
 HOOK: (file-appender) io-backend ( path -- stream )
 
 : <file-reader> ( path encoding -- stream )
-    swap normalize-path (file-reader) swap <decoder> ;
+    [ normalize-path (file-reader) { file-reader } declare ] dip <decoder> ; inline
 
 : <file-writer> ( path encoding -- stream )
-    swap normalize-path (file-writer) swap <encoder> ;
+    [ normalize-path (file-writer) { file-writer } declare ] dip <encoder> ; inline
 
 : <file-appender> ( path encoding -- stream )
-    swap normalize-path (file-appender) swap <encoder> ;
+    [ normalize-path (file-appender) { file-writer } declare ] dip <encoder> ; inline
 
 : file-lines ( path encoding -- seq )
     <file-reader> stream-lines ;
@@ -34,13 +56,22 @@ HOOK: (file-appender) io-backend ( path -- stream )
 : 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 -- )
     [ <file-appender> ] dip with-output-stream ; inline
 
-: exists? ( path -- ? ) normalize-path (exists?) ;
+: file-exists? ( path -- ? )
+    normalize-path native-string>alien (file-exists?) ;
 
 ! Current directory
 <PRIVATE
@@ -49,13 +80,19 @@ HOOK: cd io-backend ( path -- )
 
 HOOK: cwd io-backend ( -- path )
 
-M: object cwd ( -- path ) "." ;
+M: object cwd "." ;
 
 PRIVATE>
 
-[
+: 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
-    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
+    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
+]