rdelim.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2006, 2010, 2013,
  3. ;;;; 2014 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;; This is the Scheme part of the module for delimited I/O. It's
  20. ;;; similar to (scsh rdelim) but somewhat incompatible.
  21. (define-module (ice-9 rdelim)
  22. #:export (read-line
  23. read-line!
  24. read-delimited
  25. read-delimited!
  26. read-string
  27. read-string!
  28. %read-delimited!
  29. %read-line
  30. write-line))
  31. (%init-rdelim-builtins)
  32. (define* (read-line! string #:optional (port current-input-port))
  33. ;; corresponds to SCM_LINE_INCREMENTORS in libguile.
  34. (define scm-line-incrementors "\n")
  35. (let* ((rv (%read-delimited! scm-line-incrementors
  36. string
  37. #t
  38. port))
  39. (terminator (car rv))
  40. (nchars (cdr rv)))
  41. (cond ((and (= nchars 0)
  42. (eof-object? terminator))
  43. terminator)
  44. ((not terminator) #f)
  45. (else nchars))))
  46. (define* (read-delimited! delims buf #:optional
  47. (port (current-input-port)) (handle-delim 'trim)
  48. (start 0) (end (string-length buf)))
  49. (let* ((rv (%read-delimited! delims
  50. buf
  51. (not (eq? handle-delim 'peek))
  52. port
  53. start
  54. end))
  55. (terminator (car rv))
  56. (nchars (cdr rv)))
  57. (cond ((or (not terminator) ; buffer filled
  58. (eof-object? terminator))
  59. (if (zero? nchars)
  60. (if (eq? handle-delim 'split)
  61. (cons terminator terminator)
  62. terminator)
  63. (if (eq? handle-delim 'split)
  64. (cons nchars terminator)
  65. nchars)))
  66. (else
  67. (case handle-delim
  68. ((trim peek) nchars)
  69. ((concat) (string-set! buf (+ nchars start) terminator)
  70. (+ nchars 1))
  71. ((split) (cons nchars terminator))
  72. (else (error "unexpected handle-delim value: "
  73. handle-delim)))))))
  74. (define* (read-delimited delims #:optional (port (current-input-port))
  75. (handle-delim 'trim))
  76. (let loop ((substrings '())
  77. (total-chars 0)
  78. (buf-size 100)) ; doubled each time through.
  79. (let* ((buf (make-string buf-size))
  80. (rv (%read-delimited! delims
  81. buf
  82. (not (eq? handle-delim 'peek))
  83. port))
  84. (terminator (car rv))
  85. (nchars (cdr rv))
  86. (new-total (+ total-chars nchars)))
  87. (cond
  88. ((not terminator)
  89. ;; buffer filled.
  90. (loop (cons (substring buf 0 nchars) substrings)
  91. new-total
  92. (* buf-size 2)))
  93. ((and (eof-object? terminator) (zero? new-total))
  94. (if (eq? handle-delim 'split)
  95. (cons terminator terminator)
  96. terminator))
  97. (else
  98. (let ((joined
  99. (string-concatenate-reverse
  100. (cons (substring buf 0 nchars) substrings))))
  101. (case handle-delim
  102. ((concat)
  103. (if (eof-object? terminator)
  104. joined
  105. (string-append joined (string terminator))))
  106. ((trim peek) joined)
  107. ((split) (cons joined terminator))
  108. (else (error "unexpected handle-delim value: "
  109. handle-delim)))))))))
  110. (define-syntax-rule (check-arg exp message arg ...)
  111. (unless exp
  112. (error message arg ...)))
  113. (define (index? n)
  114. (and (integer? n) (exact? n) (>= n 0)))
  115. (define* (read-string! buf #:optional
  116. (port (current-input-port))
  117. (start 0) (end (string-length buf)))
  118. "Read all of the characters out of PORT and write them to BUF.
  119. Returns the number of characters read.
  120. This function only reads out characters from PORT if it will be able to
  121. write them to BUF. That is to say, if BUF is smaller than the number of
  122. available characters, then BUF will be filled, and characters will be
  123. left in the port."
  124. (check-arg (string? buf) "not a string" buf)
  125. (check-arg (index? start) "bad index" start)
  126. (check-arg (index? end) "bad index" end)
  127. (check-arg (<= start end) "start beyond end" start end)
  128. (check-arg (<= end (string-length buf)) "end beyond string length" end)
  129. (let lp ((n start))
  130. (if (< n end)
  131. (let ((c (read-char port)))
  132. (if (eof-object? c)
  133. (- n start)
  134. (begin
  135. (string-set! buf n c)
  136. (lp (1+ n)))))
  137. (- n start))))
  138. (define* read-string
  139. (case-lambda*
  140. "Read all of the characters out of PORT and return them as a string.
  141. If the COUNT argument is present, treat it as a limit to the number of
  142. characters to read. By default, there is no limit."
  143. ((#:optional (port (current-input-port)))
  144. ;; Fast path.
  145. (let loop ((head (make-string 30)) (pos 0) (tail '()))
  146. (let ((char (read-char port)))
  147. (cond
  148. ((eof-object? char)
  149. (let ((head (substring head 0 pos)))
  150. (if (null? tail)
  151. (substring head 0 pos)
  152. (string-concatenate-reverse tail head pos))))
  153. (else
  154. (string-set! head pos char)
  155. (if (< (1+ pos) (string-length head))
  156. (loop head (1+ pos) tail)
  157. (loop (make-string (* (string-length head) 2)) 0
  158. (cons head tail))))))))
  159. ((port count)
  160. ;; Slower path.
  161. (let loop ((chars '())
  162. (total 0))
  163. (let ((char (read-char port)))
  164. (if (or (eof-object? char) (>= total count))
  165. (list->string (reverse chars))
  166. (loop (cons char chars) (+ 1 total))))))))
  167. ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
  168. ;;; from PORT. The return value depends on the value of HANDLE-DELIM,
  169. ;;; which may be one of the symbols `trim', `concat', `peek' and
  170. ;;; `split'. If it is `trim' (the default), the trailing newline is
  171. ;;; removed and the string is returned. If `concat', the string is
  172. ;;; returned with the trailing newline intact. If `peek', the newline
  173. ;;; is left in the input port buffer and the string is returned. If
  174. ;;; `split', the newline is split from the string and read-line
  175. ;;; returns a pair consisting of the truncated string and the newline.
  176. (define* (read-line #:optional (port (current-input-port))
  177. (handle-delim 'trim))
  178. (let* ((line/delim (%read-line port))
  179. (line (car line/delim))
  180. (delim (cdr line/delim)))
  181. (case handle-delim
  182. ((trim) line)
  183. ((split) line/delim)
  184. ((concat) (if (and (string? line) (char? delim))
  185. (string-append line (string delim))
  186. line))
  187. ((peek) (if (char? delim)
  188. (unread-char delim port))
  189. line)
  190. (else
  191. (error "unexpected handle-delim value: " handle-delim)))))