1;;; File input/output utilities 2 3;; Copyright (C) 2004 Brailcom, o.p.s. 4 5;; Author: Milan Zamazal <pdm@brailcom.org> 6 7;; COPYRIGHT NOTICE 8 9;; This program is free software; you can redistribute it and/or modify 10;; it under the terms of the GNU General Public License as published by 11;; the Free Software Foundation; either version 2 of the License, or 12;; (at your option) any later version. 13 14;; This program is distributed in the hope that it will be useful, but 15;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17;; for more details. 18 19;; You should have received a copy of the GNU General Public License 20;; along with this program; if not, write to the Free Software 21;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 23 24(require 'util) 25 26 27(defmac (with-open-file form) 28 (let* ((spec (nth 1 form)) 29 (body (nth_cdr 2 form)) 30 (var (nth 0 spec)) 31 (filename (nth 1 spec)) 32 (how (or (nth 2 spec) "r"))) 33 `(let ((,var (fopen ,filename ,how))) 34 (unwind-protect* (begin ,@body) 35 (fclose ,var))))) 36 37(defmac (with-temp-file-data form) 38 (let* ((spec (nth 1 form)) 39 (body (nth_cdr 2 form)) 40 (filename (nth 0 spec)) 41 (data (nth 1 spec))) 42 `(with-temp-file ,filename 43 (write-file ,filename ,data) 44 ,@body))) 45 46(define (write-file filename string) 47 (with-open-file (f filename "w") 48 (fwrite (if (symbol? string) (format nil "%s" string) string) f))) 49 50(define (read-file filename) 51 (with-open-file (f filename) 52 (let* ((strings '()) 53 (buffer (format nil "%1024s" "")) 54 (buflen (length buffer)) 55 (n 0) 56 (reading t)) 57 (while reading 58 (set! n (fread buffer f)) 59 (if n 60 (begin 61 (push (substring buffer 0 n) strings) 62 (when (< n buflen) 63 (set! reading nil))) 64 (set! reading nil))) 65 (apply string-append (reverse strings))))) 66 67(define (make-read-line-state) 68 (list "")) 69 70(define (read-line file state) 71 (let* ((text (car state)) 72 (line (and text (string-before text "\n")))) 73 (cond 74 ((not text) 75 nil) 76 ((equal? line "") 77 (let* ((buffer (format nil "%256s" "")) 78 (n (fread buffer file))) 79 (cond 80 ((and (not n) (eqv? text "")) 81 (set! line nil) 82 (set! text nil)) 83 ((not n) 84 (set! line text) 85 (set! text nil)) 86 (t 87 (set! text (string-append text (substring buffer 0 n))) 88 (let ((state* (list text))) 89 (set! line (read-line file state*)) 90 (set! text (car state*))))))) 91 (t 92 (set! text (string-after text "\n")))) 93 (set-car! state text) 94 line)) 95 96 97(provide 'fileio) 98