r4891: Auto commit for Debian build
[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: io.lisp,v 1.6 2003/05/09 09:35:04 kevin Exp $
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 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     (with-open-file (in file :direction :input)
25       (let ((eof (gensym)))                 
26         (do ((line (read-line in nil eof) 
27                    (read-line in nil eof)))
28             ((eq line eof))
29           (format strm "~A~%" line))))))
30
31 (defun read-file-to-string (file)
32   "Opens a reads a file. Returns the contents as a single string"
33   (with-output-to-string (out)
34     (with-open-file (in file :direction :input)
35       (let ((eof (gensym)))                 
36         (do ((line (read-line in nil eof) 
37                    (read-line in nil eof)))
38             ((eq line eof))
39           (format out "~A~%" line))))))
40
41 (defun read-file-to-strings (file)
42   "Opens a reads a file. Returns the contents as a list of strings"
43   (let ((lines '()))
44     (with-open-file (in file :direction :input)
45       (let ((eof (gensym)))                 
46         (do ((line (read-line in nil eof) 
47                    (read-line in nil eof)))
48             ((eq line eof))
49           (push line lines)))
50       (nreverse lines))))
51
52 (defun file-subst (old new file1 file2)
53   (with-open-file (in file1 :direction :input)
54     (with-open-file (out file2 :direction :output
55                          :if-exists :supersede)
56       (stream-subst old new in out))))
57
58 (defmacro print-n-chars (char n stream)
59   (let ((i (gensym)))
60     `(dotimes (,i ,n)
61       (declare (fixnum ,i))
62       (write-char ,char ,stream))))
63   
64 (defun indent-spaces (n &optional (stream *standard-output*))
65   "Indent n*2 spaces to output stream"
66   (print-n-chars #\space (+ n n) stream))
67
68 (defun print-list (l &optional (output *standard-output*))
69   "Print a list to a stream"
70   (format output "~{~A~%~}" l))
71
72 (defun print-rows (rows &optional (ostrm *standard-output*))
73   "Print a list of list rows to a stream"  
74   (dolist (r rows) (format ostrm "~{~A~^ ~}~%" r)))
75
76
77 ;; Buffered stream substitute
78
79 (defstruct buf
80   vec (start -1) (used -1) (new -1) (end -1))
81
82 (defun bref (buf n)
83   (svref (buf-vec buf)
84          (mod n (length (buf-vec buf)))))
85
86 (defun (setf bref) (val buf n)
87   (setf (svref (buf-vec buf)
88                (mod n (length (buf-vec buf))))
89         val))
90
91 (defun new-buf (len)
92   (make-buf :vec (make-array len)))
93
94 (defun buf-insert (x b)
95   (setf (bref b (incf (buf-end b))) x))
96
97 (defun buf-pop (b)
98   (prog1 
99     (bref b (incf (buf-start b)))
100     (setf (buf-used b) (buf-start b)
101           (buf-new  b) (buf-end   b))))
102
103 (defun buf-next (b)
104   (when (< (buf-used b) (buf-new b))
105     (bref b (incf (buf-used b)))))
106
107 (defun buf-reset (b)
108   (setf (buf-used b) (buf-start b)
109         (buf-new  b) (buf-end   b)))
110
111 (defun buf-clear (b)
112   (setf (buf-start b) -1 (buf-used  b) -1
113         (buf-new   b) -1 (buf-end   b) -1))
114
115 (defun buf-flush (b str)
116   (do ((i (1+ (buf-used b)) (1+ i)))
117       ((> i (buf-end b)))
118     (princ (bref b i) str)))
119
120
121 (defun stream-subst (old new in out)
122   (declare (string old new))
123   (let* ((pos 0)
124          (len (length old))
125          (buf (new-buf len))
126          (from-buf nil))
127     (declare (fixnum pos len))
128     (do ((c (read-char in nil :eof)
129             (or (setf from-buf (buf-next buf))
130                 (read-char in nil :eof))))
131         ((eql c :eof))
132       (declare (character c))
133       (cond ((char= c (char old pos))
134              (incf pos)
135              (cond ((= pos len)            ; 3
136                     (princ new out)
137                     (setf pos 0)
138                     (buf-clear buf))
139                    ((not from-buf)         ; 2
140                     (buf-insert c buf))))
141             ((zerop pos)                   ; 1
142              (princ c out)
143              (when from-buf
144                (buf-pop buf)
145                (buf-reset buf)))
146             (t                             ; 4
147              (unless from-buf
148                (buf-insert c buf))
149              (princ (buf-pop buf) out)
150              (buf-reset buf)
151              (setf pos 0))))
152     (buf-flush buf out)))
153