1
0
Fork 0
cl-sites/guile.html_node/Ports.html
2024-12-17 12:49:28 +01:00

176 lines
11 KiB
HTML

<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>Ports (Guile Reference Manual)</title>
<meta name="description" content="Ports (Guile Reference Manual)">
<meta name="keywords" content="Ports (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Input-and-Output.html" rel="up" title="Input and Output">
<link href="Binary-I_002fO.html" rel="next" title="Binary I/O">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Ports">
<div class="nav-panel">
<p>
Next: <a href="Binary-I_002fO.html" accesskey="n" rel="next">Binary I/O</a>, Up: <a href="Input-and-Output.html" accesskey="u" rel="up">Input and Output</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Ports-1"><span>6.12.1 Ports<a class="copiable-link" href="#Ports-1"> &para;</a></span></h4>
<a class="index-entry-id" id="index-Port"></a>
<p>Ports are the way that Guile performs input and output. Guile can read
in characters or bytes from an <em class="dfn">input port</em>, or write them out to an
<em class="dfn">output port</em>. Some ports support both interfaces.
</p>
<p>There are a number of different port types implemented in Guile. File
ports provide input and output over files, as you might imagine. For
example, we might display a string to a file like this:
</p>
<div class="example">
<pre class="example-preformatted">(let ((port (open-output-file &quot;foo.txt&quot;)))
(display &quot;Hello, world!\n&quot; port)
(close-port port))
</pre></div>
<p>There are also string ports, for taking input from a string, or
collecting output to a string; bytevector ports, for doing the same but
using a bytevector as a source or sink of data; and custom ports, for
arranging to call Scheme functions to provide input or handle output.
See <a class="xref" href="Port-Types.html">Types of Port</a>.
</p>
<p>Ports should be <em class="dfn">closed</em> when they are not needed by calling
<code class="code">close-port</code> on them, as in the example above. This will make sure
that any pending output is successfully written out to disk, in the case
of a file port, or otherwise to whatever mutable store is backed by the
port. Any error that occurs while writing out that buffered data would
also be raised promptly at the <code class="code">close-port</code>, and not later when the
port is closed by the garbage collector. See <a class="xref" href="Buffering.html">Buffering</a>, for more on
buffered output.
</p>
<p>Closing a port also releases any precious resource the file might have.
Usually in Scheme a programmer doesn&rsquo;t have to clean up after their data
structures (see <a class="pxref" href="Memory-Management.html">Memory Management and Garbage Collection</a>), but most systems have strict
limits on how many files can be open, both on a per-process and a
system-wide basis. A program that uses many files should take care not
to hit those limits. The same applies to similar system resources such
as pipes and sockets.
</p>
<p>Indeed for these reasons the above example is not the most idiomatic way
to use ports. It is more common to acquire ports via procedures like
<code class="code">call-with-output-file</code>, which handle the <code class="code">close-port</code>
automatically:
</p>
<div class="example">
<pre class="example-preformatted">(call-with-output-file &quot;foo.txt&quot;
(lambda (port)
(display &quot;Hello, world!\n&quot; port)))
</pre></div>
<p>Finally, all ports have associated input and output buffers, as
appropriate. Buffering is a common strategy to limit the overhead of
small reads and writes: without buffering, each character fetched from a
file would involve at least one call into the kernel, and maybe more
depending on the character and the encoding. Instead, Guile will batch
reads and writes into internal buffers. However, sometimes you want to
make output on a port show up immediately. See <a class="xref" href="Buffering.html">Buffering</a>, for more
on interfaces to control port buffering.
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-port_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">port?</strong> <var class="def-var-arguments">x</var><a class="copiable-link" href="#index-port_003f"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005fport_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_port_p</strong> <var class="def-var-arguments">(x)</var><a class="copiable-link" href="#index-scm_005fport_005fp"> &para;</a></span></dt>
<dd><p>Return a boolean indicating whether <var class="var">x</var> is a port.
Equivalent to <code class="code">(or (input-port? <var class="var">x</var>) (output-port? <var class="var">x</var>))</code>.
</p></dd></dl>
<a class="index-entry-id" id="index-input_002dport_003f-3"></a>
<dl class="first-deffn">
<dt class="deffn" id="index-input_002dport_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">input-port?</strong> <var class="def-var-arguments">x</var><a class="copiable-link" href="#index-input_002dport_003f"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005finput_005fport_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_input_port_p</strong> <var class="def-var-arguments">(x)</var><a class="copiable-link" href="#index-scm_005finput_005fport_005fp"> &para;</a></span></dt>
<dd><p>Return <code class="code">#t</code> if <var class="var">x</var> is an input port, otherwise return
<code class="code">#f</code>. Any object satisfying this predicate also satisfies
<code class="code">port?</code>.
</p></dd></dl>
<a class="index-entry-id" id="index-output_002dport_003f-3"></a>
<dl class="first-deffn">
<dt class="deffn" id="index-output_002dport_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">output-port?</strong> <var class="def-var-arguments">x</var><a class="copiable-link" href="#index-output_002dport_003f"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005foutput_005fport_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_output_port_p</strong> <var class="def-var-arguments">(x)</var><a class="copiable-link" href="#index-scm_005foutput_005fport_005fp"> &para;</a></span></dt>
<dd><p>Return <code class="code">#t</code> if <var class="var">x</var> is an output port, otherwise return
<code class="code">#f</code>. Any object satisfying this predicate also satisfies
<code class="code">port?</code>.
</p></dd></dl>
<a class="index-entry-id" id="index-Closing-ports"></a>
<a class="index-entry-id" id="index-Port_002c-close"></a>
<dl class="first-deffn">
<dt class="deffn" id="index-close_002dport"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">close-port</strong> <var class="def-var-arguments">port</var><a class="copiable-link" href="#index-close_002dport"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005fclose_005fport"><span class="category-def">C Function: </span><span><strong class="def-name">scm_close_port</strong> <var class="def-var-arguments">(port)</var><a class="copiable-link" href="#index-scm_005fclose_005fport"> &para;</a></span></dt>
<dd><p>Close the specified port object. Return <code class="code">#t</code> if it successfully
closes a port or <code class="code">#f</code> if it was already closed. An exception may
be raised if an error occurs, for example when flushing buffered output.
See <a class="xref" href="Buffering.html">Buffering</a>, for more on buffered output. See <a class="xref" href="Ports-and-File-Descriptors.html">close</a>, for a procedure which can close file descriptors.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-port_002dclosed_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">port-closed?</strong> <var class="def-var-arguments">port</var><a class="copiable-link" href="#index-port_002dclosed_003f"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005fport_005fclosed_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_port_closed_p</strong> <var class="def-var-arguments">(port)</var><a class="copiable-link" href="#index-scm_005fport_005fclosed_005fp"> &para;</a></span></dt>
<dd><p>Return <code class="code">#t</code> if <var class="var">port</var> is closed or <code class="code">#f</code> if it is
open.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-call_002dwith_002dport"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">call-with-port</strong> <var class="def-var-arguments">port proc</var><a class="copiable-link" href="#index-call_002dwith_002dport"> &para;</a></span></dt>
<dd><p>Call <var class="var">proc</var>, passing it <var class="var">port</var> and closing <var class="var">port</var> upon exit
of <var class="var">proc</var>. Return the return values of <var class="var">proc</var>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Binary-I_002fO.html">Binary I/O</a>, Up: <a href="Input-and-Output.html">Input and Output</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>