memfiles.html 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>std/memfiles</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <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=="/>
  14. <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=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">std/memfiles</h1>
  24. <div class="row">
  25. <div class="three columns">
  26. <div class="theme-select-wrapper">
  27. <label for="theme-select">Theme:&nbsp;</label>
  28. <select id="theme-select" onchange="setTheme(this.value)">
  29. <option value="auto">🌗 Match OS</option>
  30. <option value="dark">🌑 Dark</option>
  31. <option value="light">🌕 Light</option>
  32. </select>
  33. </div>
  34. <div id="global-links">
  35. <ul class="simple-boot">
  36. <li><a href="manual.html">Manual</a></li>
  37. <li><a href="lib.html">Standard library</a></li>
  38. <li> <a id="indexLink" href="theindex.html">Index</a></li>
  39. <li><a href="compiler/theindex.html">Compiler docs</a></li>
  40. <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
  41. <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
  42. </ul>
  43. </div>
  44. <div id="searchInputDiv">
  45. Search: <input type="search" id="searchInput"
  46. oninput="search()" />
  47. </div>
  48. <ul class="simple simple-toc" id="toc-list">
  49. <li>
  50. <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
  51. </li>
  52. <li>
  53. <details open>
  54. <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
  55. <ul class="simple simple-toc-section">
  56. <li><a class="reference" href="#MemFile" title="MemFile = object
  57. mem*: pointer ## a pointer to the memory mapped file. The pointer
  58. ## can be used directly to change the contents of the
  59. ## file, if it was opened with write access.
  60. size*: int ## size of the memory mapped file
  61. when defined(windows):
  62. fHandle*: Handle ## **Caution**: Windows specific public field to allow
  63. ## even more low level trickery.
  64. mapHandle*: Handle ## **Caution**: Windows specific public field.
  65. wasOpened*: bool ## **Caution**: Windows specific public field.
  66. else:
  67. handle*: cint ## **Caution**: Posix specific public field.
  68. ## **Caution**: Platform specific private field.">MemFile</a></li>
  69. <li><a class="reference" href="#MemMapFileStream" title="MemMapFileStream = ref MemMapFileStreamObj">MemMapFileStream</a></li>
  70. <li><a class="reference" href="#MemMapFileStreamObj" title="MemMapFileStreamObj = object of Stream">MemMapFileStreamObj</a></li>
  71. <li><a class="reference" href="#MemSlice" title="MemSlice = object
  72. data*: pointer
  73. size*: int">MemSlice</a></li>
  74. </ul>
  75. </details>
  76. </li>
  77. <li>
  78. <details open>
  79. <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
  80. <ul class="simple simple-toc-section">
  81. <ul class="simple nested-toc-section">$
  82. <li><a class="reference" href="#%24%2CMemSlice" title="`$`(ms: MemSlice): string">`$`(ms: MemSlice): string</a></li>
  83. </ul>
  84. <ul class="simple nested-toc-section">==
  85. <li><a class="reference" href="#%3D%3D%2CMemSlice%2CMemSlice" title="`==`(x, y: MemSlice): bool">`==`(x, y: MemSlice): bool</a></li>
  86. </ul>
  87. <ul class="simple nested-toc-section">close
  88. <li><a class="reference" href="#close%2CMemFile" title="close(f: var MemFile)">close(f: var MemFile)</a></li>
  89. </ul>
  90. <ul class="simple nested-toc-section">flush
  91. <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>
  92. </ul>
  93. <ul class="simple nested-toc-section">mapMem
  94. <li><a class="reference" href="#mapMem%2CMemFile%2CFileMode%2Cint%2Cint" title="mapMem(m: var MemFile; mode: FileMode = fmRead; mappedSize = -1; offset = 0;
  95. mapFlags = cint(-1)): pointer">mapMem(m: var MemFile; mode: FileMode = fmRead; mappedSize = -1; offset = 0;
  96. mapFlags = cint(-1)): pointer</a></li>
  97. </ul>
  98. <ul class="simple nested-toc-section">newMemMapFileStream
  99. <li><a class="reference" href="#newMemMapFileStream%2Cstring%2CFileMode%2Cint" title="newMemMapFileStream(filename: string; mode: FileMode = fmRead;
  100. fileSize: int = -1): MemMapFileStream">newMemMapFileStream(filename: string; mode: FileMode = fmRead;
  101. fileSize: int = -1): MemMapFileStream</a></li>
  102. </ul>
  103. <ul class="simple nested-toc-section">open
  104. <li><a class="reference" href="#open%2Cstring%2CFileMode%2Cint%2Cint%2Cint" title="open(filename: string; mode: FileMode = fmRead; mappedSize = -1; offset = 0;
  105. newFileSize = -1; allowRemap = false; mapFlags = cint(-1)): MemFile">open(filename: string; mode: FileMode = fmRead; mappedSize = -1; offset = 0;
  106. newFileSize = -1; allowRemap = false; mapFlags = cint(-1)): MemFile</a></li>
  107. </ul>
  108. <ul class="simple nested-toc-section">resize
  109. <li><a class="reference" href="#resize%2CMemFile%2Cint" title="resize(f: var MemFile; newFileSize: int)">resize(f: var MemFile; newFileSize: int)</a></li>
  110. </ul>
  111. <ul class="simple nested-toc-section">unmapMem
  112. <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>
  113. </ul>
  114. </ul>
  115. </details>
  116. </li>
  117. <li>
  118. <details open>
  119. <summary><a class="reference reference-toplevel" href="#15" id="65">Iterators</a></summary>
  120. <ul class="simple simple-toc-section">
  121. <ul class="simple nested-toc-section">lines
  122. <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>
  123. <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>
  124. </ul>
  125. <ul class="simple nested-toc-section">memSlices
  126. <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>
  127. </ul>
  128. </ul>
  129. </details>
  130. </li>
  131. </ul>
  132. </div>
  133. <div class="nine columns" id="content">
  134. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  135. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  136. <div id="tocRoot"></div>
  137. <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>
  138. </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>
  139. <p>It also provides some fast iterators over lines in text files (or other &quot;line-like&quot;, variable length, delimited records).</p>
  140. </p>
  141. <div class="section" id="6">
  142. <h1><a class="toc-backref" href="#6">Imports</a></h1>
  143. <dl class="item">
  144. <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>
  145. </dl>
  146. </div>
  147. <div class="section" id="7">
  148. <h1><a class="toc-backref" href="#7">Types</a></h1>
  149. <dl class="item">
  150. <div id="MemFile">
  151. <dt><pre><a href="memfiles.html#MemFile"><span class="Identifier">MemFile</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
  152. <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>
  153. <span class="Comment">## can be used directly to change the contents of the</span>
  154. <span class="Comment">## file, if it was opened with write access.</span>
  155. <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>
  156. <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>
  157. <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>
  158. <span class="Comment">## even more low level trickery.</span>
  159. <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>
  160. <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>
  161. <span class="Keyword">else</span><span class="Other">:</span>
  162. <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>
  163. <span class="Comment">## **Caution**: Platform specific private field.</span></pre></dt>
  164. <dd>
  165. represents a memory mapped file
  166. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L69" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  167. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L69" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  168. </dd>
  169. </div>
  170. <div id="MemMapFileStream">
  171. <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>
  172. <dd>
  173. a stream that encapsulates a <tt class="docutils literal"><span class="pre"><span class="Identifier">MemFile</span></span></tt>
  174. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L504" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  175. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L504" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  176. </dd>
  177. </div>
  178. <div id="MemMapFileStreamObj">
  179. <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>
  180. <dd>
  181. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L505" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  182. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L505" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  183. </dd>
  184. </div>
  185. <div id="MemSlice">
  186. <dt><pre><a href="memfiles.html#MemSlice"><span class="Identifier">MemSlice</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
  187. <span class="Identifier">data</span><span class="Operator">*</span><span class="Other">:</span> <a href="system.html#pointer"><span class="Identifier">pointer</span></a>
  188. <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>
  189. <dd>
  190. represent slice of a MemFile for iteration over delimited lines/records
  191. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L407" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  192. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L407" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  193. </dd>
  194. </div>
  195. </dl>
  196. </div>
  197. <div class="section" id="12">
  198. <h1><a class="toc-backref" href="#12">Procs</a></h1>
  199. <dl class="item">
  200. <div id="$-procs-all">
  201. <div id="$,MemSlice">
  202. <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>
  203. <dd>
  204. Return a Nim string built from a MemSlice.
  205. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L415" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  206. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L415" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  207. </dd>
  208. </div>
  209. </div>
  210. <div id="==-procs-all">
  211. <div id="==,MemSlice,MemSlice">
  212. <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>
  213. <dd>
  214. Compare a pair of MemSlice for strict equality.
  215. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L411" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  216. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L411" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  217. </dd>
  218. </div>
  219. </div>
  220. <div id="close-procs-all">
  221. <div id="close,MemFile">
  222. <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>
  223. <dd>
  224. 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.
  225. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L371" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  226. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L371" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  227. </dd>
  228. </div>
  229. </div>
  230. <div id="flush-procs-all">
  231. <div id="flush,MemFile,Natural">
  232. <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>
  233. <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  234. <dd>
  235. 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.
  236. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L295" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  237. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L295" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  238. </dd>
  239. </div>
  240. </div>
  241. <div id="mapMem-procs-all">
  242. <div id="mapMem,MemFile,FileMode,int,int">
  243. <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>
  244. <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> {.
  245. <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>
  246. <dd>
  247. <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>
  248. <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>
  249. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L84" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  250. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L84" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  251. </dd>
  252. </div>
  253. </div>
  254. <div id="newMemMapFileStream-procs-all">
  255. <div id="newMemMapFileStream,string,FileMode,int">
  256. <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>
  257. <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> {.
  258. <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>
  259. <dd>
  260. 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).
  261. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L548" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  262. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L548" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  263. </dd>
  264. </div>
  265. </div>
  266. <div id="open-procs-all">
  267. <div id="open,string,FileMode,int,int,int">
  268. <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>
  269. <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> {.
  270. <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>
  271. <dd>
  272. <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>
  273. <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>
  274. <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>
  275. <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>
  276. <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>
  277. <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>
  278. <p>Example:</p>
  279. <p><pre class="listing"><span class="Keyword">var</span>
  280. <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>
  281. <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">&quot;/tmp/test.mmap&quot;</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>
  282. <span class="Identifier">mm</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  283. <span class="Comment"># Read the whole file, would fail if newFileSize was set</span>
  284. <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">&quot;/tmp/test.mmap&quot;</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>
  285. <span class="Comment"># Read the first 512 bytes</span>
  286. <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">&quot;/tmp/test.mmap&quot;</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>
  287. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L135" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  288. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L135" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  289. </dd>
  290. </div>
  291. </div>
  292. <div id="resize-procs-all">
  293. <div id="resize,MemFile,int">
  294. <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>
  295. <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>
  296. <dd>
  297. Resize &amp; 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.
  298. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L317" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  299. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L317" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  300. </dd>
  301. </div>
  302. </div>
  303. <div id="unmapMem-procs-all">
  304. <div id="unmapMem,MemFile,pointer,int">
  305. <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>
  306. <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>
  307. <dd>
  308. <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">&lt;</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>
  309. <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>
  310. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L122" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  311. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L122" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  312. </dd>
  313. </div>
  314. </div>
  315. </dl>
  316. </div>
  317. <div class="section" id="15">
  318. <h1><a class="toc-backref" href="#15">Iterators</a></h1>
  319. <dl class="item">
  320. <div id="lines-iterators-all">
  321. <div id="lines.i,MemFile,string,char,char">
  322. <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> {.
  323. <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>
  324. <dd>
  325. <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>
  326. <p>Example:</p>
  327. <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">&quot;&quot;</span>
  328. <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">&quot;foo&quot;</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">buffer</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  329. <span class="Identifier">echo</span> <span class="Identifier">line</span></pre></p>
  330. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L469" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  331. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L469" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  332. </dd>
  333. </div>
  334. <div id="lines.i,MemFile,char,char">
  335. <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>
  336. <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>
  337. <dd>
  338. <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>
  339. <p>Example:</p>
  340. <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">&quot;foo&quot;</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  341. <span class="Identifier">echo</span> <span class="Identifier">line</span></pre></p>
  342. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L488" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  343. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L488" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  344. </dd>
  345. </div>
  346. </div>
  347. <div id="memSlices-iterators-all">
  348. <div id="memSlices.i,MemFile,char,char">
  349. <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>
  350. <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>
  351. <dd>
  352. <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>
  353. <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) &amp; 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>
  354. <p>Delimiters are not part of the returned slice. A final, unterminated line or record is returned just like any other.</p>
  355. <p>Non-default delimiters can be passed to allow iteration over other sorts of &quot;line-like&quot; 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>
  356. <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>
  357. <p>Example:</p>
  358. <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">count</span> <span class="Operator">=</span> <span class="DecNumber">0</span>
  359. <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">&quot;foo&quot;</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  360. <span class="Keyword">if</span> <span class="Identifier">slice</span><span class="Operator">.</span><span class="Identifier">size</span> <span class="Operator">&gt;</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>
  361. <span class="Identifier">inc</span><span class="Punctuation">(</span><span class="Identifier">count</span><span class="Punctuation">)</span>
  362. <span class="Identifier">echo</span> <span class="Identifier">count</span></pre></p>
  363. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/memfiles.nim#L420" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  364. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/memfiles.nim#L420" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  365. </dd>
  366. </div>
  367. </div>
  368. </dl>
  369. </div>
  370. </div>
  371. </div>
  372. <div class="twelve-columns footer">
  373. <span class="nim-sprite"></span>
  374. <br>
  375. <small style="color: var(--hint);">Made with Nim. Generated: 2025-03-12 16:39:35 UTC</small>
  376. </div>
  377. </div>
  378. </div>
  379. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  380. </body>
  381. </html>