simple.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ;;; simple.scm --- The R6RS simple I/O library
  2. ;; Copyright (C) 2010, 2011, 2014 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (library (rnrs io simple (6))
  18. (export eof-object
  19. eof-object?
  20. call-with-input-file
  21. call-with-output-file
  22. input-port?
  23. output-port?
  24. current-input-port
  25. current-output-port
  26. current-error-port
  27. with-input-from-file
  28. with-output-to-file
  29. open-input-file
  30. open-output-file
  31. close-input-port
  32. close-output-port
  33. read-char
  34. peek-char
  35. read
  36. write-char
  37. newline
  38. display
  39. write
  40. &i/o make-i/o-error i/o-error?
  41. &i/o-read make-i/o-read-error i/o-read-error?
  42. &i/o-write make-i/o-write-error i/o-write-error?
  43. &i/o-invalid-position
  44. make-i/o-invalid-position-error
  45. i/o-invalid-position-error?
  46. i/o-error-position
  47. &i/o-filename
  48. make-i/o-filename-error
  49. i/o-filename-error?
  50. i/o-error-filename
  51. &i/o-file-protection
  52. make-i/o-file-protection-error
  53. i/o-file-protection-error?
  54. &i/o-file-is-read-only
  55. make-i/o-file-is-read-only-error
  56. i/o-file-is-read-only-error?
  57. &i/o-file-already-exists
  58. make-i/o-file-already-exists-error
  59. i/o-file-already-exists-error?
  60. &i/o-file-does-not-exist
  61. make-i/o-file-does-not-exist-error
  62. i/o-file-does-not-exist-error?
  63. &i/o-port
  64. make-i/o-port-error
  65. i/o-port-error?
  66. i/o-error-port)
  67. (import (only (rnrs io ports)
  68. call-with-port
  69. close-port
  70. open-file-input-port
  71. open-file-output-port
  72. eof-object
  73. eof-object?
  74. file-options
  75. buffer-mode
  76. native-transcoder
  77. get-char
  78. lookahead-char
  79. get-datum
  80. put-char
  81. put-datum
  82. input-port?
  83. output-port?)
  84. (only (guile)
  85. @@
  86. current-input-port
  87. current-output-port
  88. current-error-port
  89. define*
  90. with-input-from-port
  91. with-output-to-port)
  92. (rnrs base (6))
  93. (rnrs files (6)) ;for the condition types
  94. )
  95. (define display (@@ (rnrs io ports) display))
  96. (define (call-with-input-file filename proc)
  97. (call-with-port (open-file-input-port filename) proc))
  98. (define (call-with-output-file filename proc)
  99. (call-with-port (open-file-output-port filename) proc))
  100. (define (with-input-from-file filename thunk)
  101. (call-with-input-file filename
  102. (lambda (port) (with-input-from-port port thunk))))
  103. (define (with-output-to-file filename thunk)
  104. (call-with-output-file filename
  105. (lambda (port) (with-output-to-port port thunk))))
  106. (define (open-input-file filename)
  107. (open-file-input-port filename
  108. (file-options)
  109. (buffer-mode block)
  110. (native-transcoder)))
  111. (define (open-output-file filename)
  112. (open-file-output-port filename
  113. (file-options)
  114. (buffer-mode block)
  115. (native-transcoder)))
  116. (define close-input-port close-port)
  117. (define close-output-port close-port)
  118. (define* (read-char #:optional (port (current-input-port)))
  119. (get-char port))
  120. (define* (peek-char #:optional (port (current-input-port)))
  121. (lookahead-char port))
  122. (define* (read #:optional (port (current-input-port)))
  123. (get-datum port))
  124. (define* (write-char char #:optional (port (current-output-port)))
  125. (put-char port char))
  126. (define* (newline #:optional (port (current-output-port)))
  127. (put-char port #\newline))
  128. (define* (write object #:optional (port (current-output-port)))
  129. (put-datum port object))
  130. )