123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <!-- This file is generated by Nim. -->
- <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>std/memfiles</title>
- <!-- Google fonts -->
- <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
- <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
- <!-- Favicon -->
- <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
- <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
- <!-- CSS -->
- <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
- <!-- JS -->
- <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
- </head>
- <body>
- <div class="document" id="documentId">
- <div class="container">
- <h1 class="title">std/memfiles</h1>
- <div class="row">
- <div class="three columns">
- <div class="theme-select-wrapper">
- <label for="theme-select">Theme: </label>
- <select id="theme-select" onchange="setTheme(this.value)">
- <option value="auto">🌗 Match OS</option>
- <option value="dark">🌑 Dark</option>
- <option value="light">🌕 Light</option>
- </select>
- </div>
- <div id="global-links">
- <ul class="simple-boot">
- <li><a href="manual.html">Manual</a></li>
- <li><a href="lib.html">Standard library</a></li>
- <li> <a id="indexLink" href="theindex.html">Index</a></li>
- <li><a href="compiler/theindex.html">Compiler docs</a></li>
- <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
- <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
- </ul>
- </div>
- <div id="searchInputDiv">
- Search: <input type="search" id="searchInput"
- oninput="search()" />
- </div>
-
- <ul class="simple simple-toc" id="toc-list">
- <li>
- <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
- <ul class="simple simple-toc-section">
- <li><a class="reference" href="#MemFile" title="MemFile = object
- mem*: pointer ## a pointer to the memory mapped file. The pointer
- ## can be used directly to change the contents of the
- ## file, if it was opened with write access.
- size*: int ## size of the memory mapped file
- when defined(windows):
- fHandle*: Handle ## **Caution**: Windows specific public field to allow
- ## even more low level trickery.
- mapHandle*: Handle ## **Caution**: Windows specific public field.
- wasOpened*: bool ## **Caution**: Windows specific public field.
- else:
- handle*: cint ## **Caution**: Posix specific public field.
- ## **Caution**: Platform specific private field.">MemFile</a></li>
- <li><a class="reference" href="#MemMapFileStream" title="MemMapFileStream = ref MemMapFileStreamObj">MemMapFileStream</a></li>
- <li><a class="reference" href="#MemMapFileStreamObj" title="MemMapFileStreamObj = object of Stream">MemMapFileStreamObj</a></li>
- <li><a class="reference" href="#MemSlice" title="MemSlice = object
- data*: pointer
- size*: int">MemSlice</a></li>
- </ul>
- </details>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
- <ul class="simple simple-toc-section">
- <ul class="simple nested-toc-section">$
- <li><a class="reference" href="#%24%2CMemSlice" title="`$`(ms: MemSlice): string">`$`(ms: MemSlice): string</a></li>
- </ul>
- <ul class="simple nested-toc-section">==
- <li><a class="reference" href="#%3D%3D%2CMemSlice%2CMemSlice" title="`==`(x, y: MemSlice): bool">`==`(x, y: MemSlice): bool</a></li>
- </ul>
- <ul class="simple nested-toc-section">close
- <li><a class="reference" href="#close%2CMemFile" title="close(f: var MemFile)">close(f: var MemFile)</a></li>
- </ul>
- <ul class="simple nested-toc-section">flush
- <li><a class="reference" href="#flush%2CMemFile%2CNatural" title="flush(f: var MemFile; attempts: Natural = 3)">flush(f: var MemFile; attempts: Natural = 3)</a></li>
- </ul>
- <ul class="simple nested-toc-section">mapMem
- <li><a class="reference" href="#mapMem%2CMemFile%2CFileMode%2Cint%2Cint" title="mapMem(m: var MemFile; mode: FileMode = fmRead; mappedSize = -1; offset = 0;
- mapFlags = cint(-1)): pointer">mapMem(m: var MemFile; mode: FileMode = fmRead; mappedSize = -1; offset = 0;
- mapFlags = cint(-1)): pointer</a></li>
- </ul>
- <ul class="simple nested-toc-section">newMemMapFileStream
- <li><a class="reference" href="#newMemMapFileStream%2Cstring%2CFileMode%2Cint" title="newMemMapFileStream(filename: string; mode: FileMode = fmRead;
- fileSize: int = -1): MemMapFileStream">newMemMapFileStream(filename: string; mode: FileMode = fmRead;
- fileSize: int = -1): MemMapFileStream</a></li>
- </ul>
- <ul class="simple nested-toc-section">open
- <li><a class="reference" href="#open%2Cstring%2CFileMode%2Cint%2Cint%2Cint" title="open(filename: string; mode: FileMode = fmRead; mappedSize = -1; offset = 0;
- newFileSize = -1; allowRemap = false; mapFlags = cint(-1)): MemFile">open(filename: string; mode: FileMode = fmRead; mappedSize = -1; offset = 0;
- newFileSize = -1; allowRemap = false; mapFlags = cint(-1)): MemFile</a></li>
- </ul>
- <ul class="simple nested-toc-section">resize
- <li><a class="reference" href="#resize%2CMemFile%2Cint" title="resize(f: var MemFile; newFileSize: int)">resize(f: var MemFile; newFileSize: int)</a></li>
- </ul>
- <ul class="simple nested-toc-section">unmapMem
- <li><a class="reference" href="#unmapMem%2CMemFile%2Cpointer%2Cint" title="unmapMem(f: var MemFile; p: pointer; size: int)">unmapMem(f: var MemFile; p: pointer; size: int)</a></li>
- </ul>
- </ul>
- </details>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#15" id="65">Iterators</a></summary>
- <ul class="simple simple-toc-section">
- <ul class="simple nested-toc-section">lines
- <li><a class="reference" href="#lines.i%2CMemFile%2Cstring%2Cchar%2Cchar" title="lines(mfile: MemFile; buf: var string; delim = '\n'; eat = '\r'): string">lines(mfile: MemFile; buf: var string; delim = '\n'; eat = '\r'): string</a></li>
- <li><a class="reference" href="#lines.i%2CMemFile%2Cchar%2Cchar" title="lines(mfile: MemFile; delim = '\n'; eat = '\r'): string">lines(mfile: MemFile; delim = '\n'; eat = '\r'): string</a></li>
- </ul>
- <ul class="simple nested-toc-section">memSlices
- <li><a class="reference" href="#memSlices.i%2CMemFile%2Cchar%2Cchar" title="memSlices(mfile: MemFile; delim = '\n'; eat = '\r'): MemSlice">memSlices(mfile: MemFile; delim = '\n'; eat = '\r'): MemSlice</a></li>
- </ul>
- </ul>
- </details>
- </li>
- </ul>
- </div>
- <div class="nine columns" id="content">
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L1" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L1" class="link-seesrc" target="_blank" >Edit</a>
- <div id="tocRoot"></div>
-
- <p class="module-desc"><table class="docinfo" frame="void" rules="none"><col class="docinfo-name" /><col class="docinfo-content" /><tbody valign="top"><tr><th class="docinfo-name">Authors:</th><td>Zahary Karadjov, Andreas Rumpf</td></tr>
- </tbody></table><p>This module provides support for <span id="memory-mapped-files_1">memory mapped files</span> (Posix's <span id="mmap_1">mmap</span>) on the different operating systems.</p>
- <p>It also provides some fast iterators over lines in text files (or other "line-like", variable length, delimited records).</p>
- </p>
- <div class="section" id="6">
- <h1><a class="toc-backref" href="#6">Imports</a></h1>
- <dl class="item">
- <a class="reference external" href="posix.html">posix</a>, <a class="reference external" href="streams.html">streams</a>, <a class="reference external" href="oserrors.html">oserrors</a>, <a class="reference external" href="syncio.html">syncio</a>, <a class="reference external" href="assertions.html">assertions</a>, <a class="reference external" href="ansi_c.html">ansi_c</a>
- </dl>
- </div>
- <div class="section" id="7">
- <h1><a class="toc-backref" href="#7">Types</a></h1>
- <dl class="item">
- <div id="MemFile">
- <dt><pre><a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
- <span class="Identifier">mem</span><span class="Operator">*</span><span class="Other">:</span> <a href="system.html#pointer"><span class="Identifier">pointer</span></a> <span class="Comment">## a pointer to the memory mapped file. The pointer</span>
- <span class="Comment">## can be used directly to change the contents of the</span>
- <span class="Comment">## file, if it was opened with write access.</span>
- <span class="Identifier">size</span><span class="Operator">*</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> <span class="Comment">## size of the memory mapped file</span>
- <span class="Keyword">when</span> <span class="Identifier">defined</span><span class="Other">(</span><span class="Identifier">windows</span><span class="Other">)</span><span class="Other">:</span>
- <span class="Identifier">fHandle</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">Handle</span> <span class="Comment">## **Caution**: Windows specific public field to allow</span>
- <span class="Comment">## even more low level trickery.</span>
- <span class="Identifier">mapHandle</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">Handle</span> <span class="Comment">## **Caution**: Windows specific public field.</span>
- <span class="Identifier">wasOpened</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">bool</span> <span class="Comment">## **Caution**: Windows specific public field.</span>
- <span class="Keyword">else</span><span class="Other">:</span>
- <span class="Identifier">handle</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">cint</span> <span class="Comment">## **Caution**: Posix specific public field.</span>
- <span class="Comment">## **Caution**: Platform specific private field.</span></pre></dt>
- <dd>
-
- represents a memory mapped file
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L69" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L69" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="MemMapFileStream">
- <dt><pre><a href="memfiles.html#MemMapFileStream"><span class="Identifier">MemMapFileStream</span></a> <span class="Other">=</span> <span class="Keyword">ref</span> <a href="memfiles.html#MemMapFileStreamObj"><span class="Identifier">MemMapFileStreamObj</span></a></pre></dt>
- <dd>
-
- a stream that encapsulates a <tt class="docutils literal"><span class="pre"><span class="Identifier">MemFile</span></span></tt>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L504" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L504" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="MemMapFileStreamObj">
- <dt><pre><a href="memfiles.html#MemMapFileStreamObj"><span class="Identifier">MemMapFileStreamObj</span></a> <span class="Other">=</span> <span class="Keyword">object</span> <span class="Keyword">of</span> <a href="streams.html#Stream"><span class="Identifier">Stream</span></a></pre></dt>
- <dd>
-
-
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L505" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L505" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="MemSlice">
- <dt><pre><a href="memfiles.html#MemSlice"><span class="Identifier">MemSlice</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
- <span class="Identifier">data</span><span class="Operator">*</span><span class="Other">:</span> <a href="system.html#pointer"><span class="Identifier">pointer</span></a>
- <span class="Identifier">size</span><span class="Operator">*</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a></pre></dt>
- <dd>
-
- represent slice of a MemFile for iteration over delimited lines/records
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L407" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L407" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </dl>
- </div>
- <div class="section" id="12">
- <h1><a class="toc-backref" href="#12">Procs</a></h1>
- <dl class="item">
- <div id="$-procs-all">
- <div id="$,MemSlice">
- <dt><pre><span class="Keyword">proc</span> <a href="#%24%2CMemSlice"><span class="Identifier">`$`</span></a><span class="Other">(</span><span class="Identifier">ms</span><span class="Other">:</span> <a href="memfiles.html#MemSlice"><span class="Identifier">MemSlice</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a> {.<span class="Identifier">inline</span><span class="Other">,</span> <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Return a Nim string built from a MemSlice.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L415" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L415" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="==-procs-all">
- <div id="==,MemSlice,MemSlice">
- <dt><pre><span class="Keyword">proc</span> <a href="#%3D%3D%2CMemSlice%2CMemSlice"><span class="Identifier">`==`</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="memfiles.html#MemSlice"><span class="Identifier">MemSlice</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Compare a pair of MemSlice for strict equality.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L411" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L411" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="close-procs-all">
- <div id="close,MemFile">
- <dt><pre><span class="Keyword">proc</span> <a href="#close%2CMemFile"><span class="Identifier">close</span></a><span class="Other">(</span><span class="Identifier">f</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">OSError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- closes the memory mapped file <tt class="docutils literal"><span class="pre"><span class="Identifier">f</span></span></tt>. All changes are written back to the file system, if <tt class="docutils literal"><span class="pre"><span class="Identifier">f</span></span></tt> was opened with write access.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L371" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L371" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="flush-procs-all">
- <div id="flush,MemFile,Natural">
- <dt><pre><span class="Keyword">proc</span> <a href="#flush%2CMemFile%2CNatural"><span class="Identifier">flush</span></a><span class="Other">(</span><span class="Identifier">f</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a><span class="Other">;</span> <span class="Identifier">attempts</span><span class="Other">:</span> <a href="system.html#Natural"><span class="Identifier">Natural</span></a> <span class="Other">=</span> <span class="DecNumber">3</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">OSError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Flushes <tt class="docutils literal"><span class="pre"><span class="Identifier">f</span></span></tt>'s buffer for the number of attempts equal to <tt class="docutils literal"><span class="pre"><span class="Identifier">attempts</span></span></tt>. If were errors an exception <tt class="docutils literal"><span class="pre"><span class="Identifier">OSError</span></span></tt> will be raised.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L295" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L295" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="mapMem-procs-all">
- <div id="mapMem,MemFile,FileMode,int,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#mapMem%2CMemFile%2CFileMode%2Cint%2Cint"><span class="Identifier">mapMem</span></a><span class="Other">(</span><span class="Identifier">m</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a><span class="Other">;</span> <span class="Identifier">mode</span><span class="Other">:</span> <a href="syncio.html#FileMode"><span class="Identifier">FileMode</span></a> <span class="Other">=</span> <span class="Identifier">fmRead</span><span class="Other">;</span> <span class="Identifier">mappedSize</span> <span class="Other">=</span> <span class="DecNumber">-1</span><span class="Other">;</span>
- <span class="Identifier">offset</span> <span class="Other">=</span> <span class="DecNumber">0</span><span class="Other">;</span> <span class="Identifier">mapFlags</span> <span class="Other">=</span> <a href="ctypes.html#cint"><span class="Identifier">cint</span></a><span class="Other">(</span><span class="DecNumber">-1</span><span class="Other">)</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#pointer"><span class="Identifier">pointer</span></a> {.
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">IOError</span><span class="Other">,</span> <span class="Identifier">OSError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>returns a pointer to a mapped portion of MemFile <tt class="docutils literal"><span class="pre"><span class="Identifier">m</span></span></tt></p>
- <p><tt class="docutils literal"><span class="pre"><span class="Identifier">mappedSize</span></span></tt> of <tt class="docutils literal"><span class="pre"><span class="Operator">-</span><span class="DecNumber">1</span></span></tt> maps to the whole file, and <tt class="docutils literal"><span class="pre"><span class="Identifier">offset</span></span></tt> must be multiples of the PAGE SIZE of your OS</p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L84" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L84" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="newMemMapFileStream-procs-all">
- <div id="newMemMapFileStream,string,FileMode,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#newMemMapFileStream%2Cstring%2CFileMode%2Cint"><span class="Identifier">newMemMapFileStream</span></a><span class="Other">(</span><span class="Identifier">filename</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">;</span> <span class="Identifier">mode</span><span class="Other">:</span> <a href="syncio.html#FileMode"><span class="Identifier">FileMode</span></a> <span class="Other">=</span> <span class="Identifier">fmRead</span><span class="Other">;</span>
- <span class="Identifier">fileSize</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> <span class="Other">=</span> <span class="DecNumber">-1</span><span class="Other">)</span><span class="Other">:</span> <a href="memfiles.html#MemMapFileStream"><span class="Identifier">MemMapFileStream</span></a> {.
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">IOError</span><span class="Other">,</span> <span class="Identifier">OSError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- creates a new stream from the file named <tt class="docutils literal"><span class="pre"><span class="Identifier">filename</span></span></tt> with the mode <tt class="docutils literal"><span class="pre"><span class="Identifier">mode</span></span></tt>. Raises ## <tt class="docutils literal"><span class="pre"><span class="Identifier">OSError</span></span></tt> if the file cannot be opened. See the <a class="reference external" href="system.html">system</a> module for a list of available FileMode enums. <tt class="docutils literal"><span class="pre"><span class="Identifier">fileSize</span></span></tt> can only be set if the file does not exist and is opened with write access (e.g., with fmReadWrite).
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L548" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L548" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="open-procs-all">
- <div id="open,string,FileMode,int,int,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#open%2Cstring%2CFileMode%2Cint%2Cint%2Cint"><span class="Identifier">open</span></a><span class="Other">(</span><span class="Identifier">filename</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">;</span> <span class="Identifier">mode</span><span class="Other">:</span> <a href="syncio.html#FileMode"><span class="Identifier">FileMode</span></a> <span class="Other">=</span> <span class="Identifier">fmRead</span><span class="Other">;</span> <span class="Identifier">mappedSize</span> <span class="Other">=</span> <span class="DecNumber">-1</span><span class="Other">;</span>
- <span class="Identifier">offset</span> <span class="Other">=</span> <span class="DecNumber">0</span><span class="Other">;</span> <span class="Identifier">newFileSize</span> <span class="Other">=</span> <span class="DecNumber">-1</span><span class="Other">;</span> <span class="Identifier">allowRemap</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span> <span class="Identifier">mapFlags</span> <span class="Other">=</span> <a href="ctypes.html#cint"><span class="Identifier">cint</span></a><span class="Other">(</span><span class="DecNumber">-1</span><span class="Other">)</span><span class="Other">)</span><span class="Other">:</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a> {.
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">IOError</span><span class="Other">,</span> <span class="Identifier">OSError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>opens a memory mapped file. If this fails, <tt class="docutils literal"><span class="pre"><span class="Identifier">OSError</span></span></tt> is raised.</p>
- <p><tt class="docutils literal"><span class="pre"><span class="Identifier">newFileSize</span></span></tt> can only be set if the file does not exist and is opened with write access (e.g., with fmReadWrite).</p>
- <p><tt class="docutils literal"><span class="pre"><span class="Identifier">mappedSize</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">offset</span></span></tt> can be used to map only a slice of the file.</p>
- <p><tt class="docutils literal"><span class="pre"><span class="Identifier">offset</span></span></tt> must be multiples of the PAGE SIZE of your OS (usually 4K or 8K but is unique to your OS)</p>
- <p><tt class="docutils literal"><span class="pre"><span class="Identifier">allowRemap</span></span></tt> only needs to be true if you want to call <tt class="docutils literal"><span class="pre"><span class="Identifier">mapMem</span></span></tt> on the resulting MemFile; else file handles are not kept open.</p>
- <p><tt class="docutils literal"><span class="pre"><span class="Identifier">mapFlags</span></span></tt> allows callers to override default choices for memory mapping flags with a bitwise mask of a variety of likely platform-specific flags which may be ignored or even cause <tt class="docutils literal"><span class="pre"><span class="Identifier">open</span></span></tt> to fail if misspecified.</p>
- <p>Example:</p>
- <p><pre class="listing"><span class="Keyword">var</span>
- <span class="Identifier">mm</span><span class="Punctuation">,</span> <span class="Identifier">mm_full</span><span class="Punctuation">,</span> <span class="Identifier">mm_half</span><span class="Punctuation">:</span> <span class="Identifier">MemFile</span>
- <span class="Identifier">mm</span> <span class="Operator">=</span> <span class="Identifier">memfiles</span><span class="Operator">.</span><span class="Identifier">open</span><span class="Punctuation">(</span><span class="StringLit">"/tmp/test.mmap"</span><span class="Punctuation">,</span> <span class="Identifier">mode</span> <span class="Operator">=</span> <span class="Identifier">fmWrite</span><span class="Punctuation">,</span> <span class="Identifier">newFileSize</span> <span class="Operator">=</span> <span class="DecNumber">1024</span><span class="Punctuation">)</span> <span class="Comment"># Create a new file</span>
- <span class="Identifier">mm</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
- <span class="Comment"># Read the whole file, would fail if newFileSize was set</span>
- <span class="Identifier">mm_full</span> <span class="Operator">=</span> <span class="Identifier">memfiles</span><span class="Operator">.</span><span class="Identifier">open</span><span class="Punctuation">(</span><span class="StringLit">"/tmp/test.mmap"</span><span class="Punctuation">,</span> <span class="Identifier">mode</span> <span class="Operator">=</span> <span class="Identifier">fmReadWrite</span><span class="Punctuation">,</span> <span class="Identifier">mappedSize</span> <span class="Operator">=</span> <span class="Operator">-</span><span class="DecNumber">1</span><span class="Punctuation">)</span>
- <span class="Comment"># Read the first 512 bytes</span>
- <span class="Identifier">mm_half</span> <span class="Operator">=</span> <span class="Identifier">memfiles</span><span class="Operator">.</span><span class="Identifier">open</span><span class="Punctuation">(</span><span class="StringLit">"/tmp/test.mmap"</span><span class="Punctuation">,</span> <span class="Identifier">mode</span> <span class="Operator">=</span> <span class="Identifier">fmReadWrite</span><span class="Punctuation">,</span> <span class="Identifier">mappedSize</span> <span class="Operator">=</span> <span class="DecNumber">512</span><span class="Punctuation">)</span></pre></p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L135" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L135" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="resize-procs-all">
- <div id="resize,MemFile,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#resize%2CMemFile%2Cint"><span class="Identifier">resize</span></a><span class="Other">(</span><span class="Identifier">f</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a><span class="Other">;</span> <span class="Identifier">newFileSize</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><a href="exceptions.html#IOError"><span class="Identifier">IOError</span></a><span class="Other">,</span> <a href="exceptions.html#OSError"><span class="Identifier">OSError</span></a><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Resize & re-map the file underlying an <tt class="docutils literal"><span class="pre"><span class="Identifier">allowRemap</span> <span class="Identifier">MemFile</span></span></tt>. If the OS/FS supports it, file space is reserved to ensure room for new virtual pages. Caller should wait often enough for <tt class="docutils literal"><span class="pre"><span class="Identifier">flush</span></span></tt> to finish to limit use of system RAM for write buffering, perhaps just prior to this call. <strong>Note</strong>: this assumes the entire file is mapped read-write at offset 0. Also, the value of <tt class="docutils literal"><span class="pre"><span class="Operator">.</span><span class="Identifier">mem</span></span></tt> will probably change.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L317" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L317" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="unmapMem-procs-all">
- <div id="unmapMem,MemFile,pointer,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#unmapMem%2CMemFile%2Cpointer%2Cint"><span class="Identifier">unmapMem</span></a><span class="Other">(</span><span class="Identifier">f</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a><span class="Other">;</span> <span class="Identifier">p</span><span class="Other">:</span> <a href="system.html#pointer"><span class="Identifier">pointer</span></a><span class="Other">;</span> <span class="Identifier">size</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">OSError</span><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>unmaps the memory region <tt class="docutils literal"><span class="pre"><span class="Punctuation">(</span><span class="Identifier">p</span><span class="Punctuation">,</span> <span class="Operator"><</span><span class="Identifier">p</span><span class="Operator">+</span><span class="Identifier">size</span><span class="Punctuation">)</span></span></tt> of the mapped file <tt class="docutils literal"><span class="pre"><span class="Identifier">f</span></span></tt>. All changes are written back to the file system, if <tt class="docutils literal"><span class="pre"><span class="Identifier">f</span></span></tt> was opened with write access.</p>
- <p><tt class="docutils literal"><span class="pre"><span class="Identifier">size</span></span></tt> must be of exactly the size that was requested via <tt class="docutils literal"><span class="pre"><span class="Identifier">mapMem</span></span></tt>.</p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L122" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L122" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- </dl>
- </div>
- <div class="section" id="15">
- <h1><a class="toc-backref" href="#15">Iterators</a></h1>
- <dl class="item">
- <div id="lines-iterators-all">
- <div id="lines.i,MemFile,string,char,char">
- <dt><pre><span class="Keyword">iterator</span> <a href="#lines.i%2CMemFile%2Cstring%2Cchar%2Cchar"><span class="Identifier">lines</span></a><span class="Other">(</span><span class="Identifier">mfile</span><span class="Other">:</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a><span class="Other">;</span> <span class="Identifier">buf</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">;</span> <span class="Identifier">delim</span> <span class="Other">=</span> <span class="CharLit">'\n'</span><span class="Other">;</span> <span class="Identifier">eat</span> <span class="Other">=</span> <span class="CharLit">'\r'</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a> {.
- <span class="Identifier">inline</span><span class="Other">,</span> <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Replace contents of passed buffer with each new line, like <a class="reference external" href="syncio.html#readLine,File,string">readLine(File)</a>. <tt class="docutils literal"><span class="pre"><span class="Identifier">delim</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">eat</span></span></tt>, and delimiting logic is exactly as for <a class="reference external" href="#memSlices.i,MemFile,char,char">memSlices</a>, but Nim strings are returned.</p>
- <p>Example:</p>
- <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">buffer</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span> <span class="StringLit">""</span>
- <span class="Keyword">for</span> <span class="Identifier">line</span> <span class="Keyword">in</span> <span class="Identifier">lines</span><span class="Punctuation">(</span><span class="Identifier">memfiles</span><span class="Operator">.</span><span class="Identifier">open</span><span class="Punctuation">(</span><span class="StringLit">"foo"</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">buffer</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
- <span class="Identifier">echo</span> <span class="Identifier">line</span></pre></p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L469" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L469" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="lines.i,MemFile,char,char">
- <dt><pre><span class="Keyword">iterator</span> <a href="#lines.i%2CMemFile%2Cchar%2Cchar"><span class="Identifier">lines</span></a><span class="Other">(</span><span class="Identifier">mfile</span><span class="Other">:</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a><span class="Other">;</span> <span class="Identifier">delim</span> <span class="Other">=</span> <span class="CharLit">'\n'</span><span class="Other">;</span> <span class="Identifier">eat</span> <span class="Other">=</span> <span class="CharLit">'\r'</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a> {.<span class="Identifier">inline</span><span class="Other">,</span>
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Return each line in a file as a Nim string, like <a class="reference external" href="syncio.html#lines.i,File">lines(File)</a>. <tt class="docutils literal"><span class="pre"><span class="Identifier">delim</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">eat</span></span></tt>, and delimiting logic is exactly as for <a class="reference external" href="#memSlices.i,MemFile,char,char">memSlices</a>, but Nim strings are returned.</p>
- <p>Example:</p>
- <p><pre class="listing"><span class="Keyword">for</span> <span class="Identifier">line</span> <span class="Keyword">in</span> <span class="Identifier">lines</span><span class="Punctuation">(</span><span class="Identifier">memfiles</span><span class="Operator">.</span><span class="Identifier">open</span><span class="Punctuation">(</span><span class="StringLit">"foo"</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
- <span class="Identifier">echo</span> <span class="Identifier">line</span></pre></p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L488" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L488" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="memSlices-iterators-all">
- <div id="memSlices.i,MemFile,char,char">
- <dt><pre><span class="Keyword">iterator</span> <a href="#memSlices.i%2CMemFile%2Cchar%2Cchar"><span class="Identifier">memSlices</span></a><span class="Other">(</span><span class="Identifier">mfile</span><span class="Other">:</span> <a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a><span class="Other">;</span> <span class="Identifier">delim</span> <span class="Other">=</span> <span class="CharLit">'\n'</span><span class="Other">;</span> <span class="Identifier">eat</span> <span class="Other">=</span> <span class="CharLit">'\r'</span><span class="Other">)</span><span class="Other">:</span> <a href="memfiles.html#MemSlice"><span class="Identifier">MemSlice</span></a> {.<span class="Identifier">inline</span><span class="Other">,</span>
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Iterates over [optional <tt class="docutils literal"><span class="pre"><span class="Identifier">eat</span></span></tt>] <tt class="docutils literal"><span class="pre"><span class="Identifier">delim</span></span></tt>-delimited slices in MemFile <tt class="docutils literal"><span class="pre"><span class="Identifier">mfile</span></span></tt>.</p>
- <p>Default parameters parse lines ending in either Unix(\l) or Windows(\r\l) style on on a line-by-line basis. I.e., not every line needs the same ending. Unlike readLine(File) & lines(File), archaic MacOS9 \r-delimited lines are not supported as a third option for each line. Such archaic MacOS9 files can be handled by passing delim='\r', eat='\0', though.</p>
- <p>Delimiters are not part of the returned slice. A final, unterminated line or record is returned just like any other.</p>
- <p>Non-default delimiters can be passed to allow iteration over other sorts of "line-like" variable length records. Pass eat='\0' to be strictly <tt class="docutils literal"><span class="pre"><span class="Identifier">delim</span></span></tt>-delimited. (Eating an optional prefix equal to '\0' is not supported.)</p>
- <p>This zero copy, memchr-limited interface is probably the fastest way to iterate over line-like records in a file. However, returned (data,size) objects are not Nim strings, bounds checked Nim arrays, or even terminated C strings. So, care is required to access the data (e.g., think C mem* functions, not str* functions).</p>
- <p>Example:</p>
- <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">count</span> <span class="Operator">=</span> <span class="DecNumber">0</span>
- <span class="Keyword">for</span> <span class="Identifier">slice</span> <span class="Keyword">in</span> <span class="Identifier">memSlices</span><span class="Punctuation">(</span><span class="Identifier">memfiles</span><span class="Operator">.</span><span class="Identifier">open</span><span class="Punctuation">(</span><span class="StringLit">"foo"</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
- <span class="Keyword">if</span> <span class="Identifier">slice</span><span class="Operator">.</span><span class="Identifier">size</span> <span class="Operator">></span> <span class="DecNumber">0</span> <span class="Keyword">and</span> <span class="Keyword">cast</span><span class="Punctuation">[</span><span class="Identifier">cstring</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Identifier">slice</span><span class="Operator">.</span><span class="Identifier">data</span><span class="Punctuation">)</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">!=</span> <span class="CharLit">'#'</span><span class="Punctuation">:</span>
- <span class="Identifier">inc</span><span class="Punctuation">(</span><span class="Identifier">count</span><span class="Punctuation">)</span>
- <span class="Identifier">echo</span> <span class="Identifier">count</span></pre></p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L420" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L420" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- </dl>
- </div>
- </div>
- </div>
- <div class="twelve-columns footer">
- <span class="nim-sprite"></span>
- <br>
- <small style="color: var(--hint);">Made with Nim. Generated: 2025-03-12 16:39:35 UTC</small>
- </div>
- </div>
- </div>
- <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
-
- </body>
- </html>
|