bankstream.bmx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. Strict
  2. Rem
  3. bbdoc: Streams/Bank streams
  4. End Rem
  5. Module BRL.BankStream
  6. ModuleInfo "Version: 1.01"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: Added TBankStreamFactory"
  12. Import BRL.Bank
  13. Import BRL.Stream
  14. Rem
  15. bbdoc: BankStream Object
  16. End Rem
  17. Type TBankStream Extends TStream
  18. Field _pos,_bank:TBank
  19. Method Pos()
  20. Return _pos
  21. End Method
  22. Method Size()
  23. Return _bank.Size()
  24. End Method
  25. Method Seek( pos )
  26. If pos<0 pos=0 Else If pos>_bank.Size() pos=_bank.Size()
  27. _pos=pos
  28. Return _pos
  29. End Method
  30. Method Read( buf:Byte Ptr,count )
  31. If count<=0 Or _pos>=_bank.Size() Return 0
  32. If _pos+count>_bank.Size() count=_bank.Size()-_pos
  33. MemCopy buf,_bank.Buf()+_pos,count
  34. _pos:+count
  35. Return count
  36. End Method
  37. Method Write( buf:Byte Ptr,count )
  38. If count<=0 Or _pos>_bank.Size() Return 0
  39. If _pos+count>_bank.Size() _bank.Resize _pos+count
  40. MemCopy _bank.Buf()+_pos,buf,count
  41. _pos:+count
  42. Return count
  43. End Method
  44. Rem
  45. bbdoc: Create a bank stream
  46. returns: A bank stream object
  47. about:
  48. A bank stream allows you to read data into or out of a bank. A bank stream extends a stream so
  49. can be used in place of a stream.
  50. end rem
  51. Function Create:TBankStream( bank:TBank )
  52. Local stream:TBankStream=New TBankStream
  53. stream._bank=bank
  54. Return stream
  55. End Function
  56. End Type
  57. Rem
  58. bbdoc: Create a bank stream
  59. returns: A bank stream object
  60. about:
  61. A bank stream allows you to read data into or out of a bank. A bank stream extends a stream so
  62. can be used in place of a stream.
  63. end rem
  64. Function CreateBankStream:TBankStream( bank:TBank )
  65. If Not bank bank=TBank.Create(0)
  66. Return TBankStream.Create( bank )
  67. End Function
  68. Type TBankStreamFactory Extends TStreamFactory
  69. Method CreateStream:TBankStream( url:Object,proto$,path$,readable,writeable )
  70. Local bank:TBank=TBank(url)
  71. If bank Return CreateBankStream( bank )
  72. End Method
  73. End Type
  74. New TBankStreamFactory