]> gitweb.factorcode.org Git - factor.git/commitdiff
io.files: Add change-file-lines and change-file-contents words.
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 4 Aug 2015 22:03:37 +0000 (15:03 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 4 Aug 2015 22:03:37 +0000 (15:03 -0700)
core/io/files/files-docs.factor
core/io/files/files.factor

index 3731b93f6011c198e0b9c8ca1f4c9ac11d33712a..ae08d54ff0a5cb9bbcfa94254406b06f265be0ff 100644 (file)
@@ -25,8 +25,10 @@ ARTICLE: "io.files" "Reading and writing files"
 { $subsections
     file-contents
     set-file-contents
+    change-file-contents
     file-lines
     set-file-lines
+    change-file-lines
 }
 "Utility combinators:"
 { $subsections
@@ -87,17 +89,27 @@ HELP: file-lines
 }
 { $errors "Throws an error if the file cannot be opened for reading." } ;
 
+HELP: change-file-lines
+{ $values { "path" "a pathname string" } { "encoding" "an encoding descriptor" } { "quot" quotation }
+{ $description "Reads the file lines, transforms the file lines, and writes them back to the same file name." }
+{ $errors "Throws an error if the file cannot be opened for writing." } ;
+
 HELP: set-file-contents
 { $values { "seq" sequence } { "path" "a pathname string" } { "encoding" "an encoding descriptor" } }
 { $description "Sets the contents of a file to a sequence with the given encoding." }
 { $errors "Throws an error if the file cannot be opened for writing." } ;
 
+HELP: change-file-contents
+{ $values { "path" "a pathname string" } { "encoding" "an encoding descriptor" } { "quot" quotation }
+{ $description "Reads the file, transforms the file contents, and writes it back to the same file name." }
+{ $errors "Throws an error if the file cannot be opened for writing." } ;
+
 HELP: file-contents
 { $values { "path" "a pathname string" } { "encoding" "an encoding descriptor" } { "seq" sequence } }
 { $description "Opens the file at the given path using the given encoding, and the contents of that file as a sequence." }
 { $errors "Throws an error if the file cannot be opened for reading." } ;
 
-{ set-file-lines file-lines set-file-contents file-contents } related-words
+{ set-file-lines file-lines change-file-lines set-file-contents file-contents change-file-contents } related-words
 
 HELP: exists?
 { $values { "path" "a pathname string" } { "?" boolean } }
index 4e551f33b3a94b307f73f10a064617b6fdeb2268..da758adeef9739036a63680096e3b3890155ffdc 100644 (file)
@@ -56,9 +56,17 @@ HOOK: (file-appender) io-backend ( path -- stream )
 : set-file-lines ( seq path encoding -- )
     [ [ print ] each ] with-file-writer ;
 
+: change-file-lines ( path encoding quot -- )
+    [ [ file-lines ] dip call ]
+    [ drop set-file-lines ] 3bi ; inline
+
 : set-file-contents ( seq path encoding -- )
     [ write ] with-file-writer ;
 
+: change-file-contents ( path encoding quot -- )
+    [ [ file-contents ] dip call ]
+    [ drop set-file-contents ] 3bi ; inline
+
 : with-file-appender ( path encoding quot -- )
     [ <file-appender> ] dip with-output-stream ; inline