69bcb649d84081078ce46cc0c1870a0c8ef8edc4
[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.7 2003/06/06 21:59:29 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 (defun print-n-chars (char n stream)
59   (declare (fixnum n)
60            (optimize (speed 3) (safety 0) (space 0)))
61   (do ((i 0 (1+ i)))
62       ((= i n) char)
63     (declare (fixnum i))
64     (write-char char stream)))
65   
66 (defun indent-spaces (n &optional (stream *standard-output*))
67   "Indent n*2 spaces to output stream"
68   (print-n-chars #\space (+ n n) stream))
69
70 (defun print-list (l &optional (output *standard-output*))
71   "Print a list to a stream"
72   (format output "~{~A~%~}" l))
73
74 (defun print-rows (rows &optional (ostrm *standard-output*))
75   "Print a list of list rows to a stream"  
76   (dolist (r rows) (format ostrm "~{~A~^ ~}~%" r)))
77
78
79 ;; Buffered stream substitute
80
81 (defstruct buf
82   vec (start -1) (used -1) (new -1) (end -1))
83
84 (defun bref (buf n)
85   (svref (buf-vec buf)
86          (mod n (length (buf-vec buf)))))
87
88 (defun (setf bref) (val buf n)
89   (setf (svref (buf-vec buf)
90                (mod n (length (buf-vec buf))))
91         val))
92
93 (defun new-buf (len)
94   (make-buf :vec (make-array len)))
95
96 (defun buf-insert (x b)
97   (setf (bref b (incf (buf-end b))) x))
98
99 (defun buf-pop (b)
100   (prog1 
101     (bref b (incf (buf-start b)))
102     (setf (buf-used b) (buf-start b)
103           (buf-new  b) (buf-end   b))))
104
105 (defun buf-next (b)
106   (when (< (buf-used b) (buf-new b))
107     (bref b (incf (buf-used b)))))
108
109 (defun buf-reset (b)
110   (setf (buf-used b) (buf-start b)
111         (buf-new  b) (buf-end   b)))
112
113 (defun buf-clear (b)
114   (setf (buf-start b) -1 (buf-used  b) -1
115         (buf-new   b) -1 (buf-end   b) -1))
116
117 (defun buf-flush (b str)
118   (do ((i (1+ (buf-used b)) (1+ i)))
119       ((> i (buf-end b)))
120     (princ (bref b i) str)))
121
122
123 (defun stream-subst (old new in out)
124   (declare (string old new))
125   (let* ((pos 0)
126          (len (length old))
127          (buf (new-buf len))
128          (from-buf nil))
129     (declare (fixnum pos len))
130     (do ((c (read-char in nil :eof)
131             (or (setf from-buf (buf-next buf))
132                 (read-char in nil :eof))))
133         ((eql c :eof))
134       (declare (character c))
135       (cond ((char= c (char old pos))
136              (incf pos)
137              (cond ((= pos len)            ; 3
138                     (princ new out)
139                     (setf pos 0)
140                     (buf-clear buf))
141                    ((not from-buf)         ; 2
142                     (buf-insert c buf))))
143             ((zerop pos)                   ; 1
144              (princ c out)
145              (when from-buf
146                (buf-pop buf)
147                (buf-reset buf)))
148             (t                             ; 4
149              (unless from-buf
150                (buf-insert c buf))
151              (princ (buf-pop buf) out)
152              (buf-reset buf)
153              (setf pos 0))))
154     (buf-flush buf out)))
155