[bmx] StreamBank by N [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : StreamBank
Author : N
Posted : 1+ years ago

Description : Basically, this lets you treat streams the same was as you would a bank.  It's surprisingly handy.

However, it should probably be noted that I do zero error checking in this code.  If you find yourself getting exceptions, you'll probably want to check that you're not reading from/writing to some offset into the stream that just doesn't exist.  I've never had any problems but I do realize there are people out there who expect bizarre things like the aforementioned to magically work.


Code :
Code (blitzmax) Select
SuperStrict

Import Brl.Bank
Import Brl.Stream

Public
Type IStreamBank Extends TBank
    Field _proxy:TStream
   
    Method Delete( )
        _proxy = Null
    End Method
   
    Method Resize( size% )
        Throw "Cannot resize IStreamBanks"
    End Method
   
    Method PeekByte%( idx% )
        _proxy.Seek( idx )
        Return _proxy.ReadByte( )
    End Method
   
    Method PeekShort%( idx% )
        _proxy.Seek( idx )
        Return _proxy.ReadShort( )
    End Method
   
    Method PeekInt%( idx% )
        _proxy.Seek( idx )
        Return _proxy.ReadInt( )
    End Method
   
    Method PeekLong:Long( idx% )
        _proxy.Seek( idx )
        Return _proxy.ReadLong( )
    End Method
   
    Method PeekFloat#( idx% )
        _proxy.Seek( idx )
        Return _proxy.ReadFloat( )
    End Method
   
    Method PeekDouble!( idx% )
        _proxy.Seek( idx )
        Return _proxy.ReadDouble( )
    End Method
   
    Method PokeByte( idx%, value% )
        _proxy.Seek( idx )
        _proxy.WriteByte( value )
    End Method
   
    Method PokeShort( idx%, value% )
        _proxy.Seek( idx )
        _proxy.WriteShort( value )
    End Method
   
    Method PokeInt( idx%, value% )
        _proxy.Seek( idx )
        _proxy.WriteInt( value )
    End Method
   
    Method PokeLong( idx%, value:Long )
        _proxy.Seek( idx )
        _proxy.WriteLong( value )
    End Method
   
    Method PokeFloat( idx%, value# )
        _proxy.Seek( idx )
        _proxy.WriteFloat( value )
    End Method
   
    Method PokeDouble( idx%, value! )
        _proxy.Seek( idx )
        _proxy.WriteDouble( value )
    End Method
   
    Method Size%( )
        Return _proxy.Size( )
    End Method
   
    Method PeekBytes@ Ptr( into@ Ptr, off%, size% )
        _proxy.Seek( off )
        _proxy.ReadBytes( into, size )
    End Method
   
    Method PokeBytes( from@ Ptr, off%, size% )
        _proxy.Seek( off )
        _proxy.WriteBytes( from, size )
    End Method
   
    Method Buf@ Ptr( )
        Return Null
    End Method
End Type

Function CreateStreamBank:TBank( stream:TStream )
    Local o:IStreamBank = New IStreamBank
    o._proxy = stream
    Return o
End Function


Comments : none...