sdl_rwstream.monkey2 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Namespace std.stream
  2. Using sdl2
  3. Class SDL_RWStream Extends Stream
  4. #rem monkeydoc True if no more data can be read from the stream.
  5. You can still write to a filestream even if `Eof` is true - disk space permitting!
  6. #end
  7. Property Eof:Bool() Override
  8. Return _pos>=_end
  9. End
  10. #rem monkeydoc Current filestream position.
  11. The current file read/write position.
  12. #end
  13. Property Position:Int() Override
  14. Return _pos
  15. End
  16. #rem monkeydoc Current filestream length.
  17. The length of the filestream. This is the same as the length of the file.
  18. The file length can increase if you write past the end of the file.
  19. #end
  20. Property Length:Int() Override
  21. Return _end
  22. End
  23. #rem monkeydoc Closes the filestream.
  24. Closing the filestream also sets its position and length to 0.
  25. #end
  26. Method OnClose() Override
  27. If Not _rwops Return
  28. SDL_RWclose( _rwops )
  29. _rwops=Null
  30. _pos=0
  31. _end=0
  32. End
  33. #rem monkeydoc Seeks to a position in the filestream.
  34. @param offset The position to seek to.
  35. #end
  36. Method Seek( position:Int ) Override
  37. DebugAssert( position>=0 And position<=_end )
  38. SDL_RWseek( _rwops,position,RW_SEEK_SET )
  39. DebugAssert( SDL_RWtell( _rwops )=position )
  40. _pos=position
  41. End
  42. #rem monkeydoc Reads data from the filestream.
  43. @param buf A pointer to the memory to read the data into.
  44. @param count The number of bytes to read.
  45. @return The number of bytes actually read.
  46. #end
  47. Method Read:Int( buf:Void Ptr,count:Int ) Override
  48. count=Clamp( count,0,_end-_pos )
  49. count=SDL_RWread( _rwops,buf,1,count )
  50. DebugAssert( SDL_RWtell( _rwops )=_pos+count )
  51. _pos+=count
  52. Return count
  53. End
  54. #rem monkeydoc Writes data to the filestream.
  55. Writing past the end of the file will increase the length of a filestream.
  56. @param buf A pointer to the memory to write the data from.
  57. @param count The number of bytes to write.
  58. @return The number of bytes actually written.
  59. #end
  60. Method Write:Int( buf:Void Ptr,count:Int ) Override
  61. If count<=0 Return 0
  62. count=SDL_RWwrite( _rwops,buf,1,count )
  63. DebugAssert( SDL_RWtell( _rwops )=_pos+count )
  64. _pos+=count
  65. Return count
  66. End
  67. #rem monkeydoc Opens a file and returns a new filestream.
  68. @param path The path of the file to open.
  69. @param mode The mode to open the file in: "r", "w" or "rw".
  70. @return A new filestream, or null if the file could not be opened.
  71. #end
  72. Function Open:SDL_RWStream( path:String,mode:String )
  73. Select mode
  74. Case "r"
  75. mode="rb"
  76. Case "w"
  77. mode="wb"
  78. Case "rw"
  79. mode="W+b"
  80. Default
  81. Return Null
  82. End
  83. Local rwops:=SDL_RWFromFile( path,mode )
  84. If Not rwops Return Null
  85. Return New SDL_RWStream( rwops )
  86. End
  87. Private
  88. Field _rwops:SDL_RWops Ptr
  89. Field _pos:Int
  90. Field _end:Int
  91. Method New( rwops:SDL_RWops Ptr )
  92. _rwops=rwops
  93. _pos=SDL_RWtell( _rwops )
  94. _end=SDL_RWsize( _rwops )
  95. End
  96. End