ConvertFileSize

Share your advanced PureBasic knowledge/code with the community.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

ConvertFileSize

Post by Guimauve »

Hello everyone,

Nothing special to do tonight, I have take the time to upgrade someone else codes. This time it's a code from Nico (French forum). This instruction convert FileSize given in Bytes or octets in convenient format to display in a GUI. You can choose between decimal or binary size format.

See here for detail about Decimal/Binary mode : http://en.wikipedia.org/wiki/Octet_%28computing%29

Have fun !

Best regards
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : ConvertFileSize
; File Name : ConvertFileSize.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : Guimauve
; Date : 10-02-2013
; Last Update : 10-02-2013
; PureBasic code : V5.10 Beta 7
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programming Notes
;
; This code is based on Nico's one (French Forum) but I have made some useful 
; (I think) changes. What I have done :
;
; 1. Added Binary/Decimal calculation mode
; 2. Extended FileSize calulation to exbioctet/exaoctet
; 3. Change variables names
;
; Link to original code : 
;
; http://www.purebasic.fr/french/viewtopic.php?f=6&t=13477&p=150853#p150853
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Enumeration
  
  #FILE_SIZE_CONVERSION_BINARY
  #FILE_SIZE_CONVERSION_DECIMAL
  
EndEnumeration

Procedure.s ConvertFileSize(FileSize.q, ConversionMode.b = #FILE_SIZE_CONVERSION_BINARY, NbDecimals.b = 2)
  
  Protected Resolution.s, IntegerPart.s, FractionPart.s, ComputedSize.d, Units.s, Iteration.l = 1
  
  ComputedSize = FileSize
  
  If ConversionMode <= #FILE_SIZE_CONVERSION_BINARY
    
    Units = ",Ki,Mi,Gi,Ti,Pi,Ei"
    
    While ComputedSize >= 1024 And Iteration <= 6
      ComputedSize / 1024
      Iteration + 1
    Wend   
    
  ElseIf ConversionMode >= #FILE_SIZE_CONVERSION_DECIMAL
    
    Units = ",K,M,G,T,P,E"
    
    While ComputedSize >= 1000 And Iteration <= 6
      ComputedSize / 1000
      Iteration + 1
    Wend
    
  EndIf
  
  Resolution = StrD(ComputedSize, NbDecimals)
  IntegerPart = StringField(Resolution, 1, ".")
  FractionPart = StringField(Resolution, 2, ".")
  
  If Iteration = 1
    ProcedureReturn IntegerPart + " octets"
  Else
    If Val(FractionPart) = 0
      ProcedureReturn IntegerPart + " " + StringField(Units, Iteration, ",") + "o"
    Else
      ProcedureReturn Resolution + " " + StringField(Units, Iteration, ",") + "o"
    EndIf
  EndIf
  
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<< 
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<< 
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

Debug ConvertFileSize(250, 0, 1)
Debug ConvertFileSize(250, 1, 1)

Debug ConvertFileSize(54817, 0) 
Debug ConvertFileSize(54817, 1)

Debug ConvertFileSize(123456789, 0, 3)
Debug ConvertFileSize(123456789, 1, 3)

Debug ConvertFileSize(9223372036854775807, 0, 3) ; Signed Quad limits
Debug ConvertFileSize(9223372036854775807, 1, 3)

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Last edited by Guimauve on Wed Feb 13, 2013 2:36 am, edited 1 time in total.
Dear Optimist, Pessimist,
and Realist,

While you guys were
busy arguing about the
glass of water, I DRANK IT !

Sincerely,
the Opportunist
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: ConvertFileSize

Post by Kwai chang caine »

Thanks :wink:
ImageThe happiness is a road...
Not a destination
Post Reply