custom-ports.scm 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ;;; Guile custom 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 custom ports module.
  18. ;;;
  19. ;;; Code:
  20. (define-module (ice-9 custom-ports)
  21. #:use-module (hoot ports)
  22. #:export (make-custom-port))
  23. (define (default-read-wait-fd port) #f)
  24. (define (default-write-wait-fd port) #f)
  25. (define (default-input-waiting? port) #t)
  26. (define (default-get-natural-buffer-sizes port read-buf-size write-buf-size)
  27. (values read-buf-size write-buf-size))
  28. (define (default-truncate port length)
  29. (error "custom port did not define a truncate method" port))
  30. (define* (make-custom-port #:key
  31. read
  32. write
  33. (read-wait-fd default-read-wait-fd)
  34. (input-waiting? (and read default-input-waiting?))
  35. (write-wait-fd default-write-wait-fd)
  36. (seek #f)
  37. (random-access? #f)
  38. (close #f)
  39. (get-natural-buffer-sizes default-get-natural-buffer-sizes)
  40. (id "custom-port")
  41. (print #f) ; TODO
  42. (truncate default-truncate)
  43. (encoding #f) ; TODO
  44. (conversion-strategy #f) ; TODO
  45. (close-on-gc? #f)) ; TODO
  46. ;; FIXME: We aren't calling get-natural-buffer-sizes with the port
  47. ;; object, because we have to know what the buffer sizes are
  48. ;; *before* we create the port.
  49. (define-values (read-buf-size write-buf-size)
  50. (get-natural-buffer-sizes #f 1024 1024))
  51. (make-port read
  52. write
  53. input-waiting?
  54. seek
  55. close
  56. truncate
  57. id
  58. #f ; file-name
  59. read-buf-size
  60. write-buf-size
  61. random-access?
  62. #f ; fold-case?
  63. #f))