source: trunk/j/src/org/armedbear/lisp/with-output-to-string.lisp @ 9266

Last change on this file since 9266 was 8921, checked in by piso, 16 years ago

WITH-OUTPUT-TO-STRING.16

File size: 1.7 KB
Line 
1;;; with-output-to-string.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: with-output-to-string.lisp,v 1.2 2005-04-11 22:34:35 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package #:system)
21
22;;; From SBCL.
23(defmacro with-output-to-string ((var &optional string &key (element-type ''character))
24         &body body)
25  "If STRING is specified, it must be a string with a fill pointer;
26   the output is incrementally appended to the string (as if by use of
27   VECTOR-PUSH-EXTEND)."
28  (multiple-value-bind (forms decls) (parse-body body)
29    (if string
30        (let ((ignored (gensym)))
31          `(let ((,var (make-fill-pointer-output-stream ,string))
32                 (,ignored ,element-type))
33             (declare (ignore ,ignored))
34             ,@decls
35             (unwind-protect
36                 (progn ,@forms)
37               (close ,var))))
38        `(let ((,var (make-string-output-stream :element-type ,element-type)))
39           ,@decls
40           (unwind-protect
41               (progn ,@forms)
42             (close ,var))
43           (get-output-stream-string ,var)))))
Note: See TracBrowser for help on using the repository browser.