From 80d15411b74f3eca6969d7276152500dd8ece463 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Fri, 7 May 2021 16:45:59 +0530 Subject: Define with-file and with-data-file sc macros. * src/macros/macros.sc (with-file, with-data-file): New macros. --- src/macros/macros.sc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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))) -- cgit v1.2.3