Next: , Previous: Constructors and Accessors for Ports, Up: Port Primitives

14.9.3 Input Port Operations

This section describes the standard operations on input ports. Following that, some useful custom operations are described.

— operation on input port: read-char input-port

Removes the next character available from input-port and returns it. If input-port has no more characters and will never have any (e.g. at the end of an input file), this operation returns an end-of-file object. If input-port has no more characters but will eventually have some more (e.g. a terminal where nothing has been typed recently), and it is in non-blocking mode, #f is returned; otherwise the operation hangs until input is available.

— operation on input port: peek-char input-port

Reads the next character available from input-port and returns it. The character is not removed from input-port, and a subsequent attempt to read from the port will get that character again. In other respects this operation behaves like read-char.

— operation on input port: char-ready? input-port k

char-ready? returns #t if at least one character is available to be read from input-port. If no characters are available, the operation waits up to k milliseconds before returning #f, returning immediately if any characters become available while it is waiting.

— operation on input port: read-string input-port char-set
— operation on input port: discard-chars input-port char-set

These operations are like read-char, except that they read or discard multiple characters at once. All characters up to, but excluding, the first character in char-set (or end of file) are read from input-port. read-string returns these characters as a newly allocated string, while discard-chars discards them and returns an unspecified value. These operations hang until sufficient input is available, even if input-port is in non-blocking mode. If end of file is encountered before any input characters, read-string returns an end-of-file object.

— operation on input port: read-substring input-port string start end

Reads characters from input-port into the substring defined by string, start, and end until either the substring has been filled or there are no more characters available. Returns the number of characters written to the substring.

If input-port is an interactive port, and at least one character is immediately available, the available characters are written to the substring and this operation returns immediately. If no characters are available, and input-port is in blocking mode, the operation blocks until at least one character is available. Otherwise, the operation returns #f immediately.

This is an extremely fast way to read characters from a port.

— procedure: input-port/read-char input-port
— procedure: input-port/peek-char input-port
— procedure: input-port/char-ready? input-port k
— procedure: input-port/read-string input-port char-set
— procedure: input-port/discard-chars input-port char-set
— procedure: input-port/read-substring input-port string start end

Each of these procedures invokes the respective operation on input-port. For example, the following are equivalent:

          (input-port/read-char input-port)
          ((port/operation input-port 'read-char) input-port)

The following custom operations are implemented for input ports to files, and will also work with some other kinds of input ports:

— operation on input port: eof? input-port

Returns #t if input-port is known to be at end of file, otherwise it returns #f.

— operation on input port: chars-remaining input-port

Returns an estimate of the number of characters remaining to be read from input-port. This is useful only when input-port is a file port in binary mode; in other cases, it returns #f.

— operation on input port: buffered-input-chars input-port

Returns the number of unread characters that are stored in input-port's buffer. This will always be less than or equal to the buffer's size.

— operation on input port: input-buffer-size input-port

Returns the maximum number of characters that input-port's buffer can hold.

— operation on input port: set-input-buffer-size input-port size

Resizes input-port's buffer so that it can hold at most size characters. Characters in the buffer are discarded. Size must be an exact non-negative integer.