fileio.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. (library (fileio)
  2. (export get-string-from-file
  3. get-lines-from-file
  4. get-lines-from-port
  5. get-lines-from-file-using-splitting
  6. put-lines-to-file
  7. put-string-to-file)
  8. (import
  9. (except (rnrs base)
  10. let-values
  11. map)
  12. (only (guile)
  13. lambda* λ
  14. ;; ports and files
  15. call-with-input-file
  16. call-with-output-file
  17. set-port-encoding!
  18. eof-object?
  19. ;; string
  20. string-split
  21. string-join)
  22. (ice-9 textual-ports) ; for textual reading and writing procedures
  23. (ice-9 binary-ports) ; not sure if needed
  24. (ice-9 rdelim) ; for `eof-object?`
  25. (ice-9 optargs) ; for keyword arguments
  26. ;; not sure if needed
  27. (srfi srfi-1))
  28. (define get-string-from-file
  29. (lambda* (file-path
  30. #:key
  31. (encoding "UTF-8"))
  32. (call-with-input-file file-path
  33. (λ (port)
  34. (set-port-encoding! port encoding)
  35. (get-string-all port)))))
  36. (define get-lines-from-file
  37. (lambda* (file-path
  38. #:key
  39. (encoding "UTF-8"))
  40. ;; another common encoding is: "ISO-8859-1"
  41. ;; see http://www.iana.org/assignments/character-sets for more
  42. (define (get-lines-from-port port)
  43. (let ([line (get-line port)])
  44. (cond [(eof-object? line) '()]
  45. [else
  46. (cons line
  47. (get-lines-from-port port))])))
  48. (call-with-input-file file-path
  49. (λ (port)
  50. (set-port-encoding! port encoding)
  51. (get-lines-from-port port)))))
  52. (define get-lines-from-file-using-splitting
  53. (lambda* (file-path
  54. #:key
  55. (encoding "UTF-8"))
  56. (string-split (get-string-from-file file-path #:encoding encoding)
  57. ;; You could use simple character here, but I am using
  58. ;; lambda to show that it can be used as well.
  59. (λ (char) (char=? char #\newline)))))
  60. (define put-lines-to-file
  61. (lambda* (file-path
  62. lines
  63. #:key
  64. (encoding "UTF-8")
  65. (mode 'replace))
  66. (call-with-output-file file-path
  67. (λ (port)
  68. (set-port-encoding! port encoding)
  69. (put-string port
  70. (cond
  71. [(eq? mode 'append)
  72. (string-append (get-string-from-file
  73. file-path
  74. #:encoding encoding)
  75. "\n"
  76. (string-join lines "\n"))]
  77. [(equal? mode 'replace) (string-join lines "\n")]
  78. [else (string-join lines "\n")]))))))
  79. (define put-string-to-file
  80. (lambda* (file-path
  81. string
  82. #:key
  83. (encoding "UTF-8")
  84. (mode 'replace))
  85. (call-with-output-file file-path
  86. (λ (port)
  87. (set-port-encoding! port encoding)
  88. (put-string port
  89. (cond
  90. [(eq? mode 'append)
  91. (string-append (get-string-from-file
  92. file-path
  93. #:encoding encoding)
  94. "\n"
  95. string)]
  96. [(equal? mode 'replace) string]
  97. [else string])))))))