binary-ports.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ;;; binary-ports.scm --- Binary IO on ports
  2. ;;; Copyright (C) 2009-2011,2013,2016,2019,2021 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software: you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU Lesser General Public License as
  6. ;;; published by the Free Software Foundation, either version 3 of the
  7. ;;; License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful, but
  10. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;; Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public
  15. ;;; License along with this program. If not, see
  16. ;;; <http://www.gnu.org/licenses/>.
  17. ;;; Author: Ludovic Courtès <ludo@gnu.org>
  18. ;;; Commentary:
  19. ;;;
  20. ;;; The I/O port API of the R6RS is provided by this module. In many areas
  21. ;;; it complements or refines Guile's own historical port API. For instance,
  22. ;;; it allows for binary I/O with bytevectors.
  23. ;;;
  24. ;;; Code:
  25. (define-module (ice-9 binary-ports)
  26. #:use-module (rnrs bytevectors)
  27. #:export (eof-object
  28. open-bytevector-input-port
  29. make-custom-binary-input-port
  30. get-u8
  31. lookahead-u8
  32. get-bytevector-n
  33. get-bytevector-n!
  34. get-bytevector-some
  35. get-bytevector-some! ; Guile extension, not in R6RS
  36. get-bytevector-all
  37. get-string-n!
  38. put-u8
  39. put-bytevector
  40. unget-bytevector
  41. open-bytevector-output-port
  42. make-custom-binary-output-port
  43. make-custom-binary-input/output-port
  44. call-with-input-bytevector
  45. call-with-output-bytevector))
  46. ;; Note that this extension also defines %make-transcoded-port, which is
  47. ;; not exported but is used by (rnrs io ports).
  48. (load-extension (string-append "libguile-" (effective-version))
  49. "scm_init_r6rs_ports")
  50. (define (call-with-input-bytevector bv proc)
  51. "Call the one-argument procedure @var{proc} with a newly created
  52. binary input port from which the bytevector @var{bv}'s contents may be
  53. read. All values yielded by @var{proc} are returned."
  54. (proc (open-bytevector-input-port bv)))
  55. (define (call-with-output-bytevector proc)
  56. "Call the one-argument procedure @var{proc} with a newly created
  57. binary output port. When the function returns, port is closed and the
  58. bytevector composed of the bytes written into the port is returned."
  59. (call-with-values
  60. (lambda ()
  61. (open-bytevector-output-port))
  62. (lambda (port get-bytevector)
  63. (proc port)
  64. (let ((bv (get-bytevector)))
  65. (close-port port)
  66. bv))))