r8632: updates
[kmrcl.git] / io.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          io.lisp
6 ;;;; Purpose:       Input/Output functions for KMRCL package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002-2003 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package #:kmrcl)
20
21 (defun print-file-contents (file &optional (strm *standard-output*))
22   "Opens a reads a file. Returns the contents as a single string"
23   (when (probe-file file)
24     (let ((eof (cons 'eof nil)))
25       (with-open-file (in file :direction :input)
26         (do ((line (read-line in nil eof) 
27                    (read-line in nil eof)))
28             ((eq line eof))
29           (write-string line strm)
30           (write-char #\newline strm))))))
31
32 (defun read-file-to-string (file)
33   "Opens a reads a file. Returns the contents as a single string"
34   (with-output-to-string (out)
35     (with-open-file (in file :direction :input)
36       (let ((eof (gensym)))                 
37         (do ((line (read-line in nil eof) 
38                    (read-line in nil eof)))
39             ((eq line eof))
40           (format out "~A~%" line))))))
41
42 (defun read-file-to-strings (file)
43   "Opens a reads a file. Returns the contents as a list of strings"
44   (let ((lines '()))
45     (with-open-file (in file :direction :input)
46       (let ((eof (gensym)))                 
47         (do ((line (read-line in nil eof) 
48                    (read-line in nil eof)))
49             ((eq line eof))
50           (push line lines)))
51       (nreverse lines))))
52
53 (defun file-subst (old new file1 file2)
54   (with-open-file (in file1 :direction :input)
55     (with-open-file (out file2 :direction :output
56                          :if-exists :supersede)
57       (stream-subst old new in out))))
58
59 (defun print-n-chars (char n stream)
60   (declare (fixnum n) (optimize (speed 3) (safety 0) (space 0)))
61   (dotimes (i n)
62     (declare (fixnum i))
63     (write-char char stream)))
64
65 (defun print-n-strings (str n stream)
66   (declare (fixnum n) (optimize (speed 3) (safety 0) (space 0)))
67   (dotimes (i n)
68     (declare (fixnum i))
69     (write-string str stream)))
70
71 (defun indent-spaces (n &optional (stream *standard-output*))
72   "Indent n*2 spaces to output stream"
73   (print-n-chars #\space (+ n n) stream))
74
75
76 (defun indent-html-spaces (n &optional (stream *standard-output*))
77   "Indent n*2 html spaces to output stream"
78   (print-n-strings " " (+ n n) stream))
79      
80
81 (defun print-list (l &optional (output *standard-output*))
82   "Print a list to a stream"
83   (format output "~{~A~%~}" l))
84
85 (defun print-rows (rows &optional (ostrm *standard-output*))
86   "Print a list of list rows to a stream"  
87   (dolist (r rows) (format ostrm "~{~A~^ ~}~%" r)))
88
89
90 ;; Buffered stream substitute
91
92 (defstruct buf
93   vec (start -1) (used -1) (new -1) (end -1))
94
95 (defun bref (buf n)
96   (svref (buf-vec buf)
97          (mod n (length (buf-vec buf)))))
98
99 (defun (setf bref) (val buf n)
100   (setf (svref (buf-vec buf)
101                (mod n (length (buf-vec buf))))
102         val))
103
104 (defun new-buf (len)
105   (make-buf :vec (make-array len)))
106
107 (defun buf-insert (x b)
108   (setf (bref b (incf (buf-end b))) x))
109
110 (defun buf-pop (b)
111   (prog1 
112     (bref b (incf (buf-start b)))
113     (setf (buf-used b) (buf-start b)
114           (buf-new  b) (buf-end   b))))
115
116 (defun buf-next (b)
117   (when (< (buf-used b) (buf-new b))
118     (bref b (incf (buf-used b)))))
119
120 (defun buf-reset (b)
121   (setf (buf-used b) (buf-start b)
122         (buf-new  b) (buf-end   b)))
123
124 (defun buf-clear (b)
125   (setf (buf-start b) -1 (buf-used  b) -1
126         (buf-new   b) -1 (buf-end   b) -1))
127
128 (defun buf-flush (b str)
129   (do ((i (1+ (buf-used b)) (1+ i)))
130       ((> i (buf-end b)))
131     (princ (bref b i) str)))
132
133
134 (defun stream-subst (old new in out)
135   (declare (string old new))
136   (let* ((pos 0)
137          (len (length old))
138          (buf (new-buf len))
139          (from-buf nil))
140     (declare (fixnum pos len))
141     (do ((c (read-char in nil :eof)
142             (or (setf from-buf (buf-next buf))
143                 (read-char in nil :eof))))
144         ((eql c :eof))
145       (declare (character c))
146       (cond ((char= c (char old pos))
147              (incf pos)
148              (cond ((= pos len)            ; 3
149                     (princ new out)
150                     (setf pos 0)
151                     (buf-clear buf))
152                    ((not from-buf)         ; 2
153                     (buf-insert c buf))))
154             ((zerop pos)                   ; 1
155              (princ c out)
156              (when from-buf
157                (buf-pop buf)
158                (buf-reset buf)))
159             (t                             ; 4
160              (unless from-buf
161                (buf-insert c buf))
162              (princ (buf-pop buf) out)
163              (buf-reset buf)
164              (setf pos 0))))
165     (buf-flush buf out)))
166
167 (declaim (inline write-fixnum))
168 (defun write-fixnum (n s)
169   #+allegro (excl::print-fixnum s 10 n)
170   #-allegro (write-string (write-to-string n) s))
171
172
173
174 #+openmcl
175 (defun open-device-stream (path direction)
176   (let* ((mode (ecase direction
177                  (:input #.(read-from-string "#$O_RDONLY"))
178                  (:output #.(read-from-string "#$O_WRONLY"))
179                  (:io #.(read-from-string "#$O_RDWR"))))
180          (fd (ccl::fd-open (ccl::native-translated-namestring path) mode)))
181     (if (< fd 0)
182        (ccl::signal-file-error fd path)
183        (ccl::make-fd-stream fd :direction direction))))
184
185
186 (defun null-output-stream ()
187   #-openmcl
188   (when (probe-file #p"/dev/null")
189     (open #p"/dev/null" :direction :output :if-exists :overwrite))
190   #+openmcl
191   (when (probe-file #p"/dev/null")
192     (open-device-stream #p"/dev/null" :output))  
193   )
194
195
196 (defun directory-tree (filename)
197   "Returns a tree of pathnames for sub-directories of a directory"
198   (let* ((root (canonicalize-directory-name filename))
199          (subdirs (loop for path in (directory
200                                      (make-pathname :name :wild
201                                                     :type :wild
202                                                     :defaults root))
203                         when (probe-directory path)
204                         collect (canonicalize-directory-name path))))
205     (when (find nil subdirs)
206       (error "~A" subdirs))
207     (when (null root)
208       (error "~A" root))
209     (if subdirs
210         (cons root (mapcar #'directory-tree subdirs))
211         (if (probe-directory root)
212             (list root)
213             (error "root not directory ~A" root)))))
214
215