binary-ports.scm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ;;; Guile binary ports
  2. ;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Guile binary ports module.
  18. ;;;
  19. ;;; Code:
  20. (define-module (ice-9 binary-ports)
  21. #:use-module ((hoot errors) #:select (make-unimplemented-error raise))
  22. #:use-module ((hoot ports) #:select (get-output-bytevector
  23. open-input-bytevector
  24. open-output-bytevector
  25. peek-u8
  26. read-bytevector
  27. read-bytevector!
  28. read-u8
  29. write-u8
  30. write-bytevector))
  31. #:use-module (rnrs bytevectors)
  32. #:re-export (eof-object)
  33. #:export (open-bytevector-input-port
  34. open-bytevector-output-port
  35. get-u8
  36. lookahead-u8
  37. get-bytevector-n
  38. get-bytevector-n!
  39. get-bytevector-some
  40. get-bytevector-some!
  41. get-bytevector-all
  42. get-string-n!
  43. put-u8
  44. put-bytevector
  45. unget-bytevector
  46. make-custom-binary-input-port
  47. make-custom-binary-output-port
  48. make-custom-binary-input/output-port
  49. call-with-input-bytevector
  50. call-with-output-bytevector))
  51. (define* (open-bytevector-input-port bv #:optional transcoder)
  52. "Return an input port whose contents are drawn from bytevector
  53. @var{bv}."
  54. (open-input-bytevector bv))
  55. (define (call-with-input-bytevector bv proc)
  56. "Call the one-argument procedure @var{proc} with a newly created
  57. binary input port from which the bytevector @var{bv}'s contents may be
  58. read. All values yielded by @var{proc} are returned."
  59. (proc (open-input-bytevector bv)))
  60. (define* (open-bytevector-output-port #:optional transcoder)
  61. "Return two values: an output port and a procedure that returns a
  62. bytevector containing all the output written to that port.."
  63. (let ((port (open-output-bytevector)))
  64. (values port (lambda () (get-output-bytevector port)))))
  65. (define (call-with-output-bytevector proc)
  66. "Call the one-argument procedure @var{proc} with a newly created
  67. binary output port. When the function returns, port is closed and the
  68. bytevector composed of the bytes written into the port is returned."
  69. (let ((port (open-output-bytevector)))
  70. (proc port)
  71. (let ((bv (get-output-bytevector port)))
  72. (close-port port)
  73. bv)))
  74. (define (get-u8 port)
  75. "Read an octet from @var{port}, a binary input port, blocking as
  76. necessary."
  77. (read-u8 port))
  78. (define (lookahead-u8 port)
  79. "Like @code{get-u8} but does not update @var{port} to point past the
  80. octet."
  81. (peek-u8 port))
  82. (define (get-bytevector-n port count)
  83. "Read @var{count} octets from @var{port}, blocking as necessary and
  84. return a bytevector containing the octets read. If fewer bytes are
  85. available, a bytevector smaller than @var{count} is returned."
  86. (read-bytevector count port))
  87. (define (get-bytevector-n! port bv start count)
  88. "Read @var{count} bytes from @var{port} and store them in @var{bv}
  89. starting at index @var{start}. Return either the number of bytes
  90. actually read or the end-of-file object."
  91. (read-bytevector! bv port start (+ start count)))
  92. (define (get-bytevector-some port)
  93. "Read from @var{port}, blocking as necessary, until bytes are available
  94. or an end-of-file is reached. Return either the end-of-file object or
  95. a new bytevector containing some of the available bytes (at least
  96. one), and update the port position to point just past these bytes."
  97. (raise (make-unimplemented-error 'get-bytevector-some)))
  98. (define (get-bytevector-some! port bv start count)
  99. "Read up to @var{count} bytes from @var{port}, blocking as necessary
  100. until at least one byte is available or an end-of-file is reached.
  101. Store them in @var{bv} starting at index @var{start}. Return the
  102. number of bytes actually read, or an end-of-file object."
  103. (raise (make-unimplemented-error 'get-bytevector-some!)))
  104. (define (get-bytevector-all port)
  105. "Read from @var{port}, blocking as necessary, until
  106. the end-of-file is reached. Return either a new bytevector containing
  107. the data read or the end-of-file object (if no data were available)."
  108. (raise (make-unimplemented-error 'get-bytevector-all)))
  109. (define (get-string-n! port str start count)
  110. "Read up to @var{count} characters from @var{port} into @var{str},
  111. starting at @var{start}. If no characters can be read before the end
  112. of file is encountered, the end of file object is returned.
  113. Otherwise, the number of characters read is returned."
  114. (raise (make-unimplemented-error 'get-string-n!)))
  115. (define (put-u8 port octet)
  116. "Write @var{octet} to binary port @var{port}."
  117. (write-u8 octet port))
  118. (define* (put-bytevector port bv #:optional (start 0)
  119. (count (- (bytevector-length bv) start)))
  120. "Write the contents of @var{bv} to @var{port}, optionally starting at
  121. index @var{start} and limiting to @var{count} octets."
  122. (write-bytevector bv port start (+ start count)))
  123. (define* (unget-bytevector port bv #:optional (start 0)
  124. (count (- (bytevector-length bv) start)))
  125. "Unget the contents of @var{bv} to @var{port}, optionally starting at
  126. index @var{start} and limiting to @var{count} octets."
  127. (raise (make-unimplemented-error 'unget-bytevector)))
  128. (define (make-custom-binary-input-port id read get-position set-position! close)
  129. (raise (make-unimplemented-error 'make-custom-binary-input-port)))
  130. (define (make-custom-binary-output-port id write get-position set-position! close)
  131. (raise (make-unimplemented-error 'make-custom-binary-output-port)))
  132. (define (make-custom-binary-input/output-port id read write get-position set-position! close)
  133. (raise (make-unimplemented-error 'make-custom-binary-input/output-port)))