]> gitweb.factorcode.org Git - factor.git/commitdiff
io.streams.zeros: adding zero-file.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 26 Oct 2012 04:46:11 +0000 (21:46 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 26 Oct 2012 04:46:11 +0000 (21:46 -0700)
extra/io/streams/zeros/zeros.factor

index d64c7e03692dbe39691b22aa359a7dee60732573..6fa5f4984be383f58d247a82b26895963746ad88 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2010 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: destructors io kernel sequences ;
+USING: combinators destructors io io.directories
+io.encodings.binary io.files kernel math sequences ;
 IN: io.streams.zeros
 
 TUPLE: zero-stream ;
@@ -18,3 +19,20 @@ M: zero-stream stream-read-partial-unsafe stream-read-unsafe ;
 M: zero-stream dispose drop ;
 
 INSTANCE: zero-stream input-stream
+
+<PRIVATE
+
+: (zero-file) ( n path -- )
+    binary
+    [ 1 - seek-absolute seek-output 0 write1 ] with-file-writer ;
+
+PRIVATE>
+
+ERROR: invalid-file-size n path ;
+
+: zero-file ( n path -- )
+    {
+        { [ over 0 < ] [ invalid-file-size ] }
+        { [ over 0 = ] [ nip touch-file ] }
+        [ (zero-file) ]
+    } cond ;