tmemmapstreams.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. const
  16. fn = "test.mmapstream"
  17. var
  18. mms: MemMapFileStream
  19. if fileExists(fn): removeFile(fn)
  20. # Create a new memory mapped file, data all zeros
  21. mms = newMemMapFileStream(fn, mode = fmReadWrite, fileSize = 10)
  22. mms.close()
  23. if fileExists(fn): echo "Created size: ", getFileSize(fn)
  24. # write, flush, peek, read
  25. mms = newMemMapFileStream(fn, mode = fmReadWrite)
  26. let s = "Hello"
  27. mms.write(s)
  28. mms.flush
  29. echo "Position after writing: ", mms.getPosition()
  30. mms.write('!')
  31. mms.flush
  32. echo "Position after writing one char: ", mms.getPosition()
  33. mms.close()
  34. mms = newMemMapFileStream(fn, mode = fmRead)
  35. echo "Peeked data: ", mms.peekStr(s.len)
  36. echo "Position after peeking: ", mms.getPosition()
  37. echo "Readed data: ", mms.readLine
  38. echo "Position after reading line: ", mms.getPosition()
  39. mms.setPosition(mms.getPosition() - 1)
  40. echo "Position after setting position: ", mms.getPosition()
  41. mms.setPosition(0)
  42. echo "Readed line: ", mms.readLine
  43. echo "Position after reading line: ", mms.getPosition()
  44. mms.close()
  45. if fileExists(fn): removeFile(fn)