Welcome to the CHICKEN Scheme pasting service
with-file added by nalssee on Mon May 12 10:50:15 2014
(adm with-file
((with-file (port file direction) . es)
;; I could've done it like 'append not ''append
;; but I suppose it's a tradition. I'm not sure tho.
(cond ((compare? ''append direction)
`(let* ((fn (file-open ,file (+ open/wronly open/append open/creat)))
(,port (open-output-file* fn)))
(dynamic-wind
(lambda () #f)
(lambda () ,@es)
(lambda ()
(close-output-port ,port)
;; The following line must not be included
;; No idea why but just closing the port is enough
;; (file-close fn)
)
)))
((or (compare? ''write direction) (compare? ''create direction))
`(call-with-output-file ,file
(lambda (,port)
,@es)))
(else
(error "Unknown direction" form))))
((with-file (port file) . es)
`(call-with-input-file ,file
(lambda (,port)
,@es))))