stream.monkey2 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 Extends std.resource.Resource
  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. End
  24. #rem monkeydoc Seeks to a position in the stream.
  25. In debug builds, a runtime error will occur if the stream is not seekable or `position` is out of range.
  26. @param position The position to seek to.
  27. #end
  28. Method Seek( position:Int ) Abstract
  29. #rem monkeydoc Reads data from the stream into memory.
  30. Reads `count` bytes of data from the stream into either a raw memory pointer or a databuffer.
  31. Returns the number of bytes actually read.
  32. @param buf A pointer to the memory to read the data into.
  33. @param data The databuffer to read the data into.
  34. @param count The number of bytes to read from the stream.
  35. @return The number of bytes actually read.
  36. #end
  37. Method Read:Int( buf:Void Ptr,count:Int ) Abstract
  38. Method Read:Int( data:DataBuffer,offset:Int,count:Int )
  39. DebugAssert( offset>=0 And count>=0 And offset+count<=data.Length )
  40. Return Read( data.Data+offset,count )
  41. End
  42. #rem monkeydoc Writes data to the stream from memory.
  43. Writes `count` bytes of data to the stream from either a raw memory pointer or a databuffer.
  44. Returns the number of bytes actually written
  45. @param buf A pointer to the memory to write the data from.
  46. @param data The databuffer to write the data from.
  47. @param count The number of bytes to write to the stream.
  48. @return The number of bytes actually written.
  49. #end
  50. Method Write:Int( buf:Void Ptr,count:Int ) Abstract
  51. Method Write:Int( data:DataBuffer,offset:Int,count:Int )
  52. DebugAssert( offset>=0 And count>=0 And offset+count<=data.Length )
  53. Return Write( data.Data+offset,count )
  54. End
  55. #rem monkeydoc The byte order of the stream.
  56. The default byte order is ByteOrder.LittleEndian.
  57. #end
  58. Property ByteOrder:ByteOrder()
  59. Return _swap ? ByteOrder.BigEndian Else ByteOrder.LittleEndian
  60. Setter( byteOrder:ByteOrder )
  61. _swap=(byteOrder=ByteOrder.BigEndian)
  62. End
  63. #rem monkeydoc Reads as many bytes as possible from a stream into memory.
  64. Continously reads data from a stream until either `count` bytes are read or the end of stream is reached.
  65. Returns the number of bytes read or the data read.
  66. @param buf memory to read bytes into.
  67. @param data data buffer to read bytes into.
  68. @param count number of bytes to read.
  69. #end
  70. Method ReadAll:Int( buf:Void Ptr,count:Int )
  71. Local pos:=0
  72. While pos<count
  73. Local n:=Read( Cast<UByte Ptr>( buf )+pos,count-pos )
  74. If n<=0 Exit
  75. pos+=n
  76. Wend
  77. Return pos
  78. End
  79. Method ReadAll:Int( data:DataBuffer,offset:Int,count:Int )
  80. Return ReadAll( data.Data+offset,count )
  81. End
  82. Method ReadAll:DataBuffer( count:Int )
  83. Local data:=New DataBuffer( count )
  84. Local n:=ReadAll( data,0,count )
  85. If n=count Return data
  86. Local tmp:=data.Slice( 0,n )
  87. data.Discard()
  88. Return tmp
  89. End
  90. Method ReadAll:DataBuffer()
  91. If Length>=0 Return ReadAll( Length-Position )
  92. Local bufs:=New Stack<DataBuffer>
  93. Local buf:=New DataBuffer( 4096 ),pos:=0
  94. Repeat
  95. pos=ReadAll( buf,0,4096 )
  96. If pos<4096 Exit
  97. bufs.Push( buf )
  98. buf=New DataBuffer( 4096 )
  99. Forever
  100. Local len:=bufs.Length * 4096 + pos
  101. Local data:=New DataBuffer( len )
  102. pos=0
  103. For Local buf:=Eachin bufs
  104. buf.CopyTo( data,0,pos,4096 )
  105. buf.Discard()
  106. pos+=4096
  107. Next
  108. buf.CopyTo( data,0,pos,len-pos )
  109. buf.Discard()
  110. Return data
  111. End
  112. #rem monkeydoc Reads data from the stream and throws it away.
  113. @param count The number of bytes to skip.
  114. @return The number of bytes actually skipped.
  115. #end
  116. Method Skip:Int( count:Int )
  117. If count<=0 Return 0
  118. Local tmp:=libc.malloc( 4096 ),n:=0
  119. While n<count
  120. Local t:=Read( tmp,Min( count-n,4096 ) )
  121. If t<=0 Exit
  122. n+=t
  123. Wend
  124. libc.free( tmp )
  125. Return n
  126. End
  127. #rem monkeydoc Reads a byte from the stream.
  128. @return The byte read.
  129. #end
  130. Method ReadByte:Byte()
  131. Local n:Byte
  132. Read( Varptr n,1 )
  133. Return n
  134. End
  135. #rem monkeydoc Reads an unsigned byte from the stream.
  136. @return The ubyte read.
  137. #end
  138. Method ReadUByte:UByte()
  139. Local n:UByte
  140. Read( Varptr n,1 )
  141. Return n
  142. End
  143. #rem monkeydoc Reads a 16 bit short from the stream.
  144. @return The short read.
  145. #end
  146. Method ReadShort:Short()
  147. Local n:Short
  148. If Read( Varptr n,2 )<>2 n=0
  149. If _swap Swap2( Varptr n )
  150. Return n
  151. End
  152. #rem monkeydoc Reads a 16 bit unsigned short from the stream.
  153. @return The ushort read.
  154. #end
  155. Method ReadUShort:UShort()
  156. Local n:UShort
  157. If Read( Varptr n,2 )<>2 n=0
  158. If _swap Swap2( Varptr n )
  159. Return n
  160. End
  161. #rem monkeydoc Reads a 32 bit int from the stream.
  162. @return The int read.
  163. #end
  164. Method ReadInt:Int()
  165. Local n:Int
  166. If Read( Varptr n,4 )<>4 n=0
  167. If _swap Swap4( Varptr n )
  168. Return n
  169. End
  170. #rem monkeydoc Reads a 32 bit unsigned int from the stream.
  171. @return The uint read.
  172. #end
  173. Method ReadUInt:UInt()
  174. Local n:UInt
  175. If Read( Varptr n,4 )<>4 n=0
  176. If _swap Swap4( Varptr n )
  177. Return n
  178. End
  179. #rem monkeydoc Reads a 32 bit long from the stream.
  180. @return The long read.
  181. #end
  182. Method ReadLong:Long()
  183. Local n:Long
  184. If Read( Varptr n,8 )<>8 n=0
  185. If _swap Swap8( Varptr n )
  186. Return n
  187. End
  188. #rem monkeydoc Reads a 32 bit unsigned long from the stream.
  189. @return The ulong read.
  190. #end
  191. Method ReadULong:ULong()
  192. Local n:ULong
  193. If Read( Varptr n,8 )<>8 n=0
  194. If _swap Swap8( Varptr n )
  195. Return n
  196. End
  197. #rem monkeydoc Reads a 32 bit float from the stream.
  198. @return The float read.
  199. #end
  200. Method ReadFloat:Float()
  201. Local n:Float
  202. If Read( Varptr n,4 )<>4 n=0
  203. If _swap Swap4( Varptr n )
  204. Return n
  205. End
  206. #rem monkeydoc Reads a 64 bit double from the stream.
  207. @return The double read.
  208. #end
  209. Method ReadDouble:Double()
  210. Local n:Double
  211. If Read( Varptr n,8 )<>8 n=0
  212. If _swap Swap8( Varptr n )
  213. Return n
  214. End
  215. #rem monkeydoc Reads the entire stream into a string.
  216. #end
  217. Method ReadString:String()
  218. Local data:=ReadAll()
  219. Local str:=data.PeekString( 0 )
  220. data.Discard()
  221. Return str
  222. End
  223. #rem monkeydoc Reads a size prefixed string from the stream.
  224. Reads an int from the stream, then a string from that many bytes.
  225. @return the string read.
  226. #end
  227. Method ReadSizedString:String()
  228. Local n:=ReadInt()
  229. Local data:=ReadAll( n )
  230. Local str:=data.PeekString( 0 )
  231. data.Discard()
  232. Return str
  233. End
  234. #rem monkeydoc Reads a null terminated cstring from the stream.
  235. @return the string read.
  236. #end
  237. Method ReadCString:String()
  238. Local buf:=New Stack<Byte>
  239. While Not Eof
  240. Local chr:=ReadByte()
  241. If Not chr Exit
  242. buf.Push( chr )
  243. Wend
  244. Return String.FromCString( buf.Data.Data,buf.Length )
  245. End
  246. #rem monkeydoc Reads a line of text from the stream.
  247. Bytes are read from the stream until a newline character (ascii code 10) or null character (ascii code 0) is read, or end of file is detected.
  248. The bytes read are returned in the form of a string, excluding any terminating newline or null character.
  249. Carriage return characters (ascii code 13) are silently ignored.
  250. #end
  251. Method ReadLine:String()
  252. Local buf:=New Stack<Byte>
  253. While Not Eof
  254. Local chr:=ReadByte()
  255. If Not chr Or chr=10 exit
  256. If chr=13 Continue
  257. buf.Push( chr )
  258. Wend
  259. Return String.FromCString( buf.Data.Data,buf.Length )
  260. End
  261. #rem monkeydoc Writes a byte to the stream.
  262. @param data The byte to write.
  263. #end
  264. Method WriteByte( data:Byte )
  265. Write( Varptr data,1 )
  266. End
  267. #rem monkeydoc Write an unsigned byte to the stream.
  268. @param data The ubyte to write.
  269. #end
  270. Method WriteUByte( data:UByte )
  271. Write( Varptr data,1 )
  272. End
  273. #rem monkeydoc Writes a 16 bit short to the stream.
  274. @param data The short to write.
  275. #end
  276. Method WriteShort( data:Short )
  277. If _swap Swap2( Varptr data )
  278. Write( Varptr data,2 )
  279. End
  280. #rem monkeydoc Writes a 16 bit unsigned short to the stream.
  281. @param data The ushort to write.
  282. #end
  283. Method WriteUShort( data:UShort )
  284. If _swap Swap2( Varptr data )
  285. Write( Varptr data,2 )
  286. End
  287. #rem monkeydoc Writes a 32 bit int to the stream.
  288. @param data The int to write.
  289. #end
  290. Method WriteInt( data:Int )
  291. If _swap Swap4( Varptr data )
  292. Write( Varptr data,4 )
  293. End
  294. #rem monkeydoc Writes a 32 bit unsigned int to the stream.
  295. @param data The uint to write.
  296. #end
  297. Method WriteUInt( data:UInt )
  298. If _swap Swap4( Varptr data )
  299. Write( Varptr data,4 )
  300. End
  301. #rem monkeydoc Writes a 64 bit long to the stream.
  302. @param data The long to write.
  303. #end
  304. Method WriteLong( data:Long )
  305. If _swap Swap8( Varptr data )
  306. Write( Varptr data,8 )
  307. End
  308. #rem monkeydoc Writes a 64 bit unsigned long to the stream.
  309. @param data The ulong to write.
  310. #end
  311. Method WriteULong( data:ULong )
  312. If _swap Swap8( Varptr data )
  313. Write( Varptr data,8 )
  314. End
  315. #rem monkeydoc Writes a 32 bit float to the stream,
  316. @param data The float to write.
  317. #end
  318. Method WriteFloat:Void( data:Float )
  319. If _swap Swap4( Varptr data )
  320. Write( Varptr data,4 )
  321. End
  322. #rem monkeydoc Writes a 64 bit double to the stream.
  323. @param data The double to write.
  324. #end
  325. Method WriteDouble( data:Double )
  326. If _swap Swap8( Varptr data )
  327. Write( Varptr data,8 )
  328. End
  329. #rem monkeydoc Writes a string to the stream (NOT null terminated).
  330. @param str The string to write.
  331. #end
  332. Method WriteString( str:String )
  333. Local buf:=New DataBuffer( str.CStringLength )
  334. buf.PokeString( 0,str )
  335. Write( buf,0,buf.Length )
  336. buf.Discard()
  337. End
  338. #rem monkeydoc Writes a size prefixed string to the stream.
  339. Writes an int containing the size of the string to the stream, followed the string itself.
  340. #end
  341. Method WriteSizedString( str:String )
  342. WriteInt( str.CStringLength )
  343. WriteString( str )
  344. End
  345. #rem monkeydoc Writes a null terminated cstring to the stream.
  346. @param str The string to write.
  347. #end
  348. Method WriteCString( str:String )
  349. WriteString( str )
  350. WriteByte( 0 )
  351. End
  352. #rem monkeydoc Writes a line of text to the stream.
  353. Writes the characters in `str` followed by the line terminating sequence "~r~n".
  354. #end
  355. Method WriteLine( str:String )
  356. WriteString( str )
  357. WriteString( "~r~n" )
  358. End
  359. #rem monkeydoc Opens a stream
  360. `mode` should be "r" for read, "w" for write or "rw" for read/write.
  361. If the stream could not be opened, null will be returned.
  362. When opening a file using "r" or "rw", the file must already exist or the function will fail and null will be returned.
  363. When opening a file using "w", any existing file at the same path will be overwritten.
  364. Stream paths may include the following prefixes:
  365. | Stream path prefix | Supported targets | Description
  366. |:----------------------|:------------------|:-----------
  367. | `asset::` | All | Open a stream for reading an app asset.
  368. | `internal::` | Mobile | Open a stream for reading/writing internal app storage.
  369. | `external::` | Android | Open a stream for reading/writing external app storage.
  370. | `home::` | Desktop | Open a stream for reading/writing a file in the user's home directory.
  371. | `desktop::` | Desktop | Open a stream for reading/writing a file in the user's desktop directory.
  372. @param mode The mode to open the stream in: "r", "w" or "rw"
  373. #end
  374. Function Open:Stream( path:String,mode:String )
  375. Local i:=path.Find( "::" )
  376. If i=-1 Return FileStream.Open( path,mode )
  377. Local proto:=path.Slice( 0,i )
  378. Local ipath:=path.Slice( i+2 )
  379. Return OpenFuncs[proto]( proto,ipath,mode )
  380. End
  381. #rem monkeydoc Stream open function type
  382. #end
  383. Alias OpenFunc:Stream( proto:String,path:String,mode:String )
  384. #rem monkeydoc Stream open functions map
  385. #end
  386. Const OpenFuncs:=New StringMap<OpenFunc>
  387. Protected
  388. Method New()
  389. _swap=false
  390. End
  391. Method OnClose() Virtual
  392. Discard()
  393. End
  394. Private
  395. Field _swap:Bool
  396. Function Swap2( v:Void Ptr )
  397. Local t:=Cast<UShort Ptr>( v )[0]
  398. Cast<UShort Ptr>( v )[0]=(t Shr 8 & $ff) | (t & $ff) Shl 8
  399. End
  400. Function Swap4( v:Void Ptr )
  401. Local t:=Cast<UInt Ptr>( v )[0]
  402. Cast<UInt Ptr>( v )[0]=(t Shr 24 & $ff) | (t & $ff) Shl 24 | (t Shr 8 & $ff00) | (t & $ff00) Shl 8
  403. End
  404. Function Swap8( v:Void Ptr )
  405. Local t:=Cast<ULong Ptr>( v )[0]
  406. Cast<ULong Ptr>( v )[0]=(t Shr 56 & $ff) | (t & $ff) Shl 56 | (t Shr 40 & $ff00) | (t & $ff00) Shl 40 | (t Shr 24 & $ff0000) | (t & $ff0000) Shl 24 | (t Shr 8 & $ff000000) | (t & $ff000000) Shl 8
  407. End
  408. End