lineio.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1996, 1998 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. ;;;; Boston, MA 02111-1307 USA
  18. ;;;;
  19. (define-module (ice-9 lineio))
  20. ;;; {Line Buffering Input Ports}
  21. ;;;
  22. ;;; [This is a work-around to get past certain deficiencies in the capabilities
  23. ;;; of ports. Eventually, ports should be fixed and this module nuked.]
  24. ;;;
  25. ;;; A line buffering input port supports:
  26. ;;;
  27. ;;; read-string which returns the next line of input
  28. ;;; unread-string which pushes a line back onto the stream
  29. ;;;
  30. ;;; The implementation of unread-string is kind of limited; it doesn't
  31. ;;; interact properly with unread-char, or any of the other port
  32. ;;; reading functions. Only read-string will get you back the things that
  33. ;;; unread-string accepts.
  34. ;;;
  35. ;;; Normally a "line" is all characters up to and including a newline.
  36. ;;; If lines are put back using unread-string, they can be broken arbitrarily
  37. ;;; -- that is, read-string returns strings passed to unread-string (or
  38. ;;; shared substrings of them).
  39. ;;;
  40. ;; read-string port
  41. ;; unread-string port str
  42. ;; Read (or buffer) a line from PORT.
  43. ;;
  44. ;; Not all ports support these functions -- only those with
  45. ;; 'unread-string and 'read-string properties, bound to hooks
  46. ;; implementing these functions.
  47. ;;
  48. (define-public (unread-string str line-buffering-input-port)
  49. ((object-property line-buffering-input-port 'unread-string) str))
  50. ;;
  51. (define-public (read-string line-buffering-input-port)
  52. ((object-property line-buffering-input-port 'read-string)))
  53. (define-public (lineio-port? port)
  54. (not (not (object-property port 'read-string))))
  55. ;; make-line-buffering-input-port port
  56. ;; Return a wrapper for PORT. The wrapper handles read-string/unread-string.
  57. ;;
  58. ;; The port returned by this function reads newline terminated lines from PORT.
  59. ;; It buffers these characters internally, and parsels them out via calls
  60. ;; to read-char, read-string, and unread-string.
  61. ;;
  62. (define-public (make-line-buffering-input-port underlying-port)
  63. (let* (;; buffers - a list of strings put back by unread-string or cached
  64. ;; using read-line.
  65. ;;
  66. (buffers '())
  67. ;; getc - return the next character from a buffer or from the underlying
  68. ;; port.
  69. ;;
  70. (getc (lambda ()
  71. (if (not buffers)
  72. (read-char underlying-port)
  73. (let ((c (string-ref (car buffers))))
  74. (if (= 1 (string-length (car buffers)))
  75. (set! buffers (cdr buffers))
  76. (set-car! buffers (make-shared-substring (car buffers) 1)))
  77. c))))
  78. (propogate-close (lambda () (close-port underlying-port)))
  79. (self (make-soft-port (vector #f #f #f getc propogate-close) "r"))
  80. (unread-string (lambda (str)
  81. (and (< 0 (string-length str))
  82. (set! buffers (cons str buffers)))))
  83. (read-string (lambda ()
  84. (cond
  85. ((not (null? buffers))
  86. (let ((answer (car buffers)))
  87. (set! buffers (cdr buffers))
  88. answer))
  89. (else
  90. (read-line underlying-port 'concat)))))) ;handle-newline->concat
  91. (set-object-property! self 'unread-string unread-string)
  92. (set-object-property! self 'read-string read-string)
  93. self))