file-reader.scm 902 B

12345678910111213141516171819202122232425262728293031
  1. (library (file-reader)
  2. (export read-file-to-bytevector)
  3. (import
  4. (except (rnrs base) let-values)
  5. (only (guile) lambda* λ
  6. ;; ports
  7. call-with-input-file)
  8. ;; for bytevector operations
  9. (ice-9 binary-ports)
  10. (prefix (logging) log:)))
  11. (define read-file-to-bytevector
  12. (lambda* (file-location #:key (binary? #f))
  13. (cond
  14. ;; Internet knowledge: If the file is binary, it needs to be served
  15. ;; ISO-8859-1 encoded. Note: Perhaps we can use the existing guess-encoding
  16. ;; mechanism, instead of figuring things out ourselves?
  17. [binary?
  18. (call-with-input-file file-location
  19. get-bytevector-all
  20. #:guess-encoding #f
  21. #:encoding "ISO-8859-1"
  22. #:binary #t)]
  23. [else
  24. (call-with-input-file file-location
  25. get-bytevector-all
  26. #:guess-encoding #f
  27. #:encoding "utf-8"
  28. #:binary #f)])))