stream.monkey2 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. Namespace std.stream
  2. Using libc
  3. Using std.memory
  4. Using std.collections
  5. #rem monkeydoc Stream class.
  6. #end
  7. Class Stream
  8. #rem monkeydoc True if no more data can be read from the stream.
  9. #end
  10. Property Eof:Bool() Abstract
  11. #rem monkeydoc Current stream position.
  12. In the case of non-seekable streams, `Position` will always be -1.
  13. #end
  14. Property Position:Int() Abstract
  15. #rem monkeydoc Current stream length.
  16. In the case of non-seekable streams, `Length` is the number of bytes that can be read from the stream without 'blocking'.
  17. #end
  18. Property Length:Int() Abstract
  19. #rem monkeydoc Closes the stream.
  20. #end
  21. Method Close:Void()
  22. OnClose()
  23. _tmpbuf.Discard()
  24. End
  25. #rem monkeydoc Seeks to a position in the stream.
  26. In debug builds, a runtime error will occur if the stream is not seekable or `position` is out of range.
  27. @param position The position to seek to.
  28. #end
  29. Method Seek( position:Int ) Abstract
  30. #rem monkeydoc Reads data from the stream into memory.
  31. Reads `count` bytes of data from the stream into either a raw memory pointer or a databuffer.
  32. Returns the number of bytes actually read.
  33. @param buf A pointer to the memory to read the data into.
  34. @param data The databuffer to read the data into.
  35. @param count The number of bytes to read from the stream.
  36. @return The number of bytes actually read.
  37. #end
  38. Method Read:Int( buf:Void Ptr,count:Int ) Abstract
  39. Method Read:Int( data:DataBuffer,offset:Int,count:Int )
  40. DebugAssert( offset>=0 And count>=0 And offset+count<=data.Length )
  41. Return Read( data.Data+offset,count )
  42. End
  43. #rem monkeydoc Writes data to the stream from memory.
  44. Writes `count` bytes of data to the stream from either a raw memory pointer or a databuffer.
  45. Returns the number of bytes actually written
  46. @param buf A pointer to the memory to write the data from.
  47. @param data The databuffer to write the data from.
  48. @param count The number of bytes to write to the stream.
  49. @return The number of bytes actually written.
  50. #end
  51. Method Write:Int( buf:Void Ptr,count:Int ) Abstract
  52. Method Write:Int( data:DataBuffer,offset:Int,count:Int )
  53. DebugAssert( offset>=0 And count>=0 And offset+count<=data.Length )
  54. Return Write( data.Data+offset,count )
  55. End
  56. #rem monkeydoc The byte order of the stream.
  57. The default byte order is ByteOrder.BigEndian.
  58. #end
  59. Property ByteOrder:ByteOrder()
  60. Return _tmpbuf.ByteOrder
  61. Setter( byteOrder:ByteOrder )
  62. _tmpbuf.ByteOrder=byteOrder
  63. End
  64. #rem monkeydoc Reads as many bytes as possible from a stream into memory.
  65. Continously reads data from a stream until either `count` bytes are read or the end of stream is reached.
  66. Returns the number of bytes read or the data read.
  67. @param buf memory to read bytes into.
  68. @param data data buffer to read bytes into.
  69. @param count number of bytes to read.
  70. #end
  71. Method ReadAll:Int( buf:Void Ptr,count:Int )
  72. Local pos:=0
  73. While pos<count
  74. Local n:=Read( Cast<Byte Ptr>( buf )+pos,count-pos )
  75. If n<=0 Exit
  76. pos+=n
  77. Wend
  78. Return pos
  79. End
  80. Method ReadAll:Int( data:DataBuffer,offset:Int,count:Int )
  81. Return ReadAll( data.Data+offset,count )
  82. End
  83. Method ReadAll:DataBuffer( count:Int )
  84. Local data:=New DataBuffer( count )
  85. Local n:=ReadAll( data,0,count )
  86. If n>=count Return data
  87. Local tmp:=data.Slice( 0,n )
  88. data.Discard()
  89. Return tmp
  90. End
  91. Method ReadAll:DataBuffer()
  92. If Length>=0 Return ReadAll( Length-Position )
  93. Local bufs:=New Stack<DataBuffer>
  94. Local buf:=New DataBuffer( 4096 ),pos:=0
  95. Repeat
  96. pos=ReadAll( buf,0,4096 )
  97. If pos<4096 Exit
  98. bufs.Push( buf )
  99. buf=New DataBuffer( 4096 )
  100. Forever
  101. Local len:=bufs.Length * 4096 + pos
  102. Local data:=New DataBuffer( len )
  103. pos=0
  104. For Local buf:=Eachin bufs
  105. buf.CopyTo( data,0,pos,4096 )
  106. buf.Discard()
  107. pos+=4096
  108. Next
  109. buf.CopyTo( data,0,pos,len-pos )
  110. buf.Discard()
  111. Return data
  112. End
  113. #rem monkeydoc Reads data from the filestream and throws it away.
  114. @param count The number of bytes to skip.
  115. @return The number of bytes actually skipped.
  116. #end
  117. Method Skip:Int( count:Int )
  118. Local tmp:=libc.malloc( count )
  119. Local n:=Read( tmp,count )
  120. libc.free( tmp )
  121. Return n
  122. End
  123. #rem monkeydoc Reads a byte from the stream.
  124. @return The byte read.
  125. #end
  126. Method ReadByte:Byte()
  127. If Read( _tmpbuf.Data,1 )=1 Return _tmpbuf.PeekByte( 0 )
  128. Return 0
  129. End
  130. #rem monkeydoc Reads an unsigned byte from the stream.
  131. @return The ubyte read.
  132. #end
  133. Method ReadUByte:UByte()
  134. If Read( _tmpbuf.Data,1 )=1 Return _tmpbuf.PeekUByte( 0 )
  135. Return 0
  136. End
  137. #rem monkeydoc Reads a 16 bit short from the stream.
  138. @return The short read.
  139. #end
  140. Method ReadShort:Short()
  141. If ReadAll( _tmpbuf.Data,2 )=2 Return _tmpbuf.PeekShort( 0 )
  142. Return 0
  143. End
  144. #rem monkeydoc Reads a 16 bit unsigned short from the stream.
  145. @return The ushort read.
  146. #end
  147. Method ReadUShort:UShort()
  148. If ReadAll( _tmpbuf.Data,2 )=2 Return _tmpbuf.PeekUShort( 0 )
  149. Return 0
  150. End
  151. #rem monkeydoc Reads a 32 bit int from the stream.
  152. @return The int read.
  153. #end
  154. Method ReadInt:Int()
  155. If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekInt( 0 )
  156. Return 0
  157. End
  158. #rem monkeydoc Reads a 32 bit unsigned int from the stream.
  159. @return The uint read.
  160. #end
  161. Method ReadUInt:UInt()
  162. If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekUInt( 0 )
  163. Return 0
  164. End
  165. #rem monkeydoc Reads a 32 bit long from the stream.
  166. @return The long read.
  167. #end
  168. Method ReadLong:Long()
  169. If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekLong( 0 )
  170. Return 0
  171. End
  172. #rem monkeydoc Reads a 32 bit unsigned long from the stream.
  173. @return The ulong read.
  174. #end
  175. Method ReadULong:ULong()
  176. If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekULong( 0 )
  177. Return 0
  178. End
  179. #rem monkeydoc Reads a 32 bit float from the stream.
  180. @return The float read.
  181. #end
  182. Method ReadFloat:Float()
  183. If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekFloat( 0 )
  184. Return 0
  185. End
  186. #rem monkeydoc Reads a 64 bit double from the stream.
  187. @return The double read.
  188. #end
  189. Method ReadDouble:Double()
  190. If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekDouble( 0 )
  191. Return 0
  192. End
  193. #rem monkeydoc Reads the entire stream into a string.
  194. #end
  195. Method ReadString:String()
  196. Local data:=ReadAll()
  197. Local str:=data.PeekString( 0 )
  198. data.Discard()
  199. Return str
  200. End
  201. #rem monkeydoc Reads a size prefixed string from the stream.
  202. Reads an int from the stream, then a string from that many bytes.
  203. @return the string read.
  204. #end
  205. Method ReadSizedString:String()
  206. Local n:=ReadInt()
  207. Local data:=ReadAll( n )
  208. Local str:=data.PeekString( 0 )
  209. data.Discard()
  210. Return str
  211. End
  212. #rem monkeydoc Reads a null terminated cstring from the stream.
  213. @return the string read.
  214. #end
  215. Method ReadCString:String()
  216. Local buf:=New Stack<Byte>
  217. While Not Eof
  218. Local chr:=ReadByte()
  219. If Not chr Exit
  220. buf.Push( chr )
  221. Wend
  222. Return String.FromCString( buf.Data.Data,buf.Length )
  223. End
  224. #rem monkeydoc Writes a byte to the stream.
  225. @param data The byte to write.
  226. #end
  227. Method WriteByte( data:Byte )
  228. _tmpbuf.PokeByte( 0,data )
  229. Write( _tmpbuf.Data,1 )
  230. End
  231. #rem monkeydoc Write an unsigned byte to the stream.
  232. @param data The ubyte to write.
  233. #end
  234. Method WriteUByte( data:UByte )
  235. _tmpbuf.PokeUByte( 0,data )
  236. Write( _tmpbuf.Data,1 )
  237. End
  238. #rem monkeydoc Writes a 16 bit short to the stream.
  239. @param data The short to write.
  240. #end
  241. Method WriteShort( data:Short )
  242. _tmpbuf.PokeShort( 0,data )
  243. Write( _tmpbuf.Data,2 )
  244. End
  245. #rem monkeydoc Writes a 16 bit unsigned short to the stream.
  246. @param data The ushort to write.
  247. #end
  248. Method WriteUShort( data:UShort )
  249. _tmpbuf.PokeUShort( 0,data )
  250. Write( _tmpbuf.Data,2 )
  251. End
  252. #rem monkeydoc Writes a 32 bit int to the stream.
  253. @param data The int to write.
  254. #end
  255. Method WriteInt( data:Int )
  256. _tmpbuf.PokeInt( 0,data )
  257. Write( _tmpbuf.Data,4 )
  258. End
  259. #rem monkeydoc Writes a 32 bit unsigned int to the stream.
  260. @param data The uint to write.
  261. #end
  262. Method WriteUInt( data:UInt )
  263. _tmpbuf.PokeUInt( 0,data )
  264. Write( _tmpbuf.Data,4 )
  265. End
  266. #rem monkeydoc Writes a 64 bit long to the stream.
  267. @param data The long to write.
  268. #end
  269. Method WriteLong( data:Long )
  270. _tmpbuf.PokeLong( 0,data )
  271. Write( _tmpbuf.Data,8 )
  272. End
  273. #rem monkeydoc Writes a 64 bit unsigned long to the stream.
  274. @param data The ulong to write.
  275. #end
  276. Method WriteULong( data:ULong )
  277. _tmpbuf.PokeULong( 0,data )
  278. Write( _tmpbuf.Data,8 )
  279. End
  280. #rem monkeydoc Writes a 32 bit float to the stream,
  281. @param data The float to write.
  282. #end
  283. Method WriteFloat:Void( data:Float )
  284. _tmpbuf.PokeFloat( 0,data )
  285. Write( _tmpbuf.Data,4 )
  286. End
  287. #rem monkeydoc Writes a 64 bit double to the stream.
  288. @param data The double to write.
  289. #end
  290. Method WriteDouble( data:Double )
  291. _tmpbuf.PokeDouble( 0,data )
  292. Write( _tmpbuf.Data,8 )
  293. End
  294. #rem monkeydoc Writes a string to the stream (NOT null terminated).
  295. @param str The string to write.
  296. #end
  297. Method WriteString( str:String )
  298. Local buf:=New DataBuffer( str.CStringLength )
  299. buf.PokeString( 0,str )
  300. Write( buf,0,buf.Length )
  301. buf.Discard()
  302. End
  303. #rem monkeydoc Writes a size prefixed string to the stream.
  304. Writes an int containing the size of the string to the stream, followed the string itself.
  305. #end
  306. Method WriteSizedString( str:String )
  307. WriteInt( str.CStringLength )
  308. WriteString( str )
  309. End
  310. #rem monkeydoc Writes a null terminated cstring to the stream.
  311. @param str The string to write.
  312. #end
  313. Method WriteCString( str:String )
  314. WriteString( str )
  315. WriteByte( 0 )
  316. End
  317. #rem monkeydoc Opens a stream
  318. `mode` should be "r" for read, "w" for write or "rw" for read/write.
  319. @param mode The mode to open the stream in: "r", "w" or "rw"
  320. #end
  321. Function Open:Stream( path:String,mode:String )
  322. Local i:=path.Find( "::" )
  323. If i=-1 Return FileStream.Open( path,mode )
  324. Local proto:=path.Slice( 0,i )
  325. Local ipath:=path.Slice( i+2 )
  326. Return OpenFuncs[proto]( proto,ipath,mode )
  327. End
  328. #rem monkeydoc @hidden
  329. #end
  330. Alias OpenFunc:Stream( proto:String,path:String,mode:String )
  331. #rem monkeydoc @hidden
  332. #end
  333. Const OpenFuncs:=New StringMap<OpenFunc>
  334. Protected
  335. Method New()
  336. _tmpbuf=New DataBuffer( 8,ByteOrder.LittleEndian )
  337. End
  338. Method OnClose() Abstract
  339. Private
  340. Field _tmpbuf:DataBuffer
  341. End