ocaml-containers/2.3/containers/CCIO/index.html
2018-08-06 11:54:50 -05:00

23 lines
No EOL
12 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>CCIO (containers.CCIO)</title><link rel="stylesheet" href="../../odoc.css"/><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><meta name="generator" content="doc-ock-html v1.0.0-1-g1fc9bf0"/></head><body><nav id="top"><a href="../index.html">Up</a> &mdash; <span class="package">package <a href="../index.html">containers</a></span></nav><header><h1><span class="keyword">Module</span> <span class="module-path">CCIO</span></h1></header><h2>IO Utils</h2><p>Simple utilities to deal with basic Input/Output tasks in a resource-safe
way. For advanced IO tasks, the user is advised to use something
like Lwt or Async, that are far more comprehensive.</p><p>Examples:</p><ul><li>obtain the list of lines of a file:</li></ul><pre><code class="code"> # let l = CCIO.(with_in &quot;/tmp/some_file&quot; read_lines);;</code></pre><ul><li>transfer one file into another:</li></ul><pre><code class="code"> # CCIO.(
with_in &quot;/tmp/input&quot;
(fun ic -&gt;
let chunks = read_chunks ic in
with_out ~flags:[Open_binary] ~mode:0o644 &quot;/tmp/output&quot;
(fun oc -&gt;
write_gen oc chunks
)
)
) ;;</code></pre><ul class="at-tag"><li><span class="at-tag since">Since</span>: 0.6</li><li><span class="at-tag before">Before</span> 0.12.was in 'containers.io', now moved into 'containers'</li></ul><div class="spec type" id="type-or_error"><a href="#type-or_error" class="anchor"></a><div class="def type"><code><span class="keyword">type </span>'a or_error</code><code><span class="keyword"> = </span>(<span class="type-var">'a</span>, string) Result.result</code><code></code></div><div class="doc"></div></div><div class="spec type" id="type-gen"><a href="#type-gen" class="anchor"></a><div class="def type"><code><span class="keyword">type </span>'a gen</code><code><span class="keyword"> = </span>unit <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> option</code><code></code></div><div class="doc"><p>See Gen in the gen library.</p></div></div><h3>Input</h3><div class="spec val" id="val-with_in"><a href="#val-with_in" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>with_in : ?&#8288;mode:int <span class="keyword">&#8209;&gt;</span> ?&#8288;flags:Pervasives.open_flag list <span class="keyword">&#8209;&gt;</span> string <span class="keyword">&#8209;&gt;</span> (Pervasives.in_channel <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span>) <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span></code></div><div class="doc"><p>Open an input file with the given optional flag list, calls the function
on the input channel. When the function raises or returns, the
channel is closed.</p><ul class="at-tag"><li><span class="at-tag raise">Raises</span> <span class="module-path">Sys_error</span>: in case of error (same as open_in and close_in).</li><li><span class="at-tag parameter">Parameter</span> <span class="module-path">flags</span>: opening flags (default <code class="code">[Open_text]</code>). <code class="code">Open_rdonly</code> is used in any cases.</li></ul></div></div><div class="spec val" id="val-read_chunks"><a href="#val-read_chunks" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>read_chunks : ?&#8288;size:int <span class="keyword">&#8209;&gt;</span> Pervasives.in_channel <span class="keyword">&#8209;&gt;</span> string <a href="index.html#type-gen">gen</a></code></div><div class="doc"><p>Read the channel's content into chunks of size <code class="code">size</code>.</p></div></div><div class="spec val" id="val-read_line"><a href="#val-read_line" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>read_line : Pervasives.in_channel <span class="keyword">&#8209;&gt;</span> string option</code></div><div class="doc"><p>Read a line from the channel. Returns <code class="code">None</code> if the input is terminated.
The &quot;\n&quot; is removed from the line.</p></div></div><div class="spec val" id="val-read_lines"><a href="#val-read_lines" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>read_lines : Pervasives.in_channel <span class="keyword">&#8209;&gt;</span> string <a href="index.html#type-gen">gen</a></code></div><div class="doc"><p>Read all lines. The generator should be traversed only once.</p></div></div><div class="spec val" id="val-read_lines_l"><a href="#val-read_lines_l" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>read_lines_l : Pervasives.in_channel <span class="keyword">&#8209;&gt;</span> string list</code></div><div class="doc"><p>Read all lines into a list.</p></div></div><div class="spec val" id="val-read_all"><a href="#val-read_all" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>read_all : ?&#8288;size:int <span class="keyword">&#8209;&gt;</span> Pervasives.in_channel <span class="keyword">&#8209;&gt;</span> string</code></div><div class="doc"><p>Read the whole channel into a buffer, then converted into a string.</p><ul class="at-tag"><li><span class="at-tag parameter">Parameter</span> <span class="module-path">size</span>: the internal buffer size.</li><li><span class="at-tag since">Since</span>: 0.7</li></ul></div></div><div class="spec val" id="val-read_all_bytes"><a href="#val-read_all_bytes" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>read_all_bytes : ?&#8288;size:int <span class="keyword">&#8209;&gt;</span> Pervasives.in_channel <span class="keyword">&#8209;&gt;</span> Bytes.t</code></div><div class="doc"><p>Read the whole channel into a mutable byte array.</p><ul class="at-tag"><li><span class="at-tag parameter">Parameter</span> <span class="module-path">size</span>: the internal buffer size.</li><li><span class="at-tag since">Since</span>: 0.12</li></ul></div></div><h3>Output</h3><div class="spec val" id="val-with_out"><a href="#val-with_out" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>with_out : ?&#8288;mode:int <span class="keyword">&#8209;&gt;</span> ?&#8288;flags:Pervasives.open_flag list <span class="keyword">&#8209;&gt;</span> string <span class="keyword">&#8209;&gt;</span> (Pervasives.out_channel <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span>) <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span></code></div><div class="doc"><p>Like <a href="index.html#val-with_in">with_in</a> but for an output channel.</p><ul class="at-tag"><li><span class="at-tag parameter">Parameter</span> <span class="module-path">flags</span>: opening flags (default <code class="code">[Open_creat; Open_trunc; Open_text]</code>).</li><li><span class="at-tag raise">Raises</span> <span class="module-path">Sys_error</span>: in case of error (same as open_out and close_out).
<code class="code">Open_wronly</code> is used in any cases.</li></ul></div></div><div class="spec val" id="val-with_out_a"><a href="#val-with_out_a" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>with_out_a : ?&#8288;mode:int <span class="keyword">&#8209;&gt;</span> ?&#8288;flags:Pervasives.open_flag list <span class="keyword">&#8209;&gt;</span> string <span class="keyword">&#8209;&gt;</span> (Pervasives.out_channel <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span>) <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span></code></div><div class="doc"><p>Like <a href="index.html#val-with_out">with_out</a> but with the <code class="code">[Open_append; Open_creat; Open_wronly]</code>
flags activated, to append to the file.</p><ul class="at-tag"><li><span class="at-tag raise">Raises</span> <span class="module-path">Sys_error</span>: in case of error (same as open_out and close_out).</li></ul></div></div><div class="spec val" id="val-write_line"><a href="#val-write_line" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>write_line : Pervasives.out_channel <span class="keyword">&#8209;&gt;</span> string <span class="keyword">&#8209;&gt;</span> unit</code></div><div class="doc"><p>Write the given string on the channel, followed by &quot;\n&quot;.</p></div></div><div class="spec val" id="val-write_gen"><a href="#val-write_gen" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>write_gen : ?&#8288;sep:string <span class="keyword">&#8209;&gt;</span> Pervasives.out_channel <span class="keyword">&#8209;&gt;</span> string <a href="index.html#type-gen">gen</a> <span class="keyword">&#8209;&gt;</span> unit</code></div><div class="doc"><p>Write the given strings on the output. If provided, add <code class="code">sep</code> between
every two strings (but not at the end).</p></div></div><div class="spec val" id="val-write_lines"><a href="#val-write_lines" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>write_lines : Pervasives.out_channel <span class="keyword">&#8209;&gt;</span> string <a href="index.html#type-gen">gen</a> <span class="keyword">&#8209;&gt;</span> unit</code></div><div class="doc"><p>Write every string on the output, followed by &quot;\n&quot;.</p></div></div><div class="spec val" id="val-write_lines_l"><a href="#val-write_lines_l" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>write_lines_l : Pervasives.out_channel <span class="keyword">&#8209;&gt;</span> string list <span class="keyword">&#8209;&gt;</span> unit</code></div><div class="doc"></div></div><h3>Both</h3><div class="spec val" id="val-with_in_out"><a href="#val-with_in_out" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>with_in_out : ?&#8288;mode:int <span class="keyword">&#8209;&gt;</span> ?&#8288;flags:Pervasives.open_flag list <span class="keyword">&#8209;&gt;</span> string <span class="keyword">&#8209;&gt;</span> (Pervasives.in_channel <span class="keyword">&#8209;&gt;</span> Pervasives.out_channel <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span>) <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span></code></div><div class="doc"><p>Combines <a href="index.html#val-with_in">with_in</a> and <a href="index.html#val-with_out">with_out</a>.</p><ul class="at-tag"><li><span class="at-tag parameter">Parameter</span> <span class="module-path">flags</span>: opening flags (default <code class="code">[Open_creat]</code>).</li><li><span class="at-tag raise">Raises</span> <span class="module-path">Sys_error</span>: in case of error.</li><li><span class="at-tag since">Since</span>: 0.12</li></ul></div></div><h3>Misc for Generators</h3><div class="spec val" id="val-tee"><a href="#val-tee" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>tee : (<span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> unit) list <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> <a href="index.html#type-gen">gen</a> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> <a href="index.html#type-gen">gen</a></code></div><div class="doc"><p><code class="code">tee funs gen</code> behaves like <code class="code">gen</code>, but each element is given to
every function <code class="code">f</code> in <code class="code">funs</code> at the time the element is produced.</p></div></div><h3>File and file names</h3><p>How to list recursively files in a directory:
</p><pre><code class="code"> # let files = CCIO.File.read_dir ~recurse:true (CCIO.File.make &quot;/tmp&quot;);;
# CCIO.write_lines stdout files;;</code></pre><p>See <a href="File/index.html#val-walk">File.walk</a> if you also need to list directories:</p><pre><code class="code"> # let content = CCIO.File.walk (CCIO.File.make &quot;/tmp&quot;);;
# Gen.map CCIO.File.show_walk_item content |&gt; CCIO.write_lines stdout;;</code></pre><div class="spec module" id="module-File"><a href="#module-File" class="anchor"></a><div class="def module"><code><span class="keyword">module </span><a href="File/index.html">File</a> : <span class="keyword">sig</span> ... <span class="keyword">end</span></code></div><div class="doc"></div></div></body></html>