diff --git a/core/io/files/files-docs.factor b/core/io/files/files-docs.factor index 3731b93f60..ae08d54ff0 100644 --- a/core/io/files/files-docs.factor +++ b/core/io/files/files-docs.factor @@ -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 } } diff --git a/core/io/files/files.factor b/core/io/files/files.factor index 4e551f33b3..da758adeef 100644 --- a/core/io/files/files.factor +++ b/core/io/files/files.factor @@ -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 -- ) [ ] dip with-output-stream ; inline