From 87970984c72b5d5bae3a05c9e8986b3dc4301713 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Tue, 20 Dec 2011 00:58:27 +0900 Subject: [PATCH] Defining with-in/output-from/to-file in terms of dynamic-wind. --- lib/init-7.scm | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/init-7.scm b/lib/init-7.scm index 52f413fc..964078a1 100644 --- a/lib/init-7.scm +++ b/lib/init-7.scm @@ -514,22 +514,6 @@ (close-output-port out) res)) -(define (with-input-from-file file thunk) - (let ((old-in (current-input-port)) - (tmp-in (open-input-file file))) - (current-input-port tmp-in) - (let ((res (thunk))) - (current-input-port old-in) - res))) - -(define (with-output-to-file file thunk) - (let ((old-out (current-output-port)) - (tmp-out (open-output-file file))) - (current-output-port tmp-out) - (let ((res (thunk))) - (current-output-port old-out) - res))) - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; values @@ -603,6 +587,22 @@ (let ((dk (%dk))) (%call/cc (lambda (k) (proc (lambda x (set-dk! dk) (k (%values x)))))))) +(define (with-input-from-file file thunk) + (let ((old-in (current-input-port)) + (tmp-in (open-input-file file))) + (dynamic-wind + (lambda () (current-input-port tmp-in)) + thunk + (lambda () (current-input-port old-in))))) + +(define (with-output-to-file file thunk) + (let ((old-out (current-output-port)) + (tmp-out (open-output-file file))) + (dynamic-wind + (lambda () (current-output-port tmp-out)) + thunk + (lambda () (current-output-port old-out))))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; syntax-rules