3 ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2006, 2010, 2013 Free Software Foundation, Inc.
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.
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.
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
21 ;;; This is the Scheme part of the module for delimited I/O. It's
22 ;;; similar to (scsh rdelim) but somewhat incompatible.
24 (define-module (ice-9 rdelim)
36 (%init-rdelim-builtins)
38 (define* (read-line! string #:optional (port current-input-port))
39 ;; corresponds to SCM_LINE_INCREMENTORS in libguile.
40 (define scm-line-incrementors "\n")
41 (let* ((rv (%read-delimited! scm-line-incrementors
47 (cond ((and (= nchars 0)
48 (eof-object? terminator))
53 (define* (read-delimited! delims buf #:optional
54 (port (current-input-port)) (handle-delim 'trim)
55 (start 0) (end (string-length buf)))
56 (let* ((rv (%read-delimited! delims
58 (not (eq? handle-delim 'peek))
64 (cond ((or (not terminator) ; buffer filled
65 (eof-object? terminator))
67 (if (eq? handle-delim 'split)
68 (cons terminator terminator)
70 (if (eq? handle-delim 'split)
71 (cons nchars terminator)
76 ((concat) (string-set! buf (+ nchars start) terminator)
78 ((split) (cons nchars terminator))
79 (else (error "unexpected handle-delim value: "
82 (define* (read-delimited delims #:optional (port (current-input-port))
84 (let loop ((substrings '())
86 (buf-size 100)) ; doubled each time through.
87 (let* ((buf (make-string buf-size))
88 (rv (%read-delimited! delims
90 (not (eq? handle-delim 'peek))
94 (new-total (+ total-chars nchars)))
98 (loop (cons (substring buf 0 nchars) substrings)
101 ((and (eof-object? terminator) (zero? new-total))
102 (if (eq? handle-delim 'split)
103 (cons terminator terminator)
107 (string-concatenate-reverse
108 (cons (substring buf 0 nchars) substrings))))
111 (if (eof-object? terminator)
113 (string-append joined (string terminator))))
115 ((split) (cons joined terminator))
116 (else (error "unexpected handle-delim value: "
117 handle-delim)))))))))
119 (define-syntax-rule (check-arg exp message arg ...)
121 (error message arg ...)))
124 (and (integer? n) (exact? n) (>= n 0)))
126 (define* (read-string! buf #:optional
127 (port (current-input-port))
128 (start 0) (end (string-length buf)))
129 "Read all of the characters out of PORT and write them to BUF.
130 Returns the number of characters read.
132 This function only reads out characters from PORT if it will be able to
133 write them to BUF. That is to say, if BUF is smaller than the number of
134 available characters, then BUF will be filled, and characters will be
136 (check-arg (string? buf) "not a string" buf)
137 (check-arg (index? start) "bad index" start)
138 (check-arg (index? end) "bad index" end)
139 (check-arg (<= start end) "start beyond end" start end)
140 (check-arg (<= end (string-length buf)) "end beyond string length" end)
143 (let ((c (read-char port)))
147 (string-set! buf n c)
151 (define* (read-string #:optional (port (current-input-port)) (count #f))
152 "Read all of the characters out of PORT and return them as a string.
153 If the COUNT argument is present, treat it as a limit to the number of
154 characters to read. By default, there is no limit."
155 (check-arg (or (not count) (index? count)) "bad count" count)
156 (let loop ((substrings '())
158 (buf-size 100)) ; doubled each time through.
159 (let* ((buf (make-string (if count
160 (min buf-size (- count total-chars))
162 (nchars (read-string! buf port))
163 (new-total (+ total-chars nchars)))
167 (loop (cons buf substrings) new-total (* buf-size 2)))
169 (string-concatenate-reverse
170 (cons (substring buf 0 nchars) substrings)))))))
172 ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
173 ;;; from PORT. The return value depends on the value of HANDLE-DELIM,
174 ;;; which may be one of the symbols `trim', `concat', `peek' and
175 ;;; `split'. If it is `trim' (the default), the trailing newline is
176 ;;; removed and the string is returned. If `concat', the string is
177 ;;; returned with the trailing newline intact. If `peek', the newline
178 ;;; is left in the input port buffer and the string is returned. If
179 ;;; `split', the newline is split from the string and read-line
180 ;;; returns a pair consisting of the truncated string and the newline.
182 (define* (read-line #:optional (port (current-input-port))
183 (handle-delim 'trim))
184 (let* ((line/delim (%read-line port))
185 (line (car line/delim))
186 (delim (cdr line/delim)))
190 ((concat) (if (and (string? line) (char? delim))
191 (string-append line (string delim))
193 ((peek) (if (char? delim)
194 (unread-char delim port))
197 (error "unexpected handle-delim value: " handle-delim)))))