home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / runtime / strott.scm < prev    next >
Text File  |  1999-02-24  |  3KB  |  87 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: strott.scm,v 14.9 1999/02/24 21:36:25 cph Exp $
  4.  
  5. Copyright (c) 1988-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; String Output Ports (Truncated)
  23. ;;; package: (runtime truncated-string-output)
  24.  
  25. (declare (usual-integrations))
  26.  
  27. (define (initialize-package!)
  28.   (set! output-string-port-type
  29.     (make-port-type `((WRITE-SELF ,operation/write-self)
  30.               (WRITE-CHAR ,operation/write-char)
  31.               (WRITE-SUBSTRING ,operation/write-substring))
  32.             #f)))
  33.  
  34. (define (with-output-to-truncated-string max thunk)
  35.   (call-with-current-continuation
  36.    (lambda (return)
  37.      (cons #f
  38.        (apply string-append
  39.           (reverse!
  40.            (let ((state
  41.               (make-output-string-state return max '() max)))
  42.              (with-output-to-port
  43.              (make-port output-string-port-type state)
  44.                thunk)
  45.              (output-string-state/accumulator state))))))))
  46.  
  47. (define output-string-port-type)
  48.  
  49. (define-structure (output-string-state (type vector)
  50.                        (conc-name output-string-state/))
  51.   (return #f read-only #t)
  52.   (max-length #f read-only #t)
  53.   accumulator
  54.   counter)
  55.  
  56. (define (operation/write-char port char)
  57.   (let ((state (port/state port)))
  58.     (let ((accumulator (output-string-state/accumulator state))
  59.       (counter (output-string-state/counter state)))
  60.       (if (zero? counter)
  61.       ((output-string-state/return state)
  62.        (cons #t (apply string-append (reverse! accumulator))))
  63.       (begin
  64.         (set-output-string-state/accumulator!
  65.          state
  66.          (cons (string char) accumulator))
  67.         (set-output-string-state/counter! state (-1+ counter)))))))
  68.  
  69. (define (operation/write-substring port string start end)
  70.   (let ((state (port/state port)))
  71.     (let ((accumulator
  72.        (cons (substring string start end)
  73.          (output-string-state/accumulator state)))
  74.       (counter (- (output-string-state/counter state) (- end start))))
  75.       (if (negative? counter)
  76.       ((output-string-state/return state)
  77.        (cons #t
  78.          (substring (apply string-append (reverse! accumulator))
  79.                 0
  80.                 (output-string-state/max-length state))))
  81.       (begin
  82.         (set-output-string-state/accumulator! state accumulator)
  83.         (set-output-string-state/counter! state counter))))))
  84.  
  85. (define (operation/write-self port output-port)
  86.   port
  87.   (write-string " to string (truncating)" output-port))