* Add MemMapFileStream * Added tests * Fixed bug in memfiles (zero index for string) * Added flush to changelog * Attempt to fix Win's nuances * Fix attempt to fix * Continue... * And again... * Reworked tests (all for win on Win) * Fixes in flush (Win) * Replace fn vars to consts * Added the attempts parameter to the flush * Replace while to for * Move to memfiles * Use Natural instead of uint * Better error messages for append mode. Handle specific cases.
38 lines
880 B
Nim
38 lines
880 B
Nim
discard """
|
|
file: "tmemfiles2.nim"
|
|
output: '''Full read size: 20
|
|
Half read size: 10 Data: Hello'''
|
|
"""
|
|
import memfiles, os
|
|
const
|
|
fn = "test.mmap"
|
|
var
|
|
mm, mm_full, mm_half: MemFile
|
|
p: pointer
|
|
|
|
if fileExists(fn): removeFile(fn)
|
|
|
|
# Create a new file, data all zeros
|
|
mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 20)
|
|
mm.close()
|
|
|
|
# read, change
|
|
mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1, allowRemap = true)
|
|
echo "Full read size: ",mm_full.size
|
|
p = mm_full.mapMem(fmReadWrite, 20, 0)
|
|
var p2 = cast[cstring](p)
|
|
p2[0] = 'H'
|
|
p2[1] = 'e'
|
|
p2[2] = 'l'
|
|
p2[3] = 'l'
|
|
p2[4] = 'o'
|
|
p2[5] = '\0'
|
|
mm_full.unmapMem(p, 20)
|
|
mm_full.close()
|
|
|
|
# read half, and verify data change
|
|
mm_half = memfiles.open(fn, mode = fmRead, mappedSize = 10)
|
|
echo "Half read size: ",mm_half.size, " Data: ", cast[cstring](mm_half.mem)
|
|
mm_half.close()
|
|
|
|
if fileExists(fn): removeFile(fn)
|