December 04, 2020, 11:25:12 AM

Author Topic: [bb] Read/Write Progress Meters by _PJ_ [ 1+ years ago ]  (Read 401 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Read/Write Progress Meters by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Read/Write Progress Meters
Author : _PJ_
Posted : 1+ years ago

Description : Substituting for Read/Wrirte/Peek or Poke etc. these functions allow for the progress of reading/writing to be measured and potentially displayed on-screen or whatever else may be required.

The functions themselves due to the calculations and byte-by-byte nature can be rather slow, so it's not really recommended for use as-is in efficient projects, however, they should serve as a working example of how the functionality can be used in one's own work.


Code :
Code: BlitzBasic
  1. Global nPROGRESS_CURRENT_BYTES%
  2. Global nPROGRESS_MAXIMUM_BYTES%
  3.  
  4. Global nPROGRESS_DIR_MAXIMUM_BYTES%
  5. Global nPROGRESS_DIR_CURRENT_BYTES%
  6.  
  7.  
  8. Global nPROGRESS_BANK_MAXIMUM_BYTES%
  9. Global nPROGRESS_BANK_CURRENT_BYTES%
  10.  
  11. ; Example:
  12.         Graphics 1024,768,32,6
  13.         SetBuffer BackBuffer()
  14.         Global sPath$=GetEnv("ProgramFiles")+"Blitz3D"
  15.         If Right(sPath,1)<>"" Then sPath=sPath+""
  16.         Local hDir=hReadProgressDir(sPath)
  17.         Local sFileName$=NextFile(hDir)
  18.        
  19.                 While (sFileName<>"")
  20.                         If (Right(sFileName,1)<>".")
  21.                                 hFile=hReadFileProgress(sPath+sFileName)
  22.                                 If (hFile)
  23.                                         While (Not (Eof(hFile)))
  24.                                                 nReadByteProgress(hFile)
  25.                                                
  26.                                                 Cls
  27.                                                
  28.                                                 DisplayProgressBar(0,0,256,32,nProgressDir(),sPath+" ("+Str(nProgressDir())+"%)")
  29.                                                 DisplayProgressBar(0,128,256,32,nProgress(),sFIleName+" ("+Str(nProgress)+"%)")                        
  30.                                                 Flip
  31.                                                 If (bProgressDirComplete())
  32.                                                         CloseFile hFile
  33.                                                         Exit
  34.                                                 End If
  35.                                         Wend
  36.                                 End If
  37.                         End If
  38.                         sFileName$=NextFile(hDir)
  39.                         If (bProgressDirComplete()) Then Exit
  40.                 Wend
  41.         CloseDir hDir
  42.                
  43. ;Functions     
  44.        
  45. Function SetByteLimit(nLimit%)
  46.         nPROGRESS_MAXIMUM_BYTES=nLimit
  47. End Function
  48.  
  49. Function SetDirByteLimit(nLimit%)
  50.         nPROGRESS_DIR_MAXIMUM_BYTES=nLimit
  51. End Function
  52.  
  53. Function SetBankByteLimit(nLimit%)
  54.         nPROGRESS_BANK_MAXIMUM_BYTES=nLimit
  55. End Function
  56.  
  57. Function SetProgress(nBytes%)
  58.         nPROGRESS_CURRENT_BYTES=nBytes
  59. End Function
  60.  
  61. Function SetProgressPercent%(fPercent#)
  62.         nPROGRESS_CURRENT_BYTES=Int((Float(nPROGRESS_MAXIMUM_BYTES)*0.01*fPercent))
  63.         Return nPROGRESS_CURRENT_BYTES
  64. End Function
  65.  
  66. Function SetProgressDir(nBytes%)
  67.         nPROGRESS_DIR_CURRENT_BYTES=nBytes
  68. End Function
  69.  
  70. Function SetProgressDirPercent%(fPercent#)
  71.         nPROGRESS_DIR_CURRENT_BYTES=Int((Float(nPROGRESS_DIR_MAXIMUM_BYTES)*0.01*fPercent))
  72.         Return nPROGRESS_DIR_CURRENT_BYTES
  73. End Function
  74.  
  75. Function SetProgressBank(nBytes%)
  76.         nPROGRESS_BANK_CURRENT_BYTES=nBytes
  77. End Function
  78.  
  79. Function SetProgressBankPercent%(fPercent#)
  80.         nPROGRESS_BANK_CURRENT_BYTES=Int((Float(nPROGRESS_BANK_MAXIMUM_BYTES)*0.01*fPercent))
  81.         Return nPROGRESS_BANK_CURRENT_BYTES
  82. End Function
  83.  
  84. ;--
  85.  
  86. Function hCreateBankProgress(nBankSize%)
  87.         Local hReturn%=CreateBank(nBankSize)
  88.         nPROGRESS_BANK_MAXIMUM_BYTES=nBankSize
  89.         nPROGRESS_BANK_CURRENT_BYTES=0
  90. End Function
  91.        
  92. Function ResizeBankProgress(hBank,nSize%)
  93.         nPROGRESS_BANK_MAXIMUM_BYTES=nSize
  94.         ResizeBank hBank,nSize
  95. End Function
  96.  
  97. Function ResetProgress()
  98.         nPROGRESS_CURRENT_BYTES=0
  99. End Function
  100.  
  101. Function ResetProgressDir()
  102.         nPROGRESS_DIR_CURRENT_BYTES=0
  103. End Function
  104.  
  105. Function ResetProgressBank()
  106.         nPROGRESS_BANK_CURRENT_BYTES=0
  107. End Function
  108.  
  109. Function ProgressSkip()
  110.         nPROGRESS_CURRENT_BYTES=nPROGRESS_MAXIMUM_BYTES
  111. End Function
  112.  
  113. Function ProgressDirSkip()
  114.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_MAXIMUM_BYTES
  115. End Function
  116.  
  117. Function ProgressBankSkip()
  118.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_MAXIMUM_BYTES
  119. End Function
  120.  
  121. Function bProgressComplete%()
  122.         Return (nPROGRESS_CURRENT_BYTES>=nPROGRESS_MAXIMUM_BYTES)
  123. End Function
  124.  
  125. Function bProgressDirComplete%()
  126.         Return (nPROGRESS_DIR_CURRENT_BYTES>=nPROGRESS_DIR_MAXIMUM_BYTES)
  127. End Function
  128.  
  129. Function bProgressBankComplete%()
  130.         Return (nPROGRESS_BANK_CURRENT_BYTES>=nPROGRESS_BANK_MAXIMUM_BYTES)
  131. End Function
  132.  
  133. ;--
  134.  
  135. Function hReadProgressDir%(sDirPath$)
  136.         nPROGRESS_DIR_CURRENT_BYTES=0
  137.         nPROGRESS_DIR_MAXIMUM_BYTES=0
  138.         Local hReturn%=0
  139.         If (FileType(sDirPath)=2)
  140.                 hReturn=ReadDir(sDirpath)              
  141.                 Local sFile$=NextFile(hReturn)
  142.                 While (sFile<>"")
  143.                         If (Right(sFile,1)<>".")
  144.                                 nPROGRESS_DIR_MAXIMUM_BYTES=nPROGRESS_DIR_MAXIMUM_BYTES+FileSize(sDirPath+sFile)
  145.                         End If
  146.                         sFile=NextFile(hReturn)
  147.                 Wend
  148.                 CloseDir hReturn
  149.                 hReturn=ReadDir(sDirPath)
  150.         End If
  151.         nPROGRESS_CURRENT_BYTES=0
  152.  
  153.         Return hReturn
  154. End Function
  155.  
  156. ;--
  157.  
  158. Function hReadFileProgress%(sFilepath$)
  159.         Local hReturn%=0
  160.         If (FileType(sFilepath)=1)
  161.                 nPROGRESS_MAXIMUM_BYTES=FileSize(sFilepath)
  162.                 nPROGRESS_CURRENT_BYTES=0
  163.                 hReturn=ReadFile(sFilepath)
  164.         End If
  165.         Return hReturn
  166. End Function
  167.  
  168. Function hOpenFileProgress%(sFilepath$)
  169.         Local hReturn%=0
  170.         If (FileType(sFilepath)=1)
  171.                 nPROGRESS_MAXIMUM_BYTES=FileSize(sFilepath)
  172.                 nPROGRESS_CURRENT_BYTES=0
  173.                 hReturn=OpenFile(sFilepath)
  174.         End If
  175.         Return hReturn
  176. End Function
  177.  
  178. Function sReadLineProgress$(hFilehandle)
  179.         Local sReturn$=ReadLine(hFilehandle)
  180.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+Len(sReturn)+1  ;Add 1 for terminator
  181.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+Len(sReturn)+1
  182.         Return sReturn
  183. End Function
  184.  
  185. Function nReadIntProgress%(hFilehandle)
  186.         Local nReturn%=ReadInt(hFilehandle)
  187.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+4
  188.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+4
  189.         Return nReturn
  190. End Function
  191.  
  192. Function nReadShortProgress(hFilehandle)
  193.         Local nReturn%=ReadShort(hFilehandle)
  194.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+2
  195.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+2
  196.         Return nReturn
  197. End Function
  198.  
  199. Function sReadStringProgress$(hFilehandle)
  200.         Local sReturn$=ReadString(hFilehandle)
  201.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+Len(sReturn)+1  ; Add 1 for terminator
  202.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+Len(sReturn)+1
  203.         Return sReturn
  204. End Function
  205.  
  206. Function fReadFloatProgress#(hFilehandle)
  207.         Local fReturn#=ReadFloat(hFilehandle)
  208.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+4
  209.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+4
  210.         Return fReturn
  211. End Function
  212.  
  213. Function nReadByteProgress$(hFilehandle)
  214.         Local nReturn=ReadByte(hFilehandle)
  215.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+1
  216.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+1
  217.         Return nReturn
  218. End Function
  219.  
  220. Function nPeekByteProgress%(hBank%,nOffset%)
  221.         Local nReturn%=PeekByte(hBank,nOffset)
  222.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+1
  223.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_CURRENT_BYTES+1
  224.         Return nReturn
  225. End Function
  226.  
  227. Function nPeekIntProgress%(hBank%,nOffset%)
  228.         Local nReturn%=PeekInt(hBank,nOffset)
  229.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+4
  230.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_CURRENT_BYTES+4
  231.         Return nReturn
  232. End Function
  233.  
  234. Function fPeekFloatProgress%(hBank%,nOffset%)
  235.         Local fReturn#=PeekFloat(hBank,nOffset)
  236.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+4
  237.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_CURRENT_BYTES+4
  238.         Return fReturn
  239. End Function   
  240.  
  241. Function nPeekShortProgress%(hBank%,nOffset%)
  242.         Local nReturn%=PeekShort(hBank,nOffset)
  243.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+2
  244.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_CURRENT_BYTES+2
  245.         Return nReturn
  246. End Function   
  247.  
  248. ;--
  249.  
  250. Function WriteLineProgress$(hFilehandle,sLine$)
  251.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+Len(sLine)+1    ;Add 1 for terminator
  252.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+Len(sLine)+1
  253.         WriteLine hFileHandle,sLine
  254. End Function
  255.  
  256. Function WriteIntProgress%(hFilehandle,nInt%)
  257.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+4
  258.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+4
  259.         WriteInt hFileHandle,nInt
  260. End Function
  261.  
  262. Function WriteShortProgress%(hFilehandle,nShort%)
  263.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+2
  264.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+2
  265.         WriteShort hFileHandle,nShort
  266. End Function
  267.  
  268. Function WriteStringProgress$(hFilehandle,sString$)
  269.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+Len(sString)+1  ;Add 1 for terminator
  270.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+Len(sString)+1
  271.         WriteString hFileHandle,sString
  272. End Function
  273.  
  274. Function WritefloatProgress%(hFilehandle,fFloat#)
  275.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+4
  276.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+4
  277.         WriteFloat hFileHandle,fFLoat
  278. End Function
  279.  
  280. Function WriteByteProgress%(hFilehandle,nInt%)
  281.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+1
  282.         nPROGRESS_DIR_CURRENT_BYTES=nPROGRESS_DIR_CURRENT_BYTES+1
  283.         WriteByte hFileHandle,nInt Mod 256
  284. End Function
  285.  
  286. Function PokeByteProgress%(hBank%,nOffset%,nByte%)
  287.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+1
  288.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_CURRENT_BYTES+1
  289.         PokeByte hBank,nOffset,nByte
  290. End Function
  291.  
  292. Function PokeIntProgress%(hBank%,nOffset%,nInt%)
  293.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+4
  294.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_CURRENT_BYTES+4
  295.         PokeInt hBank,nOffset,nInt
  296. End Function
  297.  
  298. Function PokeFloatProgress%(hBank%,nOffset%,fFloat%)
  299.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+4
  300.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_CURRENT_BYTES+4
  301.         PokeFloat hBank,nOffset,fFloat
  302. End Function
  303.  
  304. Function PokeShortProgress%(hBank%,nOffset%,nShort%)
  305.         nPROGRESS_CURRENT_BYTES=nPROGRESS_CURRENT_BYTES+2
  306.         nPROGRESS_BANK_CURRENT_BYTES=nPROGRESS_BANK_CURRENT_BYTES+2
  307.         PokeShort hBank,nOffset,nShort
  308. End Function
  309.  
  310. ;--
  311.  
  312. Function nProgress%()
  313.         Return Int (fPercentage(nPROGRESS_CURRENT_BYTES,nPROGRESS_MAXIMUM_BYTES))
  314. End Function
  315.  
  316. Function nProgressDir%()
  317.         Return Int (fPercentage(nPROGRESS_DIR_CURRENT_BYTES,nPROGRESS_DIR_MAXIMUM_BYTES))
  318. End Function
  319.  
  320. Function nProgressBank%()
  321.         Return Int (fPercentage(nPROGRESS_BANK_CURRENT_BYTES,nPROGRESS_BANK_MAXIMUM_BYTES))
  322. End Function
  323.  
  324. Function DisplayProgressBar(X%,Y%,Width%,Height%,nProportion%,Txt$)
  325.         Color 255,255,255
  326.         Rect X,Y,Width,Height,0
  327.  
  328.         Text X,Y+Height*1.5,Txt$
  329.  
  330.         Color 255,0,0
  331.         Rect X+1,Y+1,Width-2,Height-2,1
  332.        
  333.         Color 0,255,0
  334.         Rect X+1,Y+1,nProportion*0.01*(Width-2),Height-2,1
  335. End Function
  336.  
  337. Function fPercentage#(nFraction%,nMaximum%)
  338.         If (nFraction=nMaximum)  Then Return 100.0
  339.         If (nFraction)*(nMaximum)
  340.                 Return Float(nFraction)/Float(nMaximum)*100
  341.         End If
  342.                 Return 0.0
  343. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal