tmemmapstreams.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. discard """
  2. output: '''
  3. Created size: 10
  4. Position after writing: 5
  5. Position after writing one char: 6
  6. Peeked data: Hello
  7. Position after peeking: 0
  8. Readed data: Hello!
  9. Position after reading line: 7
  10. Position after setting position: 6
  11. Readed line: Hello!
  12. Position after reading line: 7'''
  13. """
  14. import os, streams, memfiles
  15. import std/syncio
  16. const
  17. fn = "test.mmapstream"
  18. var
  19. mms: MemMapFileStream
  20. if fileExists(fn): removeFile(fn)
  21. # Create a new memory mapped file, data all zeros
  22. mms = newMemMapFileStream(fn, mode = fmReadWrite, fileSize = 10)
  23. mms.close()
  24. if fileExists(fn): echo "Created size: ", getFileSize(fn)
  25. # write, flush, peek, read
  26. mms = newMemMapFileStream(fn, mode = fmReadWrite)
  27. let s = "Hello"
  28. mms.write(s)
  29. mms.flush
  30. echo "Position after writing: ", mms.getPosition()
  31. mms.write('!')
  32. mms.flush
  33. echo "Position after writing one char: ", mms.getPosition()
  34. mms.close()
  35. mms = newMemMapFileStream(fn, mode = fmRead)
  36. echo "Peeked data: ", mms.peekStr(s.len)
  37. echo "Position after peeking: ", mms.getPosition()
  38. echo "Readed data: ", mms.readLine
  39. echo "Position after reading line: ", mms.getPosition()
  40. mms.setPosition(mms.getPosition() - 1)
  41. echo "Position after setting position: ", mms.getPosition()
  42. mms.setPosition(0)
  43. echo "Readed line: ", mms.readLine
  44. echo "Position after reading line: ", mms.getPosition()
  45. mms.close()
  46. if fileExists(fn): removeFile(fn)