buffered-input.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;;; buffered-input.scm --- construct a port from a buffered input reader
  2. ;;;;
  3. ;;;; Copyright (C) 2001, 2006, 2010 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (ice-9 buffered-input)
  19. #:export (make-buffered-input-port
  20. make-line-buffered-input-port
  21. set-buffered-input-continuation?!))
  22. ;; @code{buffered-input-continuation?} is a property of the ports
  23. ;; created by @code{make-line-buffered-input-port} that stores the
  24. ;; read continuation flag for each such port.
  25. (define buffered-input-continuation? (make-object-property))
  26. (define (set-buffered-input-continuation?! port val)
  27. "Set the read continuation flag for @var{port} to @var{val}.
  28. See @code{make-buffered-input-port} for the meaning and use of this
  29. flag."
  30. (set! (buffered-input-continuation? port) val))
  31. (define (make-buffered-input-port reader)
  32. "Construct a line-buffered input port from the specified @var{reader}.
  33. @var{reader} should be a procedure of one argument that somehow reads
  34. a chunk of input and returns it as a string.
  35. The port created by @code{make-buffered-input-port} does @emph{not}
  36. interpolate any additional characters between the strings returned by
  37. @var{reader}.
  38. @var{reader} should take a boolean @var{continuation?} argument.
  39. @var{continuation?} indicates whether @var{reader} is being called to
  40. start a logically new read operation (in which case
  41. @var{continuation?} is @code{#f}) or to continue a read operation for
  42. which some input has already been read (in which case
  43. @var{continuation?} is @code{#t}). Some @var{reader} implementations
  44. use the @var{continuation?} argument to determine what prompt to
  45. display to the user.
  46. The new/continuation distinction is largely an application-level
  47. concept: @code{set-buffered-input-continuation?!} allows an
  48. application to specify when a read operation is considered to be new.
  49. But note that if there is non-whitespace data already buffered in the
  50. port when a new read operation starts, this data will be read before
  51. the first call to @var{reader}, and so @var{reader} will be called
  52. with @var{continuation?} set to @code{#t}."
  53. (let ((read-string "")
  54. (string-index 0))
  55. (letrec ((get-character
  56. (lambda ()
  57. (if (< string-index (string-length read-string))
  58. ;; Read a char.
  59. (let ((res (string-ref read-string string-index)))
  60. (set! string-index (+ 1 string-index))
  61. (if (not (char-whitespace? res))
  62. (set! (buffered-input-continuation? port) #t))
  63. res)
  64. ;; Fill the buffer.
  65. (let ((x (reader (buffered-input-continuation? port))))
  66. (cond
  67. ((eof-object? x)
  68. ;; Don't buffer the EOF object.
  69. x)
  70. (else
  71. (set! read-string x)
  72. (set! string-index 0)
  73. (get-character)))))))
  74. (input-waiting
  75. (lambda ()
  76. (- (string-length read-string) string-index)))
  77. (port #f))
  78. (set! port (make-soft-port (vector #f #f #f get-character #f input-waiting) "r"))
  79. (set! (buffered-input-continuation? port) #f)
  80. port)))
  81. (define (make-line-buffered-input-port reader)
  82. "Construct a line-buffered input port from the specified @var{reader}.
  83. @var{reader} should be a procedure of one argument that somehow reads
  84. a line of input and returns it as a string @emph{without} the
  85. terminating newline character.
  86. The port created by @code{make-line-buffered-input-port} automatically
  87. interpolates a newline character after each string returned by
  88. @var{reader}.
  89. @var{reader} should take a boolean @var{continuation?} argument. For
  90. the meaning and use of this argument, see
  91. @code{make-buffered-input-port}."
  92. (make-buffered-input-port (lambda (continuation?)
  93. (let ((str (reader continuation?)))
  94. (if (eof-object? str)
  95. str
  96. (string-append str "\n"))))))
  97. ;;; buffered-input.scm ends here