3 ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2006, 2010, 2013,
4 ;;;; 2014 Free Software Foundation, Inc.
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 ;;; This is the Scheme part of the module for delimited I/O. It's
23 ;;; similar to (scsh rdelim) but somewhat incompatible.
25 (define-module (ice-9 rdelim)
37 (%init-rdelim-builtins)
39 (define* (read-line! string #:optional (port current-input-port))
40 ;; corresponds to SCM_LINE_INCREMENTORS in libguile.
41 (define scm-line-incrementors "\n")
42 (let* ((rv (%read-delimited! scm-line-incrementors
48 (cond ((and (= nchars 0)
49 (eof-object? terminator))
54 (define* (read-delimited! delims buf #:optional
55 (port (current-input-port)) (handle-delim 'trim)
56 (start 0) (end (string-length buf)))
57 (let* ((rv (%read-delimited! delims
59 (not (eq? handle-delim 'peek))
65 (cond ((or (not terminator) ; buffer filled
66 (eof-object? terminator))
68 (if (eq? handle-delim 'split)
69 (cons terminator terminator)
71 (if (eq? handle-delim 'split)
72 (cons nchars terminator)
77 ((concat) (string-set! buf (+ nchars start) terminator)
79 ((split) (cons nchars terminator))
80 (else (error "unexpected handle-delim value: "
83 (define* (read-delimited delims #:optional (port (current-input-port))
85 (let loop ((substrings '())
87 (buf-size 100)) ; doubled each time through.
88 (let* ((buf (make-string buf-size))
89 (rv (%read-delimited! delims
91 (not (eq? handle-delim 'peek))
95 (new-total (+ total-chars nchars)))
99 (loop (cons (substring buf 0 nchars) substrings)
102 ((and (eof-object? terminator) (zero? new-total))
103 (if (eq? handle-delim 'split)
104 (cons terminator terminator)
108 (string-concatenate-reverse
109 (cons (substring buf 0 nchars) substrings))))
112 (if (eof-object? terminator)
114 (string-append joined (string terminator))))
116 ((split) (cons joined terminator))
117 (else (error "unexpected handle-delim value: "
118 handle-delim)))))))))
120 (define-syntax-rule (check-arg exp message arg ...)
122 (error message arg ...)))
125 (and (integer? n) (exact? n) (>= n 0)))
127 (define* (read-string! buf #:optional
128 (port (current-input-port))
129 (start 0) (end (string-length buf)))
130 "Read all of the characters out of PORT and write them to BUF.
131 Returns the number of characters read.
133 This function only reads out characters from PORT if it will be able to
134 write them to BUF. That is to say, if BUF is smaller than the number of
135 available characters, then BUF will be filled, and characters will be
137 (check-arg (string? buf) "not a string" buf)
138 (check-arg (index? start) "bad index" start)
139 (check-arg (index? end) "bad index" end)
140 (check-arg (<= start end) "start beyond end" start end)
141 (check-arg (<= end (string-length buf)) "end beyond string length" end)
144 (let ((c (read-char port)))
148 (string-set! buf n c)
154 "Read all of the characters out of PORT and return them as a string.
155 If the COUNT argument is present, treat it as a limit to the number of
156 characters to read. By default, there is no limit."
157 ((#:optional (port (current-input-port)))
159 ;; This creates more garbage than using 'string-set!' as in
160 ;; 'read-string!', but currently that is faster nonetheless.
161 (let loop ((chars '()))
162 (let ((char (read-char port)))
163 (if (eof-object? char)
164 (list->string (reverse! chars))
165 (loop (cons char chars))))))
168 (let loop ((chars '())
170 (let ((char (read-char port)))
171 (if (or (eof-object? char) (>= total count))
172 (list->string (reverse chars))
173 (loop (cons char chars) (+ 1 total))))))))
176 ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
177 ;;; from PORT. The return value depends on the value of HANDLE-DELIM,
178 ;;; which may be one of the symbols `trim', `concat', `peek' and
179 ;;; `split'. If it is `trim' (the default), the trailing newline is
180 ;;; removed and the string is returned. If `concat', the string is
181 ;;; returned with the trailing newline intact. If `peek', the newline
182 ;;; is left in the input port buffer and the string is returned. If
183 ;;; `split', the newline is split from the string and read-line
184 ;;; returns a pair consisting of the truncated string and the newline.
186 (define* (read-line #:optional (port (current-input-port))
187 (handle-delim 'trim))
188 (let* ((line/delim (%read-line port))
189 (line (car line/delim))
190 (delim (cdr line/delim)))
194 ((concat) (if (and (string? line) (char? delim))
195 (string-append line (string delim))
197 ((peek) (if (char? delim)
198 (unread-char delim port))
201 (error "unexpected handle-delim value: " handle-delim)))))