rdelim.scm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2006, 2010, 2013 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. ;;;;
  18. ;;; This is the Scheme part of the module for delimited I/O. It's
  19. ;;; similar to (scsh rdelim) but somewhat incompatible.
  20. (define-module (ice-9 rdelim)
  21. #:export (read-line
  22. read-line!
  23. read-delimited
  24. read-delimited!
  25. read-string
  26. read-string!
  27. %read-delimited!
  28. %read-line
  29. write-line))
  30. (%init-rdelim-builtins)
  31. (define* (read-line! string #:optional (port current-input-port))
  32. ;; corresponds to SCM_LINE_INCREMENTORS in libguile.
  33. (define scm-line-incrementors "\n")
  34. (let* ((rv (%read-delimited! scm-line-incrementors
  35. string
  36. #t
  37. port))
  38. (terminator (car rv))
  39. (nchars (cdr rv)))
  40. (cond ((and (= nchars 0)
  41. (eof-object? terminator))
  42. terminator)
  43. ((not terminator) #f)
  44. (else nchars))))
  45. (define* (read-delimited! delims buf #:optional
  46. (port (current-input-port)) (handle-delim 'trim)
  47. (start 0) (end (string-length buf)))
  48. (let* ((rv (%read-delimited! delims
  49. buf
  50. (not (eq? handle-delim 'peek))
  51. port
  52. start
  53. end))
  54. (terminator (car rv))
  55. (nchars (cdr rv)))
  56. (cond ((or (not terminator) ; buffer filled
  57. (eof-object? terminator))
  58. (if (zero? nchars)
  59. (if (eq? handle-delim 'split)
  60. (cons terminator terminator)
  61. terminator)
  62. (if (eq? handle-delim 'split)
  63. (cons nchars terminator)
  64. nchars)))
  65. (else
  66. (case handle-delim
  67. ((trim peek) nchars)
  68. ((concat) (string-set! buf (+ nchars start) terminator)
  69. (+ nchars 1))
  70. ((split) (cons nchars terminator))
  71. (else (error "unexpected handle-delim value: "
  72. handle-delim)))))))
  73. (define* (read-delimited delims #:optional (port (current-input-port))
  74. (handle-delim 'trim))
  75. (let loop ((substrings '())
  76. (total-chars 0)
  77. (buf-size 100)) ; doubled each time through.
  78. (let* ((buf (make-string buf-size))
  79. (rv (%read-delimited! delims
  80. buf
  81. (not (eq? handle-delim 'peek))
  82. port))
  83. (terminator (car rv))
  84. (nchars (cdr rv))
  85. (new-total (+ total-chars nchars)))
  86. (cond
  87. ((not terminator)
  88. ;; buffer filled.
  89. (loop (cons (substring buf 0 nchars) substrings)
  90. new-total
  91. (* buf-size 2)))
  92. ((and (eof-object? terminator) (zero? new-total))
  93. (if (eq? handle-delim 'split)
  94. (cons terminator terminator)
  95. terminator))
  96. (else
  97. (let ((joined
  98. (string-concatenate-reverse
  99. (cons (substring buf 0 nchars) substrings))))
  100. (case handle-delim
  101. ((concat)
  102. (if (eof-object? terminator)
  103. joined
  104. (string-append joined (string terminator))))
  105. ((trim peek) joined)
  106. ((split) (cons joined terminator))
  107. (else (error "unexpected handle-delim value: "
  108. handle-delim)))))))))
  109. (define-syntax-rule (check-arg exp message arg ...)
  110. (unless exp
  111. (error message arg ...)))
  112. (define (index? n)
  113. (and (integer? n) (exact? n) (>= n 0)))
  114. (define* (read-string! buf #:optional
  115. (port (current-input-port))
  116. (start 0) (end (string-length buf)))
  117. "Read all of the characters out of PORT and write them to BUF.
  118. Returns the number of characters read.
  119. This function only reads out characters from PORT if it will be able to
  120. write them to BUF. That is to say, if BUF is smaller than the number of
  121. available characters, then BUF will be filled, and characters will be
  122. left in the port."
  123. (check-arg (string? buf) "not a string" buf)
  124. (check-arg (index? start) "bad index" start)
  125. (check-arg (index? end) "bad index" end)
  126. (check-arg (<= start end) "start beyond end" start end)
  127. (check-arg (<= end (string-length buf)) "end beyond string length" end)
  128. (let lp ((n start))
  129. (if (< n end)
  130. (let ((c (read-char port)))
  131. (if (eof-object? c)
  132. (- n start)
  133. (begin
  134. (string-set! buf n c)
  135. (lp (1+ n)))))
  136. (- n start))))
  137. (define* (read-string #:optional (port (current-input-port)) (count #f))
  138. "Read all of the characters out of PORT and return them as a string.
  139. If the COUNT argument is present, treat it as a limit to the number of
  140. characters to read. By default, there is no limit."
  141. (check-arg (or (not count) (index? count)) "bad count" count)
  142. (let loop ((substrings '())
  143. (total-chars 0)
  144. (buf-size 100)) ; doubled each time through.
  145. (let* ((buf (make-string (if count
  146. (min buf-size (- count total-chars))
  147. buf-size)))
  148. (nchars (read-string! buf port))
  149. (new-total (+ total-chars nchars)))
  150. (cond
  151. ((= nchars buf-size)
  152. ;; buffer filled.
  153. (loop (cons buf substrings) new-total (* buf-size 2)))
  154. (else
  155. (string-concatenate-reverse
  156. (cons (substring buf 0 nchars) substrings)))))))
  157. ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
  158. ;;; from PORT. The return value depends on the value of HANDLE-DELIM,
  159. ;;; which may be one of the symbols `trim', `concat', `peek' and
  160. ;;; `split'. If it is `trim' (the default), the trailing newline is
  161. ;;; removed and the string is returned. If `concat', the string is
  162. ;;; returned with the trailing newline intact. If `peek', the newline
  163. ;;; is left in the input port buffer and the string is returned. If
  164. ;;; `split', the newline is split from the string and read-line
  165. ;;; returns a pair consisting of the truncated string and the newline.
  166. (define* (read-line #:optional (port (current-input-port))
  167. (handle-delim 'trim))
  168. (let* ((line/delim (%read-line port))
  169. (line (car line/delim))
  170. (delim (cdr line/delim)))
  171. (case handle-delim
  172. ((trim) line)
  173. ((split) line/delim)
  174. ((concat) (if (and (string? line) (char? delim))
  175. (string-append line (string delim))
  176. line))
  177. ((peek) (if (char? delim)
  178. (unread-char delim port))
  179. line)
  180. (else
  181. (error "unexpected handle-delim value: " handle-delim)))))