Adding some file macros.

This commit is contained in:
Kyle Isom 2015-08-31 21:55:03 -07:00
parent 4cb1950c5f
commit 4143e90171
4 changed files with 91 additions and 16 deletions

View File

@ -220,6 +220,20 @@ some code should be executed based on the results of those bindings.
@end(def) @end(def)
@term(File macros)
@begin(def)
These macros abstract common operations on files.
@cl:with-package[name="kutils"](
@cl:doc(macro read-file-string)
@cl:doc(macro with-string-output-to-file)
@cl:doc(macro with-read-from-file)
@cl:doc(macro with-write-to-file)
)
@end(def)
@end(deflist) @end(deflist)
@end(section) @end(section)
@ -251,10 +265,10 @@ described in "Miscellaneous utilities" under "General".)
"On Lisp utilities".) "On Lisp utilities".)
@item(@c(condlet) is a macro defined in @c(macros.lisp), described in @item(@c(condlet) is a macro defined in @c(macros.lisp), described in
"Macros".) "Macros" under "Let macros".)
@item(@c(condlet*) is a macro defined in @c(macros.lisp), described in @item(@c(condlet*) is a macro defined in @c(macros.lisp), described in
"Macros".) "Macros" under "Let macros".)
@item(@c(copy-hash-table) is a function defined in @item(@c(copy-hash-table) is a function defined in
@c(kutils-hash-tables.lisp), described in "Hash-table related @c(kutils-hash-tables.lisp), described in "Hash-table related
@ -294,10 +308,10 @@ utilities".)
utilities".) utilities".)
@item(@c(iflet) is a macro defined in @c(macros.lisp), described in @item(@c(iflet) is a macro defined in @c(macros.lisp), described in
"Macros".) "Macros" under "Let macros".)
@item(@c(iflet*) is a macro defined in @c(macros.lisp), described in @item(@c(iflet*) is a macro defined in @c(macros.lisp), described in
"Macros".) "Macros" under "Let macros".)
@item(@c(interpose) is a function defined in @c(kutils.lisp), @item(@c(interpose) is a function defined in @c(kutils.lisp),
described in "Miscellaneous utilities" under "Clojure-inspired described in "Miscellaneous utilities" under "Clojure-inspired
@ -328,6 +342,9 @@ described in "Miscellaneous utilities" under "Vector-related".)
@item(@c(partial) is a function defined in @c(kutils.lisp), described @item(@c(partial) is a function defined in @c(kutils.lisp), described
in "Miscellaneous utilities" under "Clojure-inspired functions".) in "Miscellaneous utilities" under "Clojure-inspired functions".)
@item(@c(read-file-string) is a macro defined in @c(macros.lisp),
described in "Macros" under "File macros".)
@item(@c(sethash) is a macro defined in @c(kutils-hash-tables.lisp), @item(@c(sethash) is a macro defined in @c(kutils-hash-tables.lisp),
described in "Hash-table related utilities".) described in "Hash-table related utilities".)
@ -338,21 +355,30 @@ Lisp utilities".)
"Miscellaneous utilities" under "Clojure-inspired functions".) "Miscellaneous utilities" under "Clojure-inspired functions".)
@item(@c(unlesslet) is a macro defined in @c(macros.lisp), described in @item(@c(unlesslet) is a macro defined in @c(macros.lisp), described in
"Macros".) "Macros" under "Let macros".)
@item(@c(unlesslet*) is a macro defined in @c(macros.lisp), described in @item(@c(unlesslet*) is a macro defined in @c(macros.lisp), described in
"Macros".) "Macros" under "Let macros".)
@item(@c(whenlet) is a macro defined in @c(macros.lisp), described in @item(@c(whenlet) is a macro defined in @c(macros.lisp), described in
"Macros".) "Macros" under "Let macros".)
@item(@c(whenlet*) is a macro defined in @c(macros.lisp), described in @item(@c(whenlet*) is a macro defined in @c(macros.lisp), described in
"Macros".) "Macros" under "Let macros".)
@item(@c(with-new-hash-table) is a macro defined in @item(@c(with-new-hash-table) is a macro defined in
@c(kutils-hash-tables.lisp), described in "Hash-table related @c(kutils-hash-tables.lisp), described in "Hash-table related
utilities".) utilities".)
@item(@c(with-read-from-file) is a macro defined in @c(macros.lisp),
described in "Macros" under "File macros".)
@item(@c(with-string-output-to-file) is a macro defined in @c(macros.lisp),
described in "Macros" under "File macros".)
@item(@c(with-write-to-file) is a macro defined in @c(macros.lisp),
described in "Macros" under "File macros".)
@item(@c(zip) is a function defined in @c(kutils.lisp), described in @item(@c(zip) is a function defined in @c(kutils.lisp), described in
"Miscellaneous utilities" under "General".) "Miscellaneous utilities" under "General".)
@ -396,6 +422,7 @@ Alphabetical documentation for all exported symbols.
@cl:doc(function new-hash-table) @cl:doc(function new-hash-table)
@cl:doc(function new-vector) @cl:doc(function new-vector)
@cl:doc(function partial) @cl:doc(function partial)
@cl:doc(macro read-file-string)
@cl:doc(macro sethash) @cl:doc(macro sethash)
@cl:doc(function symb) @cl:doc(function symb)
@cl:doc(function take) @cl:doc(function take)
@ -404,6 +431,9 @@ Alphabetical documentation for all exported symbols.
@cl:doc(macro whenlet) @cl:doc(macro whenlet)
@cl:doc(macro whenlet*) @cl:doc(macro whenlet*)
@cl:doc(macro with-new-hash-table) @cl:doc(macro with-new-hash-table)
@cl:doc(macro with-read-from-file)
@cl:doc(macro with-string-output-to-file)
@cl:doc(macro with-write-to-file)
@cl:doc(function zip)) @cl:doc(function zip))
@end(section) @end(section)

View File

@ -83,14 +83,6 @@ additional args provided to the lambda."
(cons (mapcar #'car lsts) (zip-acc (mapcar #'cdr lsts)))))) (cons (mapcar #'car lsts) (zip-acc (mapcar #'cdr lsts))))))
(zip-acc lsts))) (zip-acc lsts)))
(defun read-file-string (path)
"Read the contents of the file at path as a string."
(with-open-file (s path)
(let ((data (make-string (file-length s))))
(read-sequence data s)
data)))
(defun new-vector () (defun new-vector ()
"Create a new, empty, adjustable vector with fill pointer." "Create a new, empty, adjustable vector with fill pointer."
(make-array 0 :adjustable t :fill-pointer t)) (make-array 0 :adjustable t :fill-pointer t))
@ -150,3 +142,5 @@ effectful code, such as logging."
(lambda (&rest args) (lambda (&rest args)
(dolist (fn fns) (dolist (fn fns)
(apply fn args)))) (apply fn args))))

View File

@ -68,3 +68,50 @@ a @c(cond)."
`(let (,@bindings) `(let (,@bindings)
(cond ,@forms)))) (cond ,@forms))))
(defmacro! read-file-as-string (path &rest args &key (direction nil directionp)
&allow-other-keys)
"Read the contents of the file at @c(path) as a string. Any
remaining arguments are sent to @c(with-open-file)."
(when directionp
(error "Specifying :direction is not allowed with READ-FILE-STRING."))
`(with-open-file (g!s ,path ,@args)
(let ((g!data (make-string (file-length g!s))))
(read-sequence g!data g!s)
g!data)))
(defmacro! with-string-output-to-file ((path &rest args &key (direction :output directionp)
&allow-other-keys)
&body body)
"Evaluate @c(body), and call @c(mkstr) on the result. Write the
resulting string to @c(path). Any remaining arguments are sent to
@c(with-open-file)."
(when directionp
(error "Specifying :direction is not allowed with WRITE-FILE-STRING."))
`(with-open-file (g!s ,path :direction ,direction ,@args)
(with-standard-io-syntax
(let ((o!out (mkstr ,@body)))
(princ o!out g!s)))))
(defmacro with-read-from-file (path &rest args &key (direction nil directionp)
&allow-other-keys)
"Read the form contained in @c(path), with any remaining arguments
passed to @c(with-open-file)."
(let ((s (gensym)))
(declare (ignore direction))
(when directionp
(error "Specifying :direction is not allowed with READ-FILE."))
`(with-open-file (,s ,path ,@args)
(with-standard-io-syntax
(read ,s)))))
(defmacro with-write-to-file ((stream path &rest args
&key (direction :output directionp)
&allow-other-keys)
&body body)
"Evaluate @c(body) with the file at @c(path) opened and bound to the
value of @c(stream). Any remaining keyword arguments are passed to
@c(with-open-file)."
(when directionp
(error "Specifying :direction is not allowed with WRITE-FILE."))
`(with-open-file (,stream ,path :direction ,direction ,@args)
,@body))

View File

@ -50,4 +50,8 @@
#:iflet* #:iflet*
#:condlet #:condlet
#:condlet* #:condlet*
#:read-file-string
#:with-string-output-to-file
#:with-read-from-file
#:with-write-to-file
)) ))