file-reader.scm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. (library (file-reader)
  2. (export process-file
  3. get-string-from-file)
  4. (import
  5. (except (rnrs base) let-values)
  6. (only (guile)
  7. lambda* λ
  8. error
  9. display
  10. call-with-input-file
  11. set-port-encoding!)
  12. ;; Guile modules
  13. ;; alist->hash-table
  14. ;; (prefix (ice-9 hash-table) ice9-hash-table:)
  15. ;; Guile exception handling
  16. (ice-9 exceptions)
  17. ;; for bytevector operations
  18. (ice-9 binary-ports)
  19. (ice-9 textual-ports)
  20. (ice-9 rdelim) ; for the eof-object? binding
  21. ;; SRFIs
  22. ;; hash tables
  23. ;; (prefix (srfi srfi-69) srfi-69:)
  24. ;; ;; receive form
  25. ;; (prefix (srfi srfi-8) srfi-8:)
  26. ;; ;; let-values
  27. ;; (prefix (srfi srfi-11) srfi-11:)
  28. ;; ;; list utils
  29. ;; (prefix (srfi srfi-1) srfi-1:)
  30. (prefix (logging) log:)))
  31. (define get-string-from-file
  32. (lambda* (file-path #:key (encoding "UTF-8"))
  33. (call-with-input-file file-path
  34. (λ (port)
  35. (set-port-encoding! port encoding)
  36. (get-string-all port)))))
  37. (define process-file
  38. (λ (loc proc)
  39. ;; call-with-input-file automatically closes the port
  40. ;; when the given procedure returns.
  41. (call-with-input-file loc
  42. (λ (fport)
  43. (proc fport))
  44. #:guess-encoding #f
  45. #:binary #t)))
  46. ;; get-string-all
  47. ;; (call-with-output-file file-path
  48. ;; (lambda (port)
  49. ;; (rnrs:assert (list? scm-output))
  50. ;; (set-port-encoding! port encoding)
  51. ;; (scm->dsv (stringify* scm-output)
  52. ;; port
  53. ;; delimiter
  54. ;; #:format format)))
  55. ;; flushing the buffers of ports:
  56. ;; force-output
  57. ;; close-port
  58. ;; setvbuf port mode [size]
  59. ;; mode: 'none 'line 'block (with size)