Dumping File

Share your advanced PureBasic knowledge/code with the community.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Dumping File

Post by flaith »

Hi,

this is my 2 cents contribution (no more)

you can dump that way

Code: Select all

dump file.ext > dump.txt
or :

Code: Select all

dump -t 8 file.ext
to output with 8 bytes per line on the screen

Code:

Code: Select all

; Flaith
;version 0.1 : 22/09/07
;version 0.2 : 24/09/07

#QUOTE = Chr(34)
#LINE_4 = 4
#LINE_8 = 8
#ffile = 0

Structure HDUMP
  hex.s
  chr.s
EndStructure

Global Dim argv.s(10)
Global length.l = 0, _VAL = #False
Global *Buffer
Global NewList Dumpf.HDUMP()

Procedure.l GET_Argv()
Protected txt.s, i.l

  i = 0
  txt = ProgramParameter()

  While txt <> ""
    argv(i) = txt
    i+1
    txt = ProgramParameter()
  Wend
  ProcedureReturn i
EndProcedure

Procedure Add_Buffer(taille.l)

  i = 0 : counter = 0 : a$ = "" : b$ = ""
  a = PeekB(*Buffer+i) & $FF
  
  If a>31 And a<127
    b$ + Chr(a)
  Else
    b$ + "."
  EndIf

  a$ = RSet(Hex(a),2,"0") + " "
  i+1
  While i < length
    If i % taille = 0
      AddElement(DumpF())
        DumpF()\hex = RSet(Hex(counter),#LINE_8,"0")+ ":" +a$
        DumpF()\chr = b$
      a$ = "": b$ = "" : counter = i
    EndIf
    a = PeekB(*Buffer+i) & $FF
    If a>31 And a<127
      b$ + Chr(a)
    Else
      b$ + "."
    EndIf
    a$ + RSet(Hex(a),2,"0") + " "
    i+1
  Wend

  z = taille - (length-counter)
  sp$ = Space(z+z*2)

  AddElement(DumpF())
    DumpF()\hex = RSet(Hex(counter),#LINE_8,"0") + ":" + a$ + sp$
    DumpF()\chr = b$
EndProcedure

Procedure.l load_file(file.s,taille.l)
Protected fd.l, bytes.l
  
  fd = ReadFile(#ffile,file)
  If fd
    length = Lof(#ffile)                         ; Lit la taille en octets du fichier
    *Buffer = AllocateMemory(length)             ; alloue un bloc mémoire
    If *Buffer
      bytes = ReadData(#ffile, *Buffer, length)  ; Lit les caractères du fichier
      Add_Buffer(taille)
    Else
      PrintN("Allocation Memory Error")
      End
    EndIf
	Else
    ProcedureReturn -1
	EndIf
	
	CloseFile(#ffile)
	FreeMemory(*Buffer)
  ProcedureReturn 0
EndProcedure

Procedure Dump()
  ResetList(DumpF())
  ForEach DumpF()
    PrintN(DumpF()\hex+"- "+DumpF()\chr)
  Next
EndProcedure

Procedure Print_Version()
  PrintN("DumpF 0.2 : Dump file in hexa")
EndProcedure

Procedure Print_Usage()
  PrintN("usage: dumpf <OPTION> file")
  PrintN("")
  PrintN("  Option :")
  PrintN("    -t <VAL> : Dump to <VAL> bytes per line (default = 16)")
EndProcedure

Procedure Main(taille.l=16)
Protected file.s							        ; Nom du fichier à ouvrir
Protected argc.l

  _VAL = #False
  argc = GET_Argv()
    
  If argv(0) = ""                       ; Pas d'autres arguments
    Print_Version()
    Print_Usage()
    End
  Else                                  ; il y a d'autres arguments
    For i = 0 To argc
      If Left(argv(i),1) <> "-"         ; ce n'est pas une option
        Break
      EndIf
      Select LCase(Mid(argv(i),2,1))    ; c'est une option
        Case "t"
          _VAL = #True
          taille = Val(argv(1))
          If taille <= 0 : taille = 1 : EndIf
        Case "v"
          Print_Version()
          End
        Case "h"
          Print_Version()
          Print_Usage()
          End
        Case "l"
;          print_licence()
          End
        Default
          PrintN(">>> ERROR : Unknow option "+#QUOTE+Mid(argv(i),2,Len(argv(i))-1)+#QUOTE)
          Print_Usage()
          End
      EndSelect
	  Next i
  EndIf
  
  If _VAL = #True
    file = argv(2)
  Else
    file = argv(0)
  EndIf

  If Load_File(file,taille) = -1
    PrintN("Error loading file")
    End
  Else
    Dump()
  EndIf
   
EndProcedure

OpenConsole()
Main()
CloseConsole()
Last edited by flaith on Sat Oct 20, 2007 10:26 am, edited 2 times in total.
“Fear is a reaction. Courage is a decision.” - WC
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Very nice indeed. 8)

The simplest tools are often the best. Thanks.
I may look like a mule, but I'm not a complete ass.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

Thanks srod :)
“Fear is a reaction. Courage is a decision.” - WC
Post Reply