Ooops
November 30, 2020, 02:07:41 AM

Author Topic: [bmx] ProgressStream by Byteemoz [ 1+ years ago ]  (Read 611 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] ProgressStream by Byteemoz [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : ProgressStream
Author : Byteemoz
Posted : 1+ years ago

Description : A simple demo:
Code: [Select]
Import "ProgressStream.bmx"

' get filename
file$ = RequestFile("Open big file...")
If file = "" End

' use the progress stream
file = "progress::" + file

' set the stream hook
SetProgressStreamHook Hook, 500 ' Hook is being called every 500ms

' start message
Print "Loading file..."

' a simple loading function
Local stream:TStream = ReadStream(file)
While Not stream.Eof()
stream.ReadByte()
Wend
stream.close

' end message
Print "Done"


' hook function
Function Hook(Stream:TStream, Read:Int, Written:Int, Context:Object)
Local size:Int = Stream.Size()
If size = 0 Then Print "Progress ??? (" + Read + " Bytes)"
Local percent = 100.0 * Read / size
Print "Progress " + percent + "% (" + Read + " Bytes)"
EndFunction


Code :
Code: BlitzMax
  1. ' ProgressStream.bmx
  2.  
  3. SuperStrict
  4.  
  5. Import BRL.Stream
  6.  
  7. Type TProgressStream Extends TStreamWrapper
  8.  
  9.         Field _readcount:Int = 0
  10.         Field _writecount:Int = 0
  11.         Field _lasthook:Int = 0
  12.  
  13.         Method Close()
  14.                 SetStream Null
  15.         End Method
  16.  
  17.         Function Create:TProgressStream( stream:TStream )
  18.                 Local t:TProgressStream = New TProgressStream
  19.                 t.SetStream stream
  20.                 Return t
  21.         End Function
  22.  
  23.         Method Read:Int( buf:Byte Ptr,count:Int )
  24.                 _readcount:+ count ; Progress()
  25.                 Return _stream.Read( buf, count )
  26.         End Method
  27.  
  28.         Method Write:Int( buf:Byte Ptr,count:Int )
  29.                 _writecount:+ count ; Progress()
  30.                 Return _stream.Write( buf,count )
  31.         End Method
  32.        
  33.         Method ReadByte:Int()
  34.                 _readcount:+ 1 ; Progress()
  35.                 Return _stream.ReadByte()
  36.         End Method
  37.        
  38.         Method WriteByte( n:Int )
  39.                 _writecount:+ 1 ; Progress()
  40.                 _stream.WriteByte n
  41.         End Method
  42.        
  43.         Method ReadShort:Int()
  44.                 _readcount:+ 2 ; Progress()
  45.                 Return _stream.ReadShort()
  46.         End Method
  47.        
  48.         Method WriteShort( n:Int )
  49.                 _writecount:+ 2 ; Progress()
  50.                 _stream.WriteShort n
  51.         End Method
  52.        
  53.         Method ReadInt:Int()
  54.                 _readcount:+ 4 ; Progress()
  55.                 Return _stream.ReadInt()
  56.         End Method
  57.        
  58.         Method WriteInt( n:Int )
  59.                 _writecount:+ 4 ; Progress()
  60.                 _stream.WriteInt n
  61.         End Method
  62.        
  63.         Method ReadFloat:Float()
  64.                 _readcount:+ 4 ; Progress()
  65.                 Return _stream.ReadFloat()
  66.         End Method
  67.        
  68.         Method WriteFloat( n:Float )
  69.                 _writecount:+ 4 ; Progress()
  70.                 _stream.WriteFloat n
  71.         End Method
  72.        
  73.         Method ReadDouble:Double()
  74.                 _readcount:+ 8 ; Progress()
  75.                 Return _stream.ReadDouble()
  76.         End Method
  77.        
  78.         Method WriteDouble( n:Double )
  79.                 _writecount:+ 8 ; Progress()
  80.                 _stream.WriteDouble n
  81.         End Method
  82.        
  83.         Method ReadLong:Long()
  84.                 _readcount:+ 8 ; Progress()
  85.                 Return _stream.ReadLong()
  86.         End Method
  87.        
  88.         Method WriteLong( n:Long )
  89.                 _writecount:+ 8 ; Progress()
  90.                 _stream.WriteLong n
  91.         End Method
  92.        
  93.         Method ReadLine$()
  94.                 Local t$ = _stream.ReadLine()
  95.                 _readcount:+ t.Length ; Progress()
  96.                 Return t$
  97.         End Method
  98.        
  99.         Method WriteLine:Int( t$ )
  100.                 _writecount:+ t.Length ; Progress()
  101.                 Return _stream.WriteLine( t )
  102.         End Method
  103.        
  104.         Method ReadString$( n:Int )
  105.                 _readcount:+ n ; Progress()
  106.                 Return _stream.ReadString( n )
  107.         End Method
  108.        
  109.         Method WriteString( t$ )
  110.                 _writecount:+ t.Length ; Progress()
  111.                 _stream.WriteString t
  112.         End Method
  113.        
  114.         Method Progress()
  115.                 If MilliSecs() - _lasthook < ProgressInterval Then Return
  116.                 ProgressHook _stream, _readcount, _writecount, ProgressContext
  117.                 _lasthook = MilliSecs()
  118.         EndMethod
  119. EndType
  120.  
  121. Function SetProgressStreamHook(Hook(Stream:TStream, Read:Int, Written:Int, Context:Object), Interval:Int = 100, Context:Object = Null)
  122.         ProgressHook = Hook
  123.         ProgressInterval = Interval
  124.         ProgressContext = Context
  125. EndFunction
  126.  
  127. Private
  128.        
  129. Type TProgressStreamFactory Extends TStreamFactory
  130.  
  131.         Method CreateStream:TStream(URL:Object, Proto:String, Path:String, Readable:Int, Writeable:Int)
  132.                 If Proto = "progress" Then
  133.                         Local stream:TStream=OpenStream(path,readable,writeable)
  134.                         If stream Return TProgressStream.Create(stream)
  135.                 EndIf
  136.         End Method
  137. End Type
  138.  
  139. New TProgressStreamFactory
  140.  
  141. Function NullProgressHook(Stream:TStream, Read:Int, Written:Int, Context:Object) ; EndFunction
  142. Global ProgressHook(Stream:TStream, Read:Int, Written:Int, Context:Object) = NullProgressHook
  143. Global ProgressInterval:Int = 100
  144. Global ProgressContext:Object = Null


Comments :


Streaksy(Posted 1+ years ago)

 woa...would this work with things like LoadImage ?


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal