aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Isaac2021-05-07 16:45:59 +0530
committerArun Isaac2021-05-07 17:06:27 +0530
commit80d15411b74f3eca6969d7276152500dd8ece463 (patch)
treef56ee80e30482838bd39bac163ceb59ab885f7be
parent4224e183522b696e60556c8ea88dec5409d8a746 (diff)
downloadnsmc-80d15411b74f3eca6969d7276152500dd8ece463.tar.gz
nsmc-80d15411b74f3eca6969d7276152500dd8ece463.tar.lz
nsmc-80d15411b74f3eca6969d7276152500dd8ece463.zip
Define with-file and with-data-file sc macros.
* src/macros/macros.sc (with-file, with-data-file): New macros.
-rw-r--r--src/macros/macros.sc16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/macros/macros.sc b/src/macros/macros.sc
index 8ff799b..1ccfeab 100644
--- a/src/macros/macros.sc
+++ b/src/macros/macros.sc
@@ -67,3 +67,19 @@
body ...
(for-i i n
(gsl-rstat-free (array-get var i)))))
+
+(sc-define-syntax (with-file fp filename mode body ...)
+ (with-alloc fp FILE* (begin (fopen filename mode)
+ (unless fp
+ (perror filename)
+ (exit EXIT_FAILURE)))
+ fclose
+ body ...))
+
+(sc-define-syntax* (with-data-file fp filename mode body ...)
+ (let* ((data-file (string-append "data/" filename)))
+ `(with-file fp ,data-file ,mode
+ ,@(if (string=? mode "w")
+ `((fprintf stderr "Writing to %s...\n" ,data-file))
+ '())
+ ,@body)))