fileio.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (library (lib fileio)
  2. (export get-document-from-file
  3. get-string-from-file
  4. get-lines-from-file
  5. put-lines-to-file
  6. put-string-to-file)
  7. (import
  8. (except (rnrs base) let-values)
  9. (only (guile) lambda* λ
  10. ;; io
  11. call-with-input-file
  12. call-with-output-file
  13. set-port-encoding!
  14. eof-object?
  15. ;; strings
  16. string-split
  17. string-join)
  18. (ice-9 textual-ports) ; for textual reading and writing procedures
  19. (ice-9 binary-ports) ; not sure if needed
  20. (ice-9 rdelim) ; for `eof-object?`
  21. (ice-9 optargs) ; for keyword arguments
  22. (srfi srfi-1)))
  23. (define* (get-document-from-file
  24. file-location
  25. content-processing-proc
  26. #:key (encoding "UTF-8"))
  27. "Use CONTENT-PROCESSING-PROC to read a document from the PORT, which the file content is read from. CONTENT-PROCESSING-PROC is called as follows: (CONTENT-PROCESSING-PROC PORT)."
  28. (call-with-input-file file-location
  29. (λ (port)
  30. (set-port-encoding! port encoding)
  31. (content-processing-proc port))))
  32. (define* (get-string-from-file 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* (get-lines-from-file file-path #:key (encoding "UTF-8"))
  38. ;; another common encoding is: "ISO-8859-1"
  39. ;; see http://www.iana.org/assignments/character-sets for more
  40. (define (get-lines-from-port port)
  41. (let ([line (get-line port)])
  42. (cond [(eof-object? line) '()]
  43. [else
  44. (cons line
  45. (get-lines-from-port port))])))
  46. (call-with-input-file file-path
  47. (lambda (port)
  48. (set-port-encoding! port encoding)
  49. (get-lines-from-port port))))
  50. (define* (get-lines-from-file-using-splitting file-path #:key (encoding "UTF-8"))
  51. (string-split (get-string-from-file file-path #:encoding encoding)
  52. ;; You could use simple character here, but I am using
  53. ;; lambda to show that it can be used as well.
  54. (lambda (char) (char=? char #\newline))))
  55. (define* (put-lines-to-file file-path lines #:key (encoding "UTF-8") (mode 'replace))
  56. (call-with-output-file file-path
  57. (λ (port)
  58. (set-port-encoding! port encoding)
  59. (put-string port
  60. (cond [(eq? mode 'append)
  61. (string-append (get-string-from-file
  62. file-path
  63. #:encoding encoding)
  64. "\n"
  65. (string-join lines "\n"))]
  66. [(equal? mode 'replace) (string-join lines "\n")]
  67. [else (string-join lines "\n")])))))
  68. (define* (put-string-to-file file-path string #:key (encoding "UTF-8") (mode 'replace))
  69. (call-with-output-file file-path
  70. (λ (port)
  71. (set-port-encoding! port encoding)
  72. (put-string port
  73. (cond [(eq? mode 'append)
  74. (string-append (get-string-from-file
  75. file-path
  76. #:encoding encoding)
  77. "\n"
  78. string)]
  79. [(equal? mode 'replace) string]
  80. [else string])))))