io.files: Add change-file-lines and change-file-contents words.

db4
Doug Coleman 2015-08-04 15:03:37 -07:00
parent 18699e83c1
commit 73d45cf6db
2 changed files with 21 additions and 1 deletions

View 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 } }

View 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