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 / strnin.scm < prev    next >
Text File  |  1999-02-24  |  4KB  |  131 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: strnin.scm,v 14.8 1999/02/24 21:36:21 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 I/O Ports
  23. ;;; package: (runtime string-input)
  24.  
  25. (declare (usual-integrations))
  26.  
  27. (define (initialize-package!)
  28.   (set! input-string-port-type
  29.     (make-port-type `((CHAR-READY? ,operation/char-ready?)
  30.               (DISCARD-CHAR ,operation/discard-char)
  31.               (DISCARD-CHARS ,operation/discard-chars)
  32.               (PEEK-CHAR ,operation/peek-char)
  33.               (WRITE-SELF ,operation/write-self)
  34.               (READ-CHAR ,operation/read-char)
  35.               (READ-STRING ,operation/read-string))
  36.             #f))
  37.   unspecific)
  38.  
  39. (define (with-input-from-string string thunk)
  40.   (with-input-from-port (string->input-port string) thunk))
  41.  
  42. (define (string->input-port string #!optional start end)
  43.   (let ((end
  44.      (if (default-object? end)
  45.          (string-length string)
  46.          (check-index end (string-length string) 'STRING->INPUT-PORT))))
  47.     (make-port
  48.      input-string-port-type
  49.      (make-input-string-state string
  50.                   (if (default-object? start)
  51.                   0
  52.                   (check-index start end 'STRING->INPUT-PORT))
  53.                   end))))
  54.  
  55. (define (check-index index limit procedure)
  56.   (if (not (exact-nonnegative-integer? index))
  57.       (error:wrong-type-argument index "exact non-negative integer" procedure))
  58.   (if (not (<= index limit))
  59.       (error:bad-range-argument index procedure))
  60.   index)
  61.  
  62. (define input-string-port-type)
  63.  
  64. (define-structure (input-string-state (type vector)
  65.                       (conc-name input-string-state/))
  66.   (string #f read-only #t)
  67.   start
  68.   (end #f read-only #t))
  69.  
  70. (define-integrable (input-port/string port)
  71.   (input-string-state/string (port/state port)))
  72.  
  73. (define-integrable (input-port/start port)
  74.   (input-string-state/start (port/state port)))
  75.  
  76. (define-integrable (set-input-port/start! port index)
  77.   (set-input-string-state/start! (port/state port) index))
  78.  
  79. (define-integrable (input-port/end port)
  80.   (input-string-state/end (port/state port)))
  81.  
  82. (define (operation/char-ready? port interval)
  83.   interval
  84.   (fix:< (input-port/start port) (input-port/end port)))
  85.  
  86. (define (operation/peek-char port)
  87.   (if (fix:< (input-port/start port) (input-port/end port))
  88.       (string-ref (input-port/string port) (input-port/start port))
  89.       (make-eof-object port)))
  90.  
  91. (define (operation/discard-char port)
  92.   (set-input-port/start! port (fix:+ (input-port/start port) 1)))
  93.  
  94. (define (operation/read-char port)
  95.   (let ((start (input-port/start port)))
  96.     (if (fix:< start (input-port/end port))
  97.     (begin
  98.       (set-input-port/start! port (fix:+ start 1))
  99.       (string-ref (input-port/string port) start))
  100.     (make-eof-object port))))
  101.  
  102. (define (operation/read-string port delimiters)
  103.   (let ((start (input-port/start port))
  104.     (end (input-port/end port)))
  105.     (if (fix:< start end)
  106.     (let ((string (input-port/string port)))
  107.       (let ((index
  108.          (or (substring-find-next-char-in-set string
  109.                               start
  110.                               end
  111.                               delimiters)
  112.              end)))
  113.         (set-input-port/start! port index)
  114.         (substring string start index)))
  115.     (make-eof-object port))))
  116.  
  117. (define (operation/discard-chars port delimiters)
  118.   (let ((start (input-port/start port))
  119.     (end (input-port/end port)))
  120.     (if (fix:< start end)
  121.     (set-input-port/start!
  122.      port
  123.      (or (substring-find-next-char-in-set (input-port/string port)
  124.                           start
  125.                           end
  126.                           delimiters)
  127.          end)))))
  128.  
  129. (define (operation/write-self port output-port)
  130.   port
  131.   (write-string " from string" output-port))