tmemfiles2.nim 895 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. discard """
  2. disabled: "Windows"
  3. output: '''Full read size: 20
  4. Half read size: 10 Data: Hello'''
  5. """
  6. import memfiles, os
  7. const
  8. fn = "test.mmap"
  9. var
  10. mm, mm_full, mm_half: MemFile
  11. p: pointer
  12. if fileExists(fn): removeFile(fn)
  13. # Create a new file, data all zeros
  14. mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 20)
  15. mm.close()
  16. # read, change
  17. mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1, allowRemap = true)
  18. let size = mm_full.size
  19. p = mm_full.mapMem(fmReadWrite, 20, 0)
  20. echo "Full read size: ", size
  21. var p2 = cast[cstring](p)
  22. p2[0] = 'H'
  23. p2[1] = 'e'
  24. p2[2] = 'l'
  25. p2[3] = 'l'
  26. p2[4] = 'o'
  27. p2[5] = '\0'
  28. mm_full.unmapMem(p, 20)
  29. mm_full.close()
  30. # read half, and verify data change
  31. mm_half = memfiles.open(fn, mode = fmRead, mappedSize = 10)
  32. echo "Half read size: ", mm_half.size, " Data: ", cast[cstring](mm_half.mem)
  33. mm_half.close()
  34. if fileExists(fn): removeFile(fn)