Next: Programmatic ports, Up: I/O system
While channels provide the low-level interface directly to the OS's I/O facilities, ports provide a more abstract & generalized mechanism for I/O transmission. Rather than being specific to channels or being themselves primitive I/O devices, ports are functionally parameterized. This section describes the usual I/O operations on ports. The next section describes the programmatic port parameterization mechanism, and the section following that describes the most commonly used built-in port abstraction, ports atop channels.
The following names are exported by the i/o structure.
These return
#tif their argument is both a port and either an input port or output port, respectively, or#fif neither condition is true.
Closes port, which must be an input port or an output port, respectively.
Char-ready?returns a true value if there is a character ready to be read from port and#fif there is no character ready. Port defaults to the current input port if absent; see below on current ports.Output-port-ready?returns a true value if port is ready to receive a single written character and#fif not.
Read-blockattempts to read count elements from port into block, which may be a string or a byte vector, starting at start. If fewer than count characters or bytes are available to read from port, and wait? is a true value or absent,read-blockwill wait until count characters are available and read into block; if wait? is#f,read-blockimmediately returns.Read-blockreturns the number of elements read into block, or an end of file object if the stream's end is immediately encountered.Write-blockwrites count elements from block, which may be a string or a byte vector, starting at start to port.Write-stringis a convenience atopwrite-blockfor writing the entirety of a string to a port.
Writes a newline character or character sequence to the output port port. Port defaults to the current output port; see below on current ports.
Returns a disclosed representation of port; see Writer.
Forces all buffered output in the output port port to be sent.
Returns an output port that will ignore any output it receives.
Scheme48 keeps in its dynamic environment a set of `current' ports. These include R5RS's current
input and output ports, as well as ports for general noise produced by
the system, and ports for where error messages are printed. These
procedures are exported by the i/o structure.
These return the values in the current dynamic environment of the respective ports.
Current-input-portandcurrent-output-portare also exported by theschemestructure.
These are utilities for retrieving optional input and output port arguments from rest argument lists, defaulting to the current input or output ports. For example, assuming the newline character sequence is simply
#\newline,newlinemight be written as:(define (newline . maybe-port) (write-char #\newline (output-port-option maybe-port)))
This stifles output from the current noise port in the dynamic extent of thunk, which is applied to zero arguments.
Silentlyreturns the values that thunk returns.
With-current-portsdynamically binds the current input, output, and error ports to input, output, and error, respectively, in the dynamic extent of thunk, which is applied to zero arguments. The current noise port is also bound to error.With-current-portsreturns the values that thunk returns.
Similarly to with-current-ports, the i/o-internal
structure also exports these procedures:
These bind individual current ports for the dynamic extent of each thunk, which is applied to zero arguments. These all return the values that thunk returns.