ocaml-containers/0.20/CCIO.html
2016-10-14 13:43:14 +02:00

247 lines
No EOL
16 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<link rel="Start" href="index.html">
<link rel="previous" href="CCFormat.html">
<link rel="next" href="CCInt64.html">
<link rel="Up" href="index.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of exceptions" rel=Appendix href="index_exceptions.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of class methods" rel=Appendix href="index_methods.html">
<link title="Index of class types" rel=Appendix href="index_class_types.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Index of module types" rel=Appendix href="index_module_types.html">
<link title="CCVector" rel="Chapter" href="CCVector.html">
<link title="CCPrint" rel="Chapter" href="CCPrint.html">
<link title="CCError" rel="Chapter" href="CCError.html">
<link title="CCHeap" rel="Chapter" href="CCHeap.html">
<link title="CCList" rel="Chapter" href="CCList.html">
<link title="CCOpt" rel="Chapter" href="CCOpt.html">
<link title="CCPair" rel="Chapter" href="CCPair.html">
<link title="CCFun" rel="Chapter" href="CCFun.html">
<link title="CCHash" rel="Chapter" href="CCHash.html">
<link title="CCInt" rel="Chapter" href="CCInt.html">
<link title="CCBool" rel="Chapter" href="CCBool.html">
<link title="CCFloat" rel="Chapter" href="CCFloat.html">
<link title="CCArray" rel="Chapter" href="CCArray.html">
<link title="CCRef" rel="Chapter" href="CCRef.html">
<link title="CCSet" rel="Chapter" href="CCSet.html">
<link title="CCOrd" rel="Chapter" href="CCOrd.html">
<link title="CCRandom" rel="Chapter" href="CCRandom.html">
<link title="CCString" rel="Chapter" href="CCString.html">
<link title="CCHashtbl" rel="Chapter" href="CCHashtbl.html">
<link title="CCMap" rel="Chapter" href="CCMap.html">
<link title="CCFormat" rel="Chapter" href="CCFormat.html">
<link title="CCIO" rel="Chapter" href="CCIO.html">
<link title="CCInt64" rel="Chapter" href="CCInt64.html">
<link title="CCChar" rel="Chapter" href="CCChar.html">
<link title="CCResult" rel="Chapter" href="CCResult.html">
<link title="Containers" rel="Chapter" href="Containers.html">
<link title="CCKTree" rel="Chapter" href="CCKTree.html">
<link title="CCKList" rel="Chapter" href="CCKList.html">
<link title="CCLazy_list" rel="Chapter" href="CCLazy_list.html">
<link title="CCMultiMap" rel="Chapter" href="CCMultiMap.html">
<link title="CCMultiSet" rel="Chapter" href="CCMultiSet.html">
<link title="CCTrie" rel="Chapter" href="CCTrie.html">
<link title="CCFlatHashtbl" rel="Chapter" href="CCFlatHashtbl.html">
<link title="CCCache" rel="Chapter" href="CCCache.html">
<link title="CCPersistentHashtbl" rel="Chapter" href="CCPersistentHashtbl.html">
<link title="CCDeque" rel="Chapter" href="CCDeque.html">
<link title="CCFQueue" rel="Chapter" href="CCFQueue.html">
<link title="CCBV" rel="Chapter" href="CCBV.html">
<link title="CCMixtbl" rel="Chapter" href="CCMixtbl.html">
<link title="CCMixmap" rel="Chapter" href="CCMixmap.html">
<link title="CCRingBuffer" rel="Chapter" href="CCRingBuffer.html">
<link title="CCIntMap" rel="Chapter" href="CCIntMap.html">
<link title="CCPersistentArray" rel="Chapter" href="CCPersistentArray.html">
<link title="CCMixset" rel="Chapter" href="CCMixset.html">
<link title="CCHashconsedSet" rel="Chapter" href="CCHashconsedSet.html">
<link title="CCGraph" rel="Chapter" href="CCGraph.html">
<link title="CCHashSet" rel="Chapter" href="CCHashSet.html">
<link title="CCBitField" rel="Chapter" href="CCBitField.html">
<link title="CCHashTrie" rel="Chapter" href="CCHashTrie.html">
<link title="CCBloom" rel="Chapter" href="CCBloom.html">
<link title="CCWBTree" rel="Chapter" href="CCWBTree.html">
<link title="CCRAL" rel="Chapter" href="CCRAL.html">
<link title="CCAllocCache" rel="Chapter" href="CCAllocCache.html">
<link title="CCImmutArray" rel="Chapter" href="CCImmutArray.html">
<link title="CCHet" rel="Chapter" href="CCHet.html">
<link title="Containers_string" rel="Chapter" href="Containers_string.html">
<link title="CCKMP" rel="Chapter" href="CCKMP.html">
<link title="CCLevenshtein" rel="Chapter" href="CCLevenshtein.html">
<link title="CCApp_parse" rel="Chapter" href="CCApp_parse.html">
<link title="CCParse" rel="Chapter" href="CCParse.html">
<link title="CCBigstring" rel="Chapter" href="CCBigstring.html">
<link title="CCArray1" rel="Chapter" href="CCArray1.html">
<link title="CCPool" rel="Chapter" href="CCPool.html">
<link title="CCLock" rel="Chapter" href="CCLock.html">
<link title="CCSemaphore" rel="Chapter" href="CCSemaphore.html">
<link title="CCThread" rel="Chapter" href="CCThread.html">
<link title="CCBlockingQueue" rel="Chapter" href="CCBlockingQueue.html">
<link title="CCTimer" rel="Chapter" href="CCTimer.html">
<link title="Containers_advanced" rel="Chapter" href="Containers_advanced.html">
<link title="CCLinq" rel="Chapter" href="CCLinq.html">
<link title="CCBatch" rel="Chapter" href="CCBatch.html">
<link title="CCCat" rel="Chapter" href="CCCat.html">
<link title="CCMonadIO" rel="Chapter" href="CCMonadIO.html">
<link title="Containers_io_is_deprecated" rel="Chapter" href="Containers_io_is_deprecated.html">
<link title="CCUnix" rel="Chapter" href="CCUnix.html">
<link title="CCSexp" rel="Chapter" href="CCSexp.html">
<link title="CCSexpM" rel="Chapter" href="CCSexpM.html"><link title="Input" rel="Section" href="#2_Input">
<link title="Output" rel="Section" href="#2_Output">
<link title="Both" rel="Section" href="#2_Both">
<link title="Misc for Generators" rel="Section" href="#2_MiscforGenerators">
<link title="File and file names" rel="Section" href="#2_Fileandfilenames">
<title>Containers doc : CCIO</title>
</head>
<body>
<div class="navbar"><a class="pre" href="CCFormat.html" title="CCFormat">Previous</a>
&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;<a class="post" href="CCInt64.html" title="CCInt64">Next</a>
</div>
<h1>Module <a href="type_CCIO.html">CCIO</a></h1>
<pre><span class="keyword">module</span> CCIO: <code class="code"><span class="keyword">sig</span></code> <a href="CCIO.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info module top">
<h1 id="1_IOUtils">IO Utils</h1>
<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>
Examples:
<p>
<ul>
<li>obtain the list of lines of a file:</li>
</ul>
<pre class="codepre"><code class="code"><span class="keywordsign">#</span>&nbsp;<span class="keyword">let</span>&nbsp;l&nbsp;=&nbsp;<span class="constructor">CCIO</span>.(with_in&nbsp;<span class="string">"/tmp/some_file"</span>&nbsp;read_lines);;<br>
</code></pre>
<p>
<ul>
<li>transfer one file into another:</li>
</ul>
<pre class="codepre"><code class="code"><span class="keywordsign">#</span>&nbsp;<span class="constructor">CCIO</span>.(<br>
&nbsp;&nbsp;with_in&nbsp;<span class="string">"/tmp/input"</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;(<span class="keyword">fun</span>&nbsp;ic&nbsp;<span class="keywordsign">-&gt;</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;chunks&nbsp;=&nbsp;read_chunks&nbsp;ic&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with_out&nbsp;~flags:[<span class="constructor">Open_binary</span>]&nbsp;~mode:0o644&nbsp;<span class="string">"/tmp/output"</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span class="keyword">fun</span>&nbsp;oc&nbsp;<span class="keywordsign">-&gt;</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_gen&nbsp;oc&nbsp;chunks<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br>
&nbsp;&nbsp;&nbsp;&nbsp;)<br>
)&nbsp;;;<br>
</code></pre><br>
<b>Before 0.12 </b> was in 'containers.io', now moved into 'containers'<br>
<b>Since</b> 0.6<br>
</div>
<hr width="100%">
<pre><span id="TYPEgen"><span class="keyword">type</span> <code class="type">'a</code> gen</span> = <code class="type">unit -> 'a option</code> </pre>
<div class="info ">
See <code class="code"><span class="constructor">Gen</span></code> in the gen library<br>
</div>
<br>
<h2 id="2_Input">Input</h2><br>
<pre><span id="VALwith_in"><span class="keyword">val</span> with_in</span> : <code class="type">?mode:int -><br> ?flags:Pervasives.open_flag list -><br> string -> (Pervasives.in_channel -> 'a) -> 'a</code></pre><div class="info ">
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.<br>
</div>
<div class="param_info"><code class="code">flags</code> : opening flags (default <code class="code">[<span class="constructor">Open_text</span>]</code>). <code class="code"><span class="constructor">Open_rdonly</span></code> is used in any cases</div>
<pre><span id="VALread_chunks"><span class="keyword">val</span> read_chunks</span> : <code class="type">?size:int -> Pervasives.in_channel -> string <a href="CCIO.html#TYPEgen">gen</a></code></pre><div class="info ">
Read the channel's content into chunks of size <code class="code">size</code><br>
</div>
<pre><span id="VALread_line"><span class="keyword">val</span> read_line</span> : <code class="type">Pervasives.in_channel -> string option</code></pre><div class="info ">
Read a line from the channel. Returns <code class="code"><span class="constructor">None</span></code> if the input is terminated.
The "\n" is removed from the line.<br>
</div>
<pre><span id="VALread_lines"><span class="keyword">val</span> read_lines</span> : <code class="type">Pervasives.in_channel -> string <a href="CCIO.html#TYPEgen">gen</a></code></pre><div class="info ">
Read all lines. The generator should be traversed only once.<br>
</div>
<pre><span id="VALread_lines_l"><span class="keyword">val</span> read_lines_l</span> : <code class="type">Pervasives.in_channel -> string list</code></pre><div class="info ">
Read all lines into a list<br>
</div>
<pre><span id="VALread_all"><span class="keyword">val</span> read_all</span> : <code class="type">?size:int -> Pervasives.in_channel -> string</code></pre><div class="info ">
Read the whole channel into a buffer, then converted into a string.<br>
<b>Since</b> 0.7<br>
</div>
<div class="param_info"><code class="code">size</code> : the internal buffer size</div>
<pre><span id="VALread_all_bytes"><span class="keyword">val</span> read_all_bytes</span> : <code class="type">?size:int -> Pervasives.in_channel -> Bytes.t</code></pre><div class="info ">
Read the whole channel into a mutable byte array<br>
<b>Since</b> 0.12<br>
</div>
<div class="param_info"><code class="code">size</code> : the internal buffer size</div>
<br>
<h2 id="2_Output">Output</h2><br>
<pre><span id="VALwith_out"><span class="keyword">val</span> with_out</span> : <code class="type">?mode:int -><br> ?flags:Pervasives.open_flag list -><br> string -> (Pervasives.out_channel -> 'a) -> 'a</code></pre><div class="info ">
Same as <a href="CCIO.html#VALwith_in"><code class="code"><span class="constructor">CCIO</span>.with_in</code></a> but for an output channel<br>
</div>
<div class="param_info"><code class="code">flags</code> : opening flags (default <code class="code">[<span class="constructor">Open_creat</span>; <span class="constructor">Open_trunc</span>; <span class="constructor">Open_text</span>]</code>).
<code class="code"><span class="constructor">Open_wronly</span></code> is used in any cases</div>
<pre><span id="VALwith_out_a"><span class="keyword">val</span> with_out_a</span> : <code class="type">?mode:int -><br> ?flags:Pervasives.open_flag list -><br> string -> (Pervasives.out_channel -> 'a) -> 'a</code></pre><div class="info ">
Similar to <a href="CCIO.html#VALwith_out"><code class="code"><span class="constructor">CCIO</span>.with_out</code></a> but with the <code class="code">[<span class="constructor">Open_append</span>; <span class="constructor">Open_creat</span>; <span class="constructor">Open_wronly</span>]</code>
flags activated, to append to the file<br>
</div>
<pre><span id="VALwrite_line"><span class="keyword">val</span> write_line</span> : <code class="type">Pervasives.out_channel -> string -> unit</code></pre><div class="info ">
Write the given string on the channel, followed by "\n"<br>
</div>
<pre><span id="VALwrite_gen"><span class="keyword">val</span> write_gen</span> : <code class="type">?sep:string -> Pervasives.out_channel -> string <a href="CCIO.html#TYPEgen">gen</a> -> unit</code></pre><div class="info ">
Write the given strings on the output. If provided, add <code class="code">sep</code> between
every two strings (but not at the end)<br>
</div>
<pre><span id="VALwrite_lines"><span class="keyword">val</span> write_lines</span> : <code class="type">Pervasives.out_channel -> string <a href="CCIO.html#TYPEgen">gen</a> -> unit</code></pre><div class="info ">
Write every string on the output, followed by "\n".<br>
</div>
<pre><span id="VALwrite_lines_l"><span class="keyword">val</span> write_lines_l</span> : <code class="type">Pervasives.out_channel -> string list -> unit</code></pre><br>
<h2 id="2_Both">Both</h2><br>
<pre><span id="VALwith_in_out"><span class="keyword">val</span> with_in_out</span> : <code class="type">?mode:int -><br> ?flags:Pervasives.open_flag list -><br> string -> (Pervasives.in_channel -> Pervasives.out_channel -> 'a) -> 'a</code></pre><div class="info ">
Combines <a href="CCIO.html#VALwith_in"><code class="code"><span class="constructor">CCIO</span>.with_in</code></a> and <a href="CCIO.html#VALwith_out"><code class="code"><span class="constructor">CCIO</span>.with_out</code></a>.<br>
<b>Since</b> 0.12<br>
</div>
<div class="param_info"><code class="code">flags</code> : opening flags (default <code class="code">[<span class="constructor">Open_creat</span>]</code>)</div>
<br>
<h2 id="2_MiscforGenerators">Misc for Generators</h2><br>
<pre><span id="VALtee"><span class="keyword">val</span> tee</span> : <code class="type">('a -> unit) list -> 'a <a href="CCIO.html#TYPEgen">gen</a> -> 'a <a href="CCIO.html#TYPEgen">gen</a></code></pre><div class="info ">
<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.<br>
</div>
<br>
<h2 id="2_Fileandfilenames">File and file names</h2>
<p>
How to list recursively files in a directory:
<pre class="codepre"><code class="code"><span class="keywordsign">#</span>&nbsp;<span class="keyword">let</span>&nbsp;files&nbsp;=&nbsp;<span class="constructor">CCIO</span>.<span class="constructor">File</span>.read_dir&nbsp;~recurse:<span class="keyword">true</span>&nbsp;(<span class="constructor">CCIO</span>.<span class="constructor">File</span>.make&nbsp;<span class="string">"/tmp"</span>);;<br>
<span class="keywordsign">#</span>&nbsp;<span class="constructor">CCIO</span>.write_lines&nbsp;stdout&nbsp;files;;<br>
</code></pre>
<p>
See <a href="CCIO.File.html#VALwalk"><code class="code"><span class="constructor">CCIO</span>.<span class="constructor">File</span>.walk</code></a> if you also need to list directories:
<p>
<pre class="codepre"><code class="code"><span class="keywordsign">#</span>&nbsp;<span class="keyword">let</span>&nbsp;content&nbsp;=&nbsp;<span class="constructor">CCIO</span>.<span class="constructor">File</span>.walk&nbsp;(<span class="constructor">CCIO</span>.<span class="constructor">File</span>.make&nbsp;<span class="string">"/tmp"</span>);;<br>
<span class="keywordsign">#</span>&nbsp;<span class="constructor">Gen</span>.map&nbsp;<span class="constructor">CCIO</span>.<span class="constructor">File</span>.show_walk_item&nbsp;content&nbsp;|&gt;&nbsp;<span class="constructor">CCIO</span>.write_lines&nbsp;stdout;;<br>
</code></pre><br>
<pre><span class="keyword">module</span> <a href="CCIO.File.html">File</a>: <code class="code"><span class="keyword">sig</span></code> <a href="CCIO.File.html">..</a> <code class="code"><span class="keyword">end</span></code></pre></body></html>